五年屌絲運維工作shell精華

屌絲運維常用shell
列出你最常用的10條shell
history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
history | awk '{a[$4]++}END{for(i in a){print a[$i] " " i}}' | sort -rn | head
grep -v "#" .bash_history |awk '{++a[$1]}END{for(i in a)print i,a[i]|"sort -k2 -nr"}'  | head
網絡連接數目
netstat -an | grep -E "^(tcp)" | cut -c 68- | sort | uniq -c | sort -n     #查看狀態數連接數
netstat -ntu | awk '{print $5"\n"}' | cut -d: -f1 | sort | uniq -c | sort -nr|head -n 20 #統計IP連接數
netstat -an -t | grep ":22" | grep ESTABLISHED | awk '{printf "%s %s\n",$5,$6}' | sort | wc -l #進程連接數
取網卡IP
/sbin/ifconfig |sed 's/.*inet addr:\(.*\) Bca.*/\1/g' |sed -n '/br/{n;p}' #雙網卡綁定用這個
/sbin/ifconfig |sed 's/.*inet addr:\(.*\) Bca.*/\1/g' |sed -n '/eth/{n;p}' #普通網卡用這個
ifconfig eth0 |grep "inet addr:" |awk '{print $2}'|cut -c 6-  或者
ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
系統信息統計
dmidecode -t system |grep -E 'Serial'|awk -F':' '{print $2}' (系統序列號查詢)
cat /proc/cpuinfo  | grep CPU | awk  -F: '{print $2}' |  sort|uniq -c (cpu核數)
dmidecode -t system |grep 'Product'|awk '{print $3$4}'  (單板設備類型)
dmidecode | grep -P -A 5 'Memory Device' | grep Size | grep -v Range|grep -i -v "no module"|sed -r 's/^\s+//g' | sort|uniq -c (內存大小)
echo `/sbin/ifconfig |sed 's/.*inet addr:\(.*\) Bca.*/\1/g' |sed -n '/eth/{n;p}' ` `hostname` >>/etc/hosts 取IP和主機名定向到/etc/hostname  
系統抓包分析
tcpdump -c 10000 -i eth0 -n dst port 80 > /root/pkts  (tcpdump 抓包 ,用來防止80端口被人***時可以分析數據 )
less | awk ' {printf $3"\n"}' | cut -d. -f 1-4 | sort | uniq -c | awk '{printf $1" "$2"\n"}' | sort -n -t\   +0 (然後檢查IP的重複數 並從小到大排序 注意 "-t\ +0" 中間是兩個空格 )
系統進程管理
ps -eo pid,lstart,etime | grep 26871 (進程運行時間)
lsof -p 10412 (查看進程打開的文件 10412是進程的PID)
ps -e -o "%C : %p : %z : %a"|sort -k5 -nr  (查看進程 按內存從大到小排列 )
 ps -e -o "%C : %p : %z : %a"|sort -nr     (按cpu利用率從大到小排列)
ps aux |grep mysql |grep -v grep |awk '{print $2}' |xargs kill -9  (殺掉mysql進程)
killall -TERM mysqld  殺掉mysql進程:
ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9 殺掉僵死進程
網卡流量
dstat -acdgilmnprstTfy (centos查看網卡流量)
iftop   (suse系統網卡流量)
sar -n DEV 1 10 (suse系統網卡流量)
iotop -o (查看那個進程最磨磁盤)
文件管理
刪除0字節文件
find -type f -size 0 -exec rm -rf {} \;
查看目錄下10個大文件
du -cks * | sort -rn | head -n 10
du -h --max-depth=1  /home
用三種方法將兩列合併成一行
cat test
192.168.110.171
00:1F:D0:D4:DD:47
xargs -n 2 < test
sed 'N;s/\n/ /' test
awk -v ORS="" '{printf $0" "}NR%2==0{print "\n"}' 1 test
192.168.110.171 00:1F:D0:D4:DD:47

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章