- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python实现linux下抓包并存库功能由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
最近项目需要抓包功能,并且抓包后要对数据包进行存库并分析。抓包想使用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 。
最后此篇关于python实现linux下抓包并存库功能的文章就讲到这里了,如果你想了解更多关于python实现linux下抓包并存库功能的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
有没有办法在 Vim 中打开一个文本文件,并在同一个窗口中打开它旁边的 shell? 我发现自己打开脚本进行编辑、更改、关闭 Vim、运行脚本进行测试,然后再次打开 Vim,一遍又一遍。我想我可以有两
使用 pip 或 pip3 安装 pytest 将为 Python2 和 pytest 安装 pytest > 分别适用于Python3。但两者都需要通过输入相同的命令pytest来调用......这
我在使 rem 中的 min-width 与百分比宽度共存时遇到了一些问题。 When resizing this jsfiddle the outer right block is going un
对于在 Windows 7 上从 Java 6 迁移到 Java 7 运行时的迁移项目,我们希望获得以下情况: 机器上同时安装了 JRE 6 和 JRE 7 JRE 6 设置为默认值 JRE 7 将由
我是一名优秀的程序员,十分优秀!