为了统计每个IP的访问次数,偿试着写了一个python脚本,得到前10个访问次数最多的IP:
def countstatics(result):
"""Compute the statics."""
li = result.split('\n')
l = {}
for a in set(li):
l[a] = 0
for a in li:
l[a] = l[a] + 1
print "The uniq ip number is ", len(l)
return l
def apachelog(path):
"""analyse the apachelog,get the top 10 ip address which visit my server
"""
print 'analyse the apachelog,get the top 10 ip address which visit my server'
cmd = 'cut -d " " -f1 ' + path
result = exec_shell(cmd)
li = countstatics(result)
l = ["%s %s" % (k, v) for v, k in li.items() if k > 1000]
l.sort(cmp=None, key=None, reverse=True)
#print l
for i in range(10):
print l[i]
if __name__ == "__main__":
print 'main'
apachelog("/var/log/apache2/access_log")
print 'finished'
运行结果:
main
apachelog
The uniq ip number is 30907
97374 192.168.0.160
8676 59.42.196.130
8313 121.228.230.114
7909 59.49.232.157
7909 221.239.137.161
6130 117.80.191.13
6056 121.227.155.37
5666 119.32.45.219
5633 220.166.172.5
5295 123.124.228.6
finished
访问次数最多的还是本地IP,再接上whois就可以查看详细的信息了。
Python 分析apache log脚本
实际只需要一行命令就能达成了
sudo cat access.log|cut -d “-” -f1|sort|uniq -c|sort -k1nr|head -n 50
439 64.120.56.210
380 5.9.144.195
222 114.115.136.7
158 69.30.198.178
68 66.249.75.89
63 66.249.75.95
51 66.249.75.90
50 66.249.75.93
50 66.249.75.94
40 138.197.13.94
37 66.249.75.91
22 172.105.225.18
13 106.11.155.143
13 106.11.157.135
13 106.11.159.147
13 169.145.197.13
12 106.11.152.158
12 106.11.158.133
11 106.11.154.141
11 138.197.111.28
10 124.238.60.162
10 216.244.66.243