您的位置:首页 > 运维架构 > Shell

shell 截取日志 统计链接数 统计日志ip

2015-12-22 10:29 507 查看
按时间段获取日志
sed -n '/22\/Dec\/2015:00/,/22\/Dec\/2015:50/p' localhost_access_log.2015-11-10.txt > /tmp/acc.www.log
sed -n '/22\/Dec\/2015:00/,/22\/Dec\/2015:50/p' localhost_access_log.2015-11-10.txt |head -3
sed -n '/22\/Dec\/2015:00/,/22\/Dec\/2015:50/p' localhost_access_log.2015-11-10.txt |tail -3
======================================
awk '/22\/Dec\/2015:00/,/22\/Dec\/2015:09/ {print $0}' access_www.log > /tmp/acc.www.log
awk '/22\/Dec\/2015:00/,/22\/Dec\/2015:09/ {print $0}' access_www.log |head -3
awk '/22\/Dec\/2015:00/,/22\/Dec\/2015:09/ {print $0}' access_www.log |tail -3
============================================================================


#######################################################
统计链接数:
netstat -tan | awk '/^tcp/{++state[$NF]}END{for (s in state) {print s"============"state[s]}}'
netstat -tan|awk '/^tcp/{S[$6]++ } END {for(n in S) {print n "============" S
}}'
------------------------
[root@iZ23eoou07sZ ~]# netstat -a|awk '/tcp/{S[$6]++ } END {for(n in S) {print n "============" S
}}'
TIME_WAIT============292
ESTABLISHED============13
SYN_RECV============2
LAST_ACK============3
LISTEN============5
[root@iZ23eoou07sZ ~]#
# netstat -tn | awk '/^tcp/{lens=split($5,client,":");ip[client[1]]++}END{for (i in ip) print i,ip[i]}'


#######################################################
统计每个IP的连接数:
netstat -ntu | awk -F'[ :]+' '/^tcp/{print $6}'|sort |uniq -c |sort -rn|head -5
netstat -ntu | awk '{ print $5}' | cut -d : -f 1 | sort | uniq -c| sort -n -r | head -n 5 | grep -v 127.0.0.1"
netstat -ntu | tail -n +3 | awk '{ print $5}' | cut -d : -f 1 | sort | uniq -c | sort -n -r |head -n 3
netstat -ntu | tail -n +3 | awk '{ print $5}' | egrep -o  "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" | sort | uniq -c | sort -n -r


#######################################################
统计日志的IP数
awk '{ip[$1]++}END{for (i in ip) {print ip[i], i}}' access_www.log  |sort -rn -k1 |head -10

[salt@iZ23fxh0i4lZ nginx]$ awk '{ip[$1]++}END{for (i in ip) {print ip[i], i}}' access_www.log  |sort -rn -k1 |head -10
6792 140.206.49.178
6106 66.249.69.247
4734 115.159.29.191
2501 42.121.119.229
2444 114.119.8.92
2179 10.162.85.26
1741 10.51.0.209
1710 121.37.61.220
1580 121.41.37.72
1568 66.249.75.95
[salt@iZ23fxh0i4lZ nginx]$


本文出自 “奋斗吧” 博客,请务必保留此出处http://lvnian.blog.51cto.com/7155281/1727079
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: