天天看點

常用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 

本文轉自 15816815732 51CTO部落格,原文連結:http://blog.51cto.com/68686789/1694914

繼續閱讀