python分析服务器日志_python实现web服务器日志分析脚本
python日志分析腳本
用python可以實現大日志文件的分析,比如查到sql注入語句,然后看到IP,就可以改下腳本,用IP為特征取出日志,分析入侵過程。
python比較shell腳本的優點是速度快,性能好,跑1G日志文件也就幾秒鐘。
使用參數:seay.py E:/1.log
python日志分析腳本: 代碼示例:
#coding = utf8
#Filename = seay.py
import os
import sys
#特征,可以隨意改,兩塊五一次
_tezheng = {'union','select','file_put_contents'}
def CheckFile(_path):
_f = open(_path,"r")
_All_Line = _f.readlines()
_f.close()
_Count_Line =0
_Len_Line = len(_All_Line)
_Ex_Str = ''
print('Read Over --')
while _Count_Line<_len_line:>
_Str = _All_Line[_Count_Line]
for _tz_Str in _tezheng:
if _tz_Str in _Str: #可以加and條件,這個貴一點,5毛一次
_Ex_Str+=_tz_Str+_Str+'\r\n'
_Count_Line+=1
_f1 = open(_path+'.seay.txt',"w")
_f1.write(_Ex_Str)
_f1.close()
print 'Find Over--'
if len(sys.argv)==2:
_File = sys.argv[1]
if os.path.lexists(_File):
CheckFile(_File)
else:
print('File does not exist!')
else:
print 'Parameter error'
print sys.argv[0]+' FilePath'
文件為:原文件名.seay.txt在同目錄下,格式為匹配的特征+日志,效果:
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python分析服务器日志_python实现web服务器日志分析脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML可以替代CSS的所有功能,CSS
- 下一篇: python random函数_Pyth