您好,登錄后才能下訂單哦!
Python 腳本如下:
#!/usr/bin/env python #_*_coding:utf-8 _*_ __author__ = 'lvnian' #!/usr/bin env python # coding: utf-8 import MySQLdb as mysql import sys, os db = mysql.connect(user="root",passwd="xxxxx@2015",db="intest",host="192.168.10.12") #數(shù)據(jù)庫連接信息 db.autocommit(True) cur = db.cursor() cur.execute('set names utf8') dict_list = {} with open('access.log') as logfin: for line in logfin: arr = line.split(' ') # 獲取ip url 和status ip = arr[0] url = arr[6] status = arr[8] # ip url 和status當(dāng)key,每次統(tǒng)計(jì)+1 dict_list[(ip,url,status)] = dict_list.get((ip,url,status),0)+1 # 轉(zhuǎn)換成列表 ip_list = [(k[0],k[1],k[2],v) for k,v in dict_list.items()] # 按照統(tǒng)計(jì)數(shù)量排序,排序后保存到數(shù)據(jù)庫。 for insert in sorted(ip_list,key=lambda x:x[3],reverse=True):#如果只想提取前十行的話可以[:10]即可。 print insert #測試用的,可以不要。整個(gè)功能是輸出一行,然后保存到數(shù)據(jù)庫。 sql = 'insert loginfo values ("%s","%s","%s","%s")' % insert print sql cur.execute(sql)
創(chuàng)建對應(yīng)的表(在上一篇的基礎(chǔ)之上)
python分析nginx日志的ip(來源)
use intest; show tables; CREATE TABLE `loginfo` ( `ip` text DEFAULT NULL, `url` longtext DEFAULT NULL, `status` varchar(200) DEFAULT NULL, `count_num` varchar(200) DEFAULT NULL );
腳本執(zhí)行后數(shù)據(jù)庫內(nèi)看到的結(jié)果如下:
mysql> select * from loginfo where count_num >3 limit 5 ; +----------------+-----------------------+--------+-----------+ | ip | url | status | count_num | +----------------+-----------------------+--------+-----------+ | 121.42.0.85 | / | 200 | 39 | | 121.42.0.37 | / | 403 | 34 | | 121.42.0.39 | / | 403 | 34 | | 222.95.248.220 | HTTP://www.baidu.com/ | 200 | 26 | | 23.251.49.10 | www.baidu.com:443 | 400 | 21 | +----------------+-----------------------+--------+-----------+ 5 rows in set (0.00 sec) mysql>
添加一個(gè)時(shí)間字段腳本改為如下
#!/usr/bin/env python #_*_coding:utf-8 _*_ __author__ = 'lvnian' #!/usr/bin env python # coding: utf-8 import MySQLdb as mysql import sys, os,time import datetime addtime = datetime.datetime.now() db = mysql.connect(user="root",passwd="xxxx@2015",db="intest",host="192.168.10.12") #數(shù)據(jù)庫連接信息 db.autocommit(True) cur = db.cursor() cur.execute('set names utf8') dict_list = {} with open('access.log') as logfin: for line in logfin: arr = line.split(' ') # 獲取ip url 和status ip = arr[0] url = arr[6] status = arr[8] # ip url 和status當(dāng)key,每次統(tǒng)計(jì)+1 dict_list[(ip,url,status)] = dict_list.get((ip,url,status),0)+1 # 轉(zhuǎn)換成列表 ip_list = [(k[0],k[1],k[2],v,addtime) for k,v in dict_list.items()] # 按照統(tǒng)計(jì)數(shù)量排序,排序后保存到數(shù)據(jù)庫。 for insert in sorted(ip_list,key=lambda x:x[3],reverse=True):#如果只想提取前十行的話可以[:10]即可。 print insert #測試用的,可以不要。整個(gè)功能是輸出一行,然后保存到數(shù)據(jù)庫。 sql = 'insert loginfo values ("%s","%s","%s","%s","%s")' % insert print sql cur.execute(sql) #到數(shù)據(jù)庫輸出查詢?nèi)缦?23.125.160.217 這個(gè)ip經(jīng)常亂***別的服務(wù)器曝光一下,如見此ip訪問log,請直接撥打911
數(shù)據(jù)庫修改
數(shù)據(jù)庫增加一個(gè)字段 alter table loginfo add time datetime not Null; or重新建庫 CREATE TABLE `loginfo` ( `ip` text, `url` longtext, `status` varchar(200) DEFAULT NULL, `count_num` varchar(200) DEFAULT NULL, `time` datetime NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。