python實(shí)現(xiàn)linux下抓包并存庫(kù)功能
最近項(xiàng)目需要抓包功能,并且抓包后要對(duì)數(shù)據(jù)包進(jìn)行存庫(kù)并分析。抓包想使用tcpdump來(lái)完成,但是tcpdump抓包之后只能保存為文件,我需要將其保存到數(shù)據(jù)庫(kù)。想來(lái)想去shell腳本似乎不太好實(shí)現(xiàn),于是用了比較熱門的python來(lái)實(shí)現(xiàn)。不得不說(shuō),python豐富的第三方庫(kù)確實(shí)是很強(qiáng)大,下面是具體的功能代碼。
from apscheduler.scheduler import Scheduler import os import sys import time import MySQLdb import ConfigParser import Logger def main(): logger = Logger.Logger(logname='flowstat.log', loglevel=1, logger='flowstat').getlog() try: cf = ConfigParser.ConfigParser() cf.read('./flowstat.conf') filterNet1 = cf.get('packet', 'filterNet1') filterNet2 = cf.get('packet', 'filterNet2') packetFile = cf.get('packet', 'packetFile') db_host = cf.get('db', 'host') db_user = cf.get('db', 'user') db_passwd = cf.get('db', 'passwd') db_dbname = cf.get('db', 'dbname') conn = MySQLdb.connect(host=db_host, user=db_user, passwd=db_passwd, db=db_dbname, port=3306) os.system('nohup ./capturePacket.sh ' + filterNet1 + ' ' + filterNet2 + ' ' + packetFile + ' &') except Exception, e: logger.error(e) sys.exit(1) sched = Scheduler(daemonic = False) @sched.cron_schedule(day_of_week='0-4', hour='*', minute='0-59', second='*/60') def packagestat_job(): logger.debug('stat package' + ' ' + time.strftime("%Y-%m-%d %H:%M:%S")) try: fos = open(packetFile, 'r+') lines = fos.readlines() values = [] for line in lines: arr = line.split(',') if len(arr) > 4: values.append((arr[0].strip(), arr[2].strip(), arr[3].strip(), arr[4].strip())) if len(values) > 0: cur = conn.cursor() cur.executemany('insert into tbpk_packet(TimesMacs, LengthIps, Seq, Ack) values(%s,%s,%s,%s)', values) conn.commit() cur.close() fos.truncate(0) fos.close() except Exception, e3: Logger.error(e3) sched.start() while 1: time.sleep(60) conn.close() if __name__ == '__main__': main() shell腳本 #!/bin/sh tcpdump -i eth0 -l >> *.txt
上面的功能涉及到了文件操作,數(shù)據(jù)庫(kù)操作,定時(shí)任務(wù)等幾個(gè)功能點(diǎn)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用Python實(shí)現(xiàn)windows下的抓包與解析
- python 抓包保存為pcap文件并解析的實(shí)例
- Python如何爬取微信公眾號(hào)文章和評(píng)論(基于 Fiddler 抓包分析)
- python調(diào)用tcpdump抓包過(guò)濾的方法
- 使用Python解析JSON數(shù)據(jù)的基本方法
- python解析json實(shí)例方法
- Python解析json時(shí)提示“string indices must be integers”問(wèn)題解決方法
- 深入理解Python對(duì)Json的解析
- 深入解析Python編程中JSON模塊的使用
- Python抓包并解析json爬蟲(chóng)的完整實(shí)例代碼
相關(guān)文章
python中的?sorted()函數(shù)和sort()方法區(qū)別
這篇文章主要介紹了python中的?sorted()函數(shù)和sort()方法,首先看sort()方法,sort方法只能對(duì)列表進(jìn)行操作,而sorted可用于所有的可迭代對(duì)象。具體內(nèi)容需要的小伙伴可以參考下面章節(jié)2022-02-02用TensorFlow實(shí)現(xiàn)lasso回歸和嶺回歸算法的示例
本篇文章主要介紹了用TensorFlow實(shí)現(xiàn)lasso回歸和嶺回歸算法的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05python pexpect ssh 遠(yuǎn)程登錄服務(wù)器的方法
今天小編就為大家分享一篇python pexpect ssh 遠(yuǎn)程登錄服務(wù)器的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Python實(shí)現(xiàn)根據(jù)指定端口探測(cè)服務(wù)器/模塊部署的方法
這篇文章主要介紹了Python根據(jù)指定端口探測(cè)服務(wù)器/模塊部署的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08樹(shù)莓派用python中的OpenCV輸出USB攝像頭畫(huà)面
這篇文章主要為大家詳細(xì)介紹了樹(shù)莓派用python中的OpenCV輸出USB攝像頭畫(huà)面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06python判斷一個(gè)數(shù)是否能被另一個(gè)整數(shù)整除的實(shí)例
今天小編就為大家分享一篇python判斷一個(gè)數(shù)是否能被另一個(gè)整數(shù)整除的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12