I am completely new to scapy , I am trying to use it to create a DHCP monitor for my local network. I use sniffto capture packets that are sent to the callback through the parameter prn=. Inside the callback, I check if the package has a layer DHCP, and then I check the type of request.
I am 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]
if req_type == 3:
print ("Request from {}".format(pkt[Ether].src))
sniff(prn=manage, count=0, store=0)
The way I access the optionsDHCP layer is rather inconvenient, but this is the only one I came across. However, I believe that there should be a better, more pythonic way, for example, through dictor something else.
What is the appropriate way to access these options?