標籤:python
問題:分析nginx日誌並找出訪問最多10個IP地址的來源以及次數
使用python模組IP
使用方法以及:https://pypi.python.org/pypi/17MonIP
相關python指令碼:
#!/usr/bin/env python#coding:utf8#Auth: lad#date:2016-12-05#desc:parser the nginx‘s log,the head of 10 import sysreload(sys)sys.setdefaultencoding( "utf-8" )import IPimport oscnt=10fd = open("/tmp/IP.txt","w")ipStr = os.popen("cat /tmp/access.log|awk ‘{print $6}‘|sort | uniq -c | sort -nr | head -10 ") fd.write(ipStr.read()) fd.close()fd1 = open("/tmp/IP.txt","r")fd2 = open("/tmp/nginx_IP.txt","w+")for i in range(1,cnt+1): d = fd1.readline() print >> fd2,"訪問次數:%8s 訪問IP:%16s IP地址來源:%20s"%(d.split()[0],d.split()[1],IP.find(d.split()[1]))fd1.close()fd2.close()print "OK"
具體使用awk分割的時候需要考慮自己日誌的格式,根據格式切割。
執行結果:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/8B/80/wKioL1hP8h7xhk80AAHrWjU8byU453.jpg-wh_500x0-wm_3-wmp_4-s_1561238548.jpg" title="nginx_IP.jpg" alt="wKioL1hP8h7xhk80AAHrWjU8byU453.jpg-wh_50" />
本文出自 “share,open source” 部落格,請務必保留此出處http://liqilong2010.blog.51cto.com/3029053/1882400
python分析nginx日誌