统计nginx单个IP访问日志并获取IP来源
2017-11-23 16:44
162 查看
#!/usr/bin/env python #coding=utf-8 import requests from urllib2 import urlopen # import lxml.html from bs4 import BeautifulSoup import subprocess import sys,os reload(sys) sys.setdefaultencoding('utf-8') # log file logfile = "./access.log" #result = "/tmp/result" # ip white list white_list = "8.8.8.8" # notify list mail_list = "xxx@sina.com xxx@xxx.com" cmd = """ awk '{ip[$1]++}END{for (k in ip){print ip[k],k}}' %s | sort -rn | head -20 | grep -v %s """ %(logfile, white_list) send_mail = 'mail -s "Walrus Rest Log Statistics" %s < /tmp/check' %mail_list label = "div" item = "class" reg = "well" def get_source(ip): url = "http://ip.chinaz.com/%s" %ip # BeautifulSoup parser html = urlopen(url) bsObj = BeautifulSoup(html,"html.parser") Check_result = bsObj.findAll("span",{"class":"Whwtdhalf w50-0"}) # get source for i in Check_result: if "IP" in i.get_text(): pass else: ip_source = i.get_text() return ip_source def run_cmd(): f = open("/tmp/check", 'w') # start command command_result = subprocess.Popen(cmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE) #temp = command_result.stdout.read() for i in command_result.stdout: ip = i.split()[1] source = get_source(ip) char = "%s\t%s\n" %(i,source) f.write(char) f.close() os.system(send_mail) return if __name__ == '__main__': run_cmd()
相关文章推荐
- 用nginx转发请求tomcat 如何配置访问日志获取真实ip
- 用nginx转发请求tomcat 如何配置访问日志获取真实ip
- nginx前端,tomcat后端服务器获取客户的真实IP,包括tomcat访问日志获取真实IP的配置
- linux 获取经过N层Nginx转发的访问来源真实IP
- linux 获取经过N层Nginx转发的访问来源真实IP
- Linux分析日志获取最多访问的前10个IP
- Asp.Net : 捕捉和记录网站中出现的所有未处理错误,抛出详细的页面来源和访问ip,调用的接口方法及异常实例(记事本日志,系统日志及数据库日志)
- linux 获取经过N层Nginx转发的访问来源真实IP
- 获取访问来源的真实IP
- nginx 日志获取不到远程访问ip问题解决~~~
- 统计nginx日志ip访问数
- 统计nginx日志单IP访问请求数排名
- 手机访问网站获取外网IP
- php获取访问的ip
- PHP获取访客IP、地区位置信息、浏览器、来源页面
- Shell 命令行统计 apache 网站日志访问IP以及IP归属地
- nginx反向代理tomcat日志获取真实IP
- 通过apache 访问日志access.log 统计IP 和每个地址访问的次数
- nginx做反向代理时,配置其让tomcat的日志获取客户端ip
- Linux分析apache日志获取最多访问的前10个IP