python日志分析脚本代码分享

发布时间:2019-10-18编辑:脚本学堂
一段python日志分析脚本代码,python分析网站日志文件的方法,对指定格式的日志文件进行日志分析得出结果,需要的朋友参考下。

python写的日志分析脚本

1、日志分析代码
 

复制代码 代码示例:

#!/usr/bin/python
#
import re 

f=open("/tmp/a.log","r") 
arr={} 
lines = f.readlines() 
for line in lines: 
    ipaddress=re.compile(r'^#(((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?))') 
    match=ipaddress.match(line) 
    if match: 
        ip = match.group(1) 
        if(arr.has_key(ip)): 
            arr[ip]+=1 
        else: 
            arr.setdefault(ip,1) 
         
f.close() 
for key in arr: 
    print key+"->"+str(arr[key]) 

2、日志文件格式:
 

#111.172.249.84 - - [12/Dec/2011:05:33:36 +0800] "GET /images/i/goTop.png HTTP/1.0" 200 486 "http://wh.xxxx.com/" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" 
#111.172.249.84 - - [12/Dec/2011:05:33:36 +0800] "GET /images/i/goTop.png HTTP/1.0" 200 486 "http://wh.xxxx.com/" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" 
#111.172.249.85 - - [12/Dec/2011:05:33:36 +0800] "GET /images/i/goTop.png HTTP/1.0" 200 486 "http://wh.xxxx.com/" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" 
#111.172.249.86 - - [12/Dec/2011:05:33:36 +0800] "GET /images/i/goTop.png HTTP/1.0" 200 486 "http://wh.xxxx.com/" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" 

3、日志文件分析,输出结果:
 

111.172.249.86->1 
111.172.249.84->2 
111.172.249.85->1