使用python 分析统计nginx访问日志ip次数并且排序

1 数据样本

221.7.8.21 - - [05/Dec/2017:00:04:53 +0800] "GET /qbwx/ml_files/ss33.jpg HTTP/1.1"   "http://tt.xxx.cn/ocpc/haikang/ocpc/12?ETT02" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:53 +0800] "GET /qbwx/ml_files/ss55.jpg HTTP/1.1"   "http://tt.xxx.cn/ocpc/haikang/ocpc/12?ETT02" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:53 +0800] "GET /qbwx/ml_files/ss22.jpg HTTP/1.1"   "http://tt.xxx.cn/ocpc/haikang/ocpc/12?ETT02" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:54 +0800] "GET /qbwx/ml_files/ss44.gif HTTP/1.1"   "http://tt.xxx.cn/ocpc/haikang/ocpc/12?ETT02" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:54 +0800] "GET /qbwx/ml_files/ss11.jpg HTTP/1.1"   "http://tt.xxx.cn/ocpc/haikang/ocpc/12?ETT02" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:55 +0800] "GET /qbwx/ml_files/ss66.jpg HTTP/1.1"   "http://tt.xxx.cn/ocpc/haikang/ocpc/12?ETT02" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:58 +0800] "GET /qbwx/ml_files/get_wechat HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:58 +0800] "GET /qbwx/ml_files/toutiao-track-log.js HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:58 +0800] "GET /qbwx/ml_files/jquery-1.8.0.min.js HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:58 +0800] "GET /qbwx/ml_files/jquery-1.8.0.min.js?visitDstTime=1 HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:59 +0800] "GET /qbwx/ml_files/statistical.js HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:59 +0800] "GET /qbwx/ml_files/anli11.jpg HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:59 +0800] "GET /qbwx/ml_files/anli22.jpg HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:59 +0800] "GET /qbwx/ml_files/1111.jpg HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:59 +0800] "GET /qbwx/ml_files/anli33.jpg HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:59 +0800] "GET /qbwx/ml_files/zs111.jpg HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:59 +0800] "GET /qbwx/ml_files/zs222.jpg HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:04:59 +0800] "GET /qbwx/ml_files/zs333.jpg HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:05:00 +0800] "GET /qbwx/ml_files/zs444.jpg HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:05:00 +0800] "GET /qbwx/ml_files/portrait-01.jpg HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:05:00 +0800] "GET /qbwx/ml_files/portrait-02.jpg HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"
221.7.8.21 - - [05/Dec/2017:00:05:00 +0800] "GET /qbwx/ml_files/portrait-03.jpg HTTP/1.1"   "http://wx.yyy.com/qbwx/n_202_ml_hkx.html" "Mozilla/5.0 (Linux; Android 6.0.1; ATH-AL00 Build/HONORATH-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.0.4 NetType/3g" "-"


2 分析程序代码

import re
from collections import Counter
lines=[]
ips=[]
with open("sesier.txt",'r') as f:
    for line in f:
        ip=re.search('\d{3}\.\d{3}\.\d{3}\.\d{3}',line

        if ip is not None:
            ips.append(ip.group())

ip_counter=Counter(ips)
ip_c=ip_counter.most_common()

for item in ip_c:
    print("IP 地址:%s,访问次数:%d"%(item[0],item[1]))

print("访问总次数:%d"%len(ips))

3 分析结果

IP 地址:182.146.242.201,访问次数:40
IP 地址:115.215.133.135,访问次数:40
IP 地址:112.101.108.239,访问次数:39
IP 地址:117.179.192.245,访问次数:39
IP 地址:115.205.105.145,访问次数:39
IP 地址:106.115.102.215,访问次数:38

你可能感兴趣的:(python,系统运维企业案例)