最近项目需要抓包功能,并且抓包后要对数据包进行存库并分析。抓包想使用tcpdump来完成,但是tcpdump抓包之后只能保存为文件,我需要将其保存到数据库。想来想去shell脚本似乎不太好实现,于是用了比较热门的python来实现。不得不说,python丰富的第三方库确实是很强大,下面是具体的功能代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
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
|
上面的功能涉及到了文件操作,数据库操作,定时任务等几个功能点。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/pingnanlee/article/details/46831293