gpt4 book ai didi

python - 'haslayer' 函数在 scapy-python 中不起作用

转载 作者:行者123 更新时间:2023-11-28 17:47:47 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的代码来检测 scapy 中 ICMP 数据包的 IP 源地址,问题是 haslayer 函数看起来没有返回任何内容。

from scapy.all import *

while 1:

pkt = sniff ( iface="eth0" , count = 1 )
pkt.summary()

try:
if pkt.haslayer(IP):
ipsrc =pkt.getlayer(IP).src
print ipsrc

except:
pass

结果是

Ether / IP / ICMP 10.0.2.15 > 10.0.2.11 echo-request 0 / Raw
Ether / IP / ICMP 10.0.2.15 > 10.0.2.11 echo-request 0 / Raw
Ether / IP / ICMP 10.0.2.15 > 10.0.2.11 echo-request 0 / Raw

所以我无法捕获 ICMP 请求的 IP 源地址有什么想法吗?

最佳答案

您的通用 except 掩盖了您的代码可能遇到的任何错误。将 pass 更改为 raise 并删除任何特定错误。例如。我第一次遇到你的代码是:

socket.error: [Errno 1] Operation not permitted

root 身份运行后,我得到:

AttributeError: 'list' object has no attribute 'haslayer'

这让我将代码更改为有效的代码(以 root 身份运行):

from scapy.all import *

while 1:

pktl = sniff ( iface="eth0" , count = 1 )
pktl.summary()

for pkt in pktl:
try:
if pkt.haslayer(IP):
ipsrc =pkt.getlayer(IP).src
print ipsrc
except:
raise

所以你可能最好删除 try - 除了完全

关于python - 'haslayer' 函数在 scapy-python 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15555007/

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