I'm totally new to scapy, I'm trying to use it to build a DHCP monitor for my LAN. I'm using sniff
to capture packets that are sent to a callback via the prn=
parameter. Inside the callback, I check if the packet has the DHCP
layer and then I check the type of request.
I'm currently doing it like this:
def manage(pkt):
if pkt.haslayer(DHCP):
req_type = [x[1] for x in pkt[DHCP].options if x[0] == 'message-type'][0]
# Message type: request
if req_type == 3:
print ("Request from {}".format(pkt[Ether].src))
sniff(prn=manage, count=0, store=0)
The way I'm accessing the options
in the DHCP layer is kind of awkward, but it's the only one I've come up with that works. However I believe there must be a better, more pythonic way, like via a dict
or something.
What's the appropriate way of accessing those options?