维护脚本一例,写得有点乱,只是作为一个实例,演示如何快速利用工具快速达到目的:
应用到:shell与python数据交互、数据抓取,编码转换
复制代码 代码如下:
'''
程序说明:apache access.log日志分析
分析访问网站IP 来源情况
日期:2014-01-06 17:01
author:gyh9711
程序说明:应用到:shell与python数据交互、数据抓取,编码转换
'''
import os
import json
import httplib
import codecs
LogFile='/var/log/apache2/access.log'
logMess='/tmp/acc.log'
if os.path.isfile(logMess):
os.system('cp /dev/null %s'% logMess)
file=codecs.open(logMess,'w+',encoding='utf-8')
def cmd(cmd):
return os.popen(cmd).readlines()
'''
def getIp(ip):
return json.loads(os.popen("/usr/bin/curl http://ip.taobao.com/service/getIpInfo.php?ip=%s" % ip).readline())['data']
'''
conn = httplib.HTTPConnection('ip.taobao.com')
def getIpCountry(ip):
conn.request('GET','/service/getIpInfo.php?ip=%s' % ip)
r1=conn.getresponse()
if r1.status == 200:
return json.loads(r1.read())['data']
else:
return "Error"
file.write(u"字段说明:ip 访问次数据 ip国家 城市的 isp号 省份 所在地区\n")
ipDb=[]
for i in cmd('''/usr/bin/awk '{print $1}' %s |sort |uniq -c''' % LogFile):
ip = i.strip().split(' ')
ipDb.append(ip)
for i in ipDb:
_tmpD=getIpCountry(i[1])
out="%s%s%s%s%s%s%s"%(i[1].ljust(20),i[0].ljust(10),_tmpD['country'].ljust(20),_tmpD['city'].ljust(16),_tmpD['isp_id'].ljust(16),_tmpD['region'].ljust(16),_tmpD['area'].ljust(16))
print out
file.write("%s\n"%out)
conn.close()
file.close()
'''
'''
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8