gpt4 book ai didi

python - 运行 Scapy Sniff 函数时出错

转载 作者:太空宇宙 更新时间:2023-11-03 18:24:47 27 4
gpt4 key购买 nike

我编写了这个程序来嗅探网络中的 icmp 数据包并打印源地址。代码如下:

from scapy.all import *

def fun_callback(pkt):
print str(pkt.payload.src)
sniff(prn = fun_callback, filter = 'icmp', timeout =5)

运行该程序后,我收到此错误。

[root@localhost icmp]# python test.py
WARNING: Failed to execute tcpdump. Check it is installed and in the PATH
WARNING: No route found for IPv6 destination :: (no default route?)
192.168.134.131
192.168.134.131
192.168.134.2
192.168.134.2
fe80::20c:29ff:fee4:a130
134.160.38.1
192.168.134.131
Traceback (most recent call last):
File "test.py", line 5, in <module>
sniff(prn = fun_callback, filter = 'icmp', timeout =5)
File "/usr/lib/python2.7/site-packages/scapy/sendrecv.py", line 586, in sniff
r = prn(p)
File "test.py", line 4, in fun_callback
print str(pkt.payload.src)
File "/usr/lib/python2.7/site-packages/scapy/packet.py", line 176, in __getattr__
fld,v = self.getfield_and_val(attr)
File "/usr/lib/python2.7/site-packages/scapy/packet.py", line 172, in getfield_and_val
return self.payload.getfield_and_val(attr)
File "/usr/lib/python2.7/site-packages/scapy/packet.py", line 172, in getfield_and_val
return self.payload.getfield_and_val(attr)
File "/usr/lib/python2.7/site-packages/scapy/packet.py", line 1057, in getfield_and_val
raise AttributeError(attr)
AttributeError: src
[root@localhost icmp]#

为什么会出现这个异常?

最佳答案

您嗅探了一个带有不带 src 属性的有效负载的数据包。如果您想快速修复代码,请编写:

def fun_callback(pkt):
if hasattr(pkt.payload, "src"):
print str(pkt.payload.src)

问题是,您并不真正知道 pkt.payload 是什么。如果您想要更好的解决方案,请尝试以下操作:

def fun_callback(pkt):
if IP in pkt: print pkt[IP].src
elif IPv6 in pkt: print pkt[IPv6].src

或者更好,使用.sprintf():

sniff(prn=lambda pkt: pkt.sprintf("{IP:%IP.src%}{IPv6:%IPv6.src%}"),
filter='icmp', timeout=5))

关于python - 运行 Scapy Sniff 函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23402924/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com