icmp sniffer with pcapy module

billiejoex billiejoex at fastwebnet.it
Fri Sep 9 11:17:28 EDT 2005


Hi all. The source below is a simple icmp sniffer made with pcapy.
To decode packets I'm using the EthDecoder() function that returns a 
rapresentation of the packet including ICMP type, ICMP code, IP source and 
IP destination.
All I need, now, is to get the ip src and ip dst only but I don't know how 
to do it.
I tried to use the IPDecoder instead of EthDecoder to decode packets but 
misteriously it doesn't works.
Does anybody have a good idea about how to get this values only?

Best regards

#!/usr/bin/python
### sniffer
import pcapy
from impacket.ImpactDecoder import *

def recv_pkts(hdr, data):
    x = EthDecoder().decode(data)
    print x

def get_int():
    devs = pcapy.findalldevs()
    i=0
    for eth in devs:
        print " %d - %s" %(i,devs[i])
        i+=1
    sel=input(" Select interface: ")
    dev=devs[sel]
    return dev

dev = get_int()
p = pcapy.open_live(dev, 1500, 0, 100)
p.setfilter('icmp')
print "Listening on eth: net=%s, mask=%s\n" % (p.getnet(), p.getmask())
p.loop(-1, recv_pkts) 





More information about the Python-list mailing list