Scapy: check the message type of the captured DHCP packet

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]

        # Message type: request
        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?

+4
1

, , . next() , , , :

req_type = next(opt[1] for opt in pkt[DHCP].options if isinstance(opt, tuple) and opt[0] == 'message-type')

, opt, .

dict(), .

DHCP, Scapy, , , , PacketListField .

+1

Source: https://habr.com/ru/post/1690242/


All Articles