简体   繁体   English

从txt文件中提取tcpdump时间戳

[英]Extract tcpdump timestamp from txt file

I'm trying to calculate the throughput from the output of tcpdump using python. 我正在尝试使用python从tcpdump的输出中计算吞吐量。 So far I called tcpdump from python and managed to write the output in a text file. 到目前为止,我从python调用了tcpdump并设法将输出写入文本文件中。 Sample output: 样本输出:

01:06:23.649464 0us IP (tos 0x0, ttl 128, id 63533, offset 0, flags [none], proto UDP (17), length 72) 251.146.199.137.1066 > 156.96.135.220.62827: UDP, length 44 01:06:23.649464 0us IP(tos 0x0,ttl 128,id 63533,偏移0,标志[无],原始UDP(17),长度72)251.146.199.137.1066> 156.96.135.220.62827:UDP,长度44

01:06:23.920316 0us IP (tos 0x0, ttl 1, id 10354, offset 0, flags [none], proto IGMP (2), length 32, options (RA)) 251.146.198.120 > fm-dyn-140-0-193-221.fast.net.id: [|igmp] 01:06:23.920316 0us IP(tos 0x0,ttl 1,id 10354,偏移量0,标志[无],原型IGMP(2),长度32,选项(RA))251.146.198.120> fm-dyn-140-0 -193-221.fast.net.id:[|igmp]

However, I'm stuck on the next part. 但是,我停留在下一部分。 Extracting the time and length (the first one) and calculating the throughput. 提取时间和长度(第一个)并计算吞吐量。 I'm new to python and don't have clear idea about regular expression. 我是python的新手,对正则表达式没有明确的想法。 Also since the timestamps include micro second is there any easy method to work with them to calculate throughput? 另外,由于时间戳包括微秒,是否有任何简便的方法可以使用它们来计算吞吐量?

Thanks in advance. 提前致谢。

Forget about regex, you can use datetime module. 忘记正则表达式,可以使用datetime模块。

Using datetime 使用datetime

>>> from datetime import datetime
>>> lines = ['01:06:23.649464 0us IP (tos 0x0, ttl 128, id 63533, offset 0, flags [none], proto UDP (17), length 72) 251.146.199.137.1066 > 156.96.135.220.62827: UDP, length 44', '01:06:23.920316 0us IP (tos 0x0, ttl 1, id 10354, offset 0, flags [none], proto IGMP (2), length 32, options (RA)) 251.146.198.120 > fm-dyn-140-0-193-221.fast.net.id: [|igmp]']
>>> times = [datetime.strptime(line[:15], '%H:%M:%S.%f') for line in lines]

The throughput could be calculated directly, but you'll need to use strptime from datetime to that. 吞吐量可以直接计算,但是您需要使用从datetimedatetime时间的strptime

>>> times[1] - times[0]
datetime.timedelta(0, 0, 270852)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM