shell统计日志

时间:2023-03-09 04:30:32
shell统计日志
#nginx日志统计独立ip的个数:
awk '{print $1}' /path-to-log-dir/access.log | sort | uniq | wc -l
#查询访问最多的前10个ip
awk '{print $1}' /path-to-log-dir/access.log | sort | uniq -c | sort -nr | head -
#查看某段时间的
grep "2012:0[3-6]" nginx.log |
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 127.0.0.1: 127.0.0.1: TIME_WAIT
tcp 127.0.0.1: 127.0.0.1: TIME_WAIT
tcp 127.0.0.1: 127.0.0.1: TIME_WAIT
tcp 127.0.0.1: 127.0.0.1: TIME_WAIT
tcp 127.0.0.1: 127.0.0.1: TIME_WAIT
tcp 127.0.0.1: 127.0.0.1: TIME_WAIT
tcp 127.0.0.1: 127.0.0.1: TIME_WAIT
tcp 127.0.0.1: 127.0.0.1: TIME_WAIT
tcp 192.168.32.62: 192.168.47.207: TIME_WAIT
tcp 192.168.32.62: 192.168.47.207: TIME_WAIT #访问次数最多的IP
netstat -ntu | tail -n + | awk '{ print $5}' | cut -d : -f | sort | uniq -c| sort -n -r | head -n
tail -n + :去掉前两行。
awk '{ print $5}':取数据的低5域(第5列)
cut -d : -f :取IP部分。
sort:对IP部分进行排序。
uniq -c:打印每一重复行出现的次数。(并去掉重复行)
sort -n -r:按照重复行出现的次序倒序排列。
head -n :取排在前5位的IP