来看具体例子:
1,grep与linuxjishu/14080.html target=_blank class=infotextkey>wc命令查看apache进程
ps aux | grep httpd | grep -v grep | wc -l
2,netstat命令与grep查看80端口的tcp连接
netstat -tan | grep "ESTABLISHED" | grep ":80" | wc -l
3,通过日志查看当天ip连接数,过滤重复
cat access_log | grep "20/Oct/2008" |
awk '{print $2}' | sort | uniq -c | sort -nr
4,当天ip连接数最高的ip(蜘蛛抓取)
cat access_log | grep "20/Oct/2008:00" | grep "122.102.7.212" | awk '{print $8}' | sort | uniq -c | sort -nr | head -n 10
5,当天访问页面排前10的url:
cat access_log | grep "20/Oct/2008:00" | awk '{print $8}' | sort | uniq -c | sort -nr | head -n 10
6,用tcpdump嗅探80端口的访问看看谁最高
tcpdump -i
eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr
然后,从日志里查看该ip访问了哪些页面:
cat access_log | grep 122.102.7.212| awk '{print $1"t"$8}' | sort | uniq -c | sort -nr | less
7,查看某一时间段的ip连接数:
grep "2006:0[7-8]" www20060723.log | awk '{print $2}' | sort | uniq -c| sort -nr | wc -l
awk数组执行时间会更短:
awk '{++S[$2]} END {for (a in S) print S[a],a}' access_log | sort -rn | head -n 10
总结:linux日志文件分析,无论是系统日志,还是nginx日志或apache日志,用好awk命令、grep命令,以及排序命令sort,即可高效处理与分析日志,得到预期的结果。