python实现扫描日志关键字
来源:互联网 发布:淘宝管理中心在哪 编辑:程序博客网 时间:2024/04/30 08:59
转载请注明出处:http://write.blog.csdn.net/postedit/78039889
我们在压力测试过程会收集到很多log,怎样快速从中找到有用信息呢?让python脚本帮我们做这部分工作吧!
废话不说,上代码
环境:win10 + python2.7.14
#-*- encoding: utf-8 -*-#author : beihuijie#version 1.1import reimport sysimport osimport countTimedef getParameters(): ''' get parameters from console command ''' with open(sys.argv[1], "r") as fread: lines = fread.readlines() keywords=[] for line in lines: temp = line.split(', ') keywords.append(temp) for i in range(0, (len(keywords[0]) - 1)): print ' Keyword = %s' % keywords[0][i] return keywords[0]def isFileExists(strfile): ''' check the file whether exists ''' return os.path.isfile(strfile)def Search(keyword, filename): ''' search the keyword in a assign file ''' if(isFileExists(filename) == False): print 'Input filepath is wrong,please check again!' sys.exit() linenum = 1 findtime = 0 with open(filename, 'r') as fread: lines = fread.readlines() for line in lines: rs = re.findall(keyword, line, re.IGNORECASE) if rs: #output linenum of keyword place sys.stdout.write('line:%d '%linenum) lsstr = line.split(keyword) strlength = len(lsstr) findtime = findtime + 1 #print strlength for i in range(strlength): if(i < (strlength - 1)): sys.stdout.write(lsstr[i].strip()) sys.stdout.write(keyword) else: sys.stdout.write(lsstr[i].strip() + '\n') linenum = linenum + 1 print '+----------------------------------------------------------------------------+' print (' Search result: find keyword: %s %d times'%(keyword, findtime)) print '+----------------------------------------------------------------------------+'def executeSearch(): ''' this is a execute search method ''' ls = getParameters() start = countTime.getTime() parameter_number = len(ls) print 'Filename = %s ' % ls[parameter_number - 1] print '--------------------start search-------------------------' if(parameter_number >= 2): for i in range(parameter_number - 1): Search(ls[i], ls[parameter_number - 1]) else: print 'There is a parameter error occured in executeSearch()!' end = countTime.getTime() print '+----------------------------------------------------------------------------+' print ' Total cost time: %s'%countTime.formatTime(end - start) print '+============================================================================+' if __name__=='__main__': executeSearch()
countTime.py
#-*- encoding: utf-8 -*-#author : beihuijie#version 1.1import datetimeimport timedef getTime(): ''' return time is format of time(unit is second) ''' return time.time()def getCPUClockTime(): ''' return time is CPU Clock time ''' return time.clock()def formatTime(timevalue): ''' format the time numbers ''' hour = 0 minute = 0 second = 0 if timevalue > 0: #count hour hour = timevalue // 3600 remain = timevalue % 3600 #count minute minute = remain // 60 remain = remain % 60 #count second second = round(remain, 3) return '%.0fh:%.0fm:%.3fs'%(hour, minute, second) if __name__=='__main__': value = 134.45632 print value print formatTime(value)
关键字及被扫描的日志路径信息,记录到文件中,以逗号+空格隔开,如,“, ”日志路径信息放到最后。格式如下:
anr, dalvikvm: Could not find class 'android.app.usage., panic, C:\Users\BHJ\logcat1.log
执行结果:
阅读全文
0 0
- python实现扫描日志关键字
- python实现简单Nmap扫描
- python实现关键字查询
- python实现log日志
- 【Python】实现网站常见漏洞扫描+备份扫描+源码分析
- Python安全小工具之日志分析识别AWVS扫描
- python实现局域网ip地址扫描
- python实现局域网ip地址扫描
- python多线程实现局域网活跃IP扫描
- Python实现wvs进行批量扫描
- 【Python】实现网站备份文件扫描+源码分析
- python 日志增量抓取实现
- 基于python实现日志收集
- python写的日志监控程序,关键字统计,日志大小监控,关键字出现报警并截取日志
- 20090318-Ajaxian扫描日志
- 关键字匹配之BF算法-python实现
- 使用python实现自动“扫描检测硬件改动”功能
- 区域填充之扫描线种子法的Python实现
- Deepin系统更新apt-get源
- JS实现TITLE悬停长久显示效果
- AS问题记录
- 无法连接到 .
- 聚集索引和非聚集索引(整理)
- python实现扫描日志关键字
- [caioj1462][exkmp]回文串
- Node.js 安装配置
- 算法(五)规则矩阵中找值
- 注册工具类
- react-native 安卓真机测试出现闪退情况,报错no such file or directory
- DTD的作用
- 练习题5
- CentOS 7 安装. Net Core SDK 2.0