Scapy 很慢,因为它是纯 python 解析用户空间中的整个数据包......绕过 scapy 的吞吐量限制并不是什么不寻常的事情。
做一个苹果对苹果的比较......我有一个 Xeon 服务器,它有一个直接连接到互联网的千兆以太网管道,但我的流量非常少。当我对它所连接的 Cisco 路由器运行正常 ping 时,我平均每个大约 60 微秒......
[mpenning@Bucksnort ~]$ ping -W 1 -c 3 192.0.2.1
PING 192.0.2.1 (192.0.2.6) 56(84) bytes of data.
64 bytes from 192.0.2.1: icmp_req=1 ttl=64 time=0.078 ms
64 bytes from 192.0.2.1: icmp_req=2 ttl=64 time=0.062 ms
64 bytes from 192.0.2.1: icmp_req=3 ttl=64 time=0.062 ms
--- 192.0.2.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.062/0.067/0.078/0.010 ms
[mpenning@Bucksnort ~]$
scapy 中的相同目的地......也以毫秒为单位......
[mpenning@Bucksnort ~]$ sudo python new_ping_ip.py
Ping: 0.285587072372
Ping: 0.230889797211
Ping: 0.219928979874
AVERAGE 245.468616486
[mpenning@Bucksnort ~]$
Scapy 的结果几乎是 bash 提示符 (245.469 / 0.062) 的基线 ping 的4000倍...我自己运行电缆,它不到 Cisco 路由器的 10 英尺电缆。
你能做些什么来获得更好的结果?正如评论中提到的,在解析之前先查看sent_time
并填充time
...Packet.time
这仍然比来自 shell 的 ping 慢,但可能有助于您在 scapy 中捕获数据包。
#! /usr/bin/env python
from scapy.all import *
def QoS_ping(host, count=3):
packet = Ether()/IP(dst=host)/ICMP()
t=0.0
for x in range(count):
ans,unans=srp(packet,iface="eth0", filter='icmp', verbose=0)
rx = ans[0][1]
tx = ans[0][0]
delta = rx.time-tx.sent_time
print "Ping:", delta
t+=delta
return (t/count)*1000
if __name__=="__main__":
total = QoS_ping('192.0.2.1')
print "TOTAL", total
样品运行...
[mpenning@Bucksnort ~]$ sudo python ping_ip.py
Ping: 0.000389099121094
Ping: 0.000531911849976
Ping: 0.000631093978882
TOTAL 0.51736831665
[mpenning@Bucksnort ~]$
即使使用Packet.time
andPacket.sent_time
与 shell 调用相比也很慢......
>>> from subprocess import Popen, PIPE
>>> import re
>>> cmd = Popen('ping -q -c 3 192.0.2.1'.split(' '), stdout=PIPE)
>>> output = cmd.communicate()[0]
>>> match = re.search('(\d+\.\d+)\/(\d+\.\d+)\/(\d+\.\d+)\/(\d+\.\d+)\s+ms', output)
>>> if not (match is None):
... print "Average %0.3f" % float(match.group(1))
... else:
... print "Failure"
...
Average 0.073
>>>
ping -q -c 3
提供 3 个 ping 的摘要输出,而没有打印单个 ping。
如果要捕获 ping 数据包(通过 shell ping 调用)以供以后scapy
处理,tcpdump -c <num-packets> -w <filename> icmp and host <host-addr> &
请在运行 CLI ping 之前生成...然后使用 scapyrdpcap()
从tcpdump
. 请务必正确计算您将在 pcap 文件中捕获的数据包数量。