I'm trying to write a simple code that detect IP source address for ICMP packet in scapy
, the problem is haslayer
function looks not returning anything.
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
the result is
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
So I'm not able to catch the IP source address for the ICMP request Any idea ?
Your generic except
masks any errors that your code might encounter. Change pass
to raise
and remove any specific errors. E.g. the first I encountered with your code was:
socket.error: [Errno 1] Operation not permitted
and after running as root
I got:
AttributeError: 'list' object has no attribute 'haslayer'
and that made me change the code to something that worked (running as 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
So you probably better of removing the try - except altogether