python - scapy (python) 中的跟踪器

我尝试在 Scapy 中进行 tracert 操作,但它对我不起作用,它写给我打印错误(NoneType),有人可以帮我解决问题吗?

from scapy.all import *
from scapy.layers.inet import IP, ICMP, Ether, UDP, traceroute
TTL = 28
packet = IP(dst = '8.8.8.8') / ICMP(type = 0)
for i in range(TTL):
    packet[IP].ttl = i + 1
    ans = sr1(packet, timeout = 2, verbose = 0)
    print(ans[IP].src)

回答1

您不能假设您发送的每个 ICMP 回显请求都会得到答复。如果您没有收到答案,则 sr1 会在 2 秒延迟后返回 None。这解释了错误消息,因为 ans[IP].src 不正确,因为 ansNone。因此,您必须在代码中处理这种情况:

from scapy.all import IP, ICMP, sr1
TTL = 28
packet = IP(dst = '8.8.8.8') / ICMP(type = 0)
for i in range(TTL):
    packet[IP].ttl = i + 1
    ans = sr1(packet, timeout = 2, verbose = 0)
    if ans is None:
        print('no response...')
    else:
        print(ans[IP].src)

相似文章

python - 使用 scapy 解码 https 流量

我正在尝试使用scapy捕获https流量。到目前为止,我可以看到TLS握手,但我无法使用NSS密钥文件解码应用程序数据。我正在使用scapy2.5.0rc2.dev39。我的脚本的结构是:(1)将c...

随机推荐

最新文章