Unix(Bash/Python/小命令/小应用)

站内跳转

一、基础命令组合
二、History用户执行语句记录保存
三、探测局域网内有多少在线主机
四、Centos Cmake 升级 3.9

一、基础命令组合


1. 磁盘占用分析利器 
# du --max-depth=1 -h /           ## max前面是两个" - "
2. 内存使用
# free -m|sed -n '2p'|awk '{print ""$3/$2*100"%"}'
# free -m|sed -n '2p'|awk '{print "used mem is "$3"M,total mem is "$2"M,used percent is "$3/$2*100"%"}'
3.查看磁盘坏块
# badblocks -s -v 
4.查看http的并发请求数及其TCP连接状态:
# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
5.用tcpdump嗅探80端口的访问看看谁最高, head 前 20
# tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -20
6.Linux开机启动顺序
# 启动顺序:
# POST(加电自检)-->由BIOS决定启动设备次序-->MBR(bootloader)-->解压kernel,加载Kernel到内存-->加载init进程(/etc/inittab)
7.符号链接和硬链接的区别
硬链接:不同路径的文件指定的是同一个inode
#      硬链接不能跨分区存在
#      硬链接不能链接到目录(要避免循环引用)
#      删除硬链接只会减少文件被硬链接的次数,源文件不会动.
软链接:是一个独立的文件,有自己独立的inode
#      引用的是路径本身,不是inode
#      删除软链接,跟源文件没任何关系
#      软链接没有存储任何数据,只是存储了访问文件的另一种路径.
#      软链接可以对目录创建,也可以跨分区
#      源文件被删除,软连接将不可用.
8.检测并自动修复文件系统
#e2fsck - check a Linux ext2/ext3 file system
#     -B     指定块大小
#     -f     强制检测.
#     -p     自动修复文件系统
9.在指定范围内取随机数
# echo $[$RANDOM%39] 对39进行取余,余数肯定是39以内的数字.
10.Linux挂载Winodws共享文件夹
# mount -t cifs //1.1.1.254/ok /var/tmp/share/ -o username=administrator,password=yourpass
11.将接收到的6805端口数据,封装成10051端口数据,进行接收
#iptables -t nat -A PREROUTING -p tcp -m tcp --dport 6805 -j REDIRECT --to-ports 10051  

二、History用户执行语句记录保存


将下面这段代码添加到/etc/profile 文件中,即可实现上述功能。
#history
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
HISTDIR=/usr/share/.history
if [ -z $USER_IP ]
then
USER_IP=`hostname`
fi
if [ ! -d $HISTDIR ]
then
mkdir -p $HISTDIR
chmod 777 $HISTDIR
fi
if [ ! -d $HISTDIR/${LOGNAME} ]
then
mkdir -p $HISTDIR/${LOGNAME}
chmod 300 $HISTDIR/${LOGNAME}
fi
export HISTSIZE=4000
DT=`date +%Y%m%d_%H%M%S`
export HISTFILE="$HISTDIR/${LOGNAME}/${USER_IP}.history.$DT"
export HISTTIMEFORMAT="[%Y.%m.%d %H:%M:%S]"
chmod 600 $HISTDIR/${LOGNAME}/*.history* 2>/dev/null
这段代码将每个用户的shell命令执行历史以文件的形式保存在/usr/share/.history目录中,
每个用户一个文件夹, 并且文件夹下的每个文件以IP地址加shell命令操作时间的格式命名。
保存历史命令的文件夹目录要尽量隐蔽,避免被黑客发现后删除。

三、探测局域网内有多少在线主机


[root@Luckyero ~]# vim wlan.sh
#!/bin/bash
# ping check scripts
for IP in 10.10.3.{1..10}
do
 ping -c 2 -i 0.1 -W 1 $IP &> /dev/null
 if [ $? -eq 0 ]
  then
   echo "Host $IP is up."
  else
   echo "Host $IP is down."
 fi
done
探测局域网内存活主机:10.10.3.0网段

四、Centos Cmake 升级 3.9


## 升级 Cmake 到 3.9
yum install -y gcc gcc-c++ make automake wget
wget https://cmake.org/files/v3.9/cmake-3.9.0-rc3.tar.gz
tar -zxvf cmake-3.9.0-rc3.tar.gz
cd cmake-3.9.0-rc3
./bootstrap
gmake
gmake install
## 如果安装报错,删除 CmakeCache.txt 从 bootstrap 从心开始
## 下方表示安装成功
[100%] Built target pseudo_valgrind 

################################################################################
开源

发表于 2年前 (2017-01-09) 2017-01-9 10:43 星期一
2 COMMENTS
  1. 2017/02/08
    Lora Wood

    Hello my name is Lora Wood and I just wanted to send you a quick note here instead of calling you. I discovered your 常用命令及脚本 – 孙先生 website and noticed you could have a lot more visitors. I have found that the key to running a popular website is making sure the visitors you are getting are interested in your website topic. There is a company that you can get keyword targeted visitors from and they let you try their service for free for 7 days. I managed to get over 300 targeted visitors to day to my website. http://www.v-diagram.com/2syza

  2. 2017/02/15
    Sandra

    I decided to leave a message here on your 常用命令及脚本 – 孙先生 page instead of calling you. Do you need more likes for your Facebook Fan Page? The more people that LIKE your website and fanpage on Facebook, the more credibility you will have with new visitors. It works the same for Twitter, Instagram and Youtube. When people visit your page and see that you have a lot of followers, they now want to follow you too. They too want to know what all the hype is and why all those people are following you. Get some free likes, followers, and views just for trying this service I found: http://v-doc.co/nm/39zu3

LEAVE A REPLY

loading
32