1

The problem in code is if a field isn't received in the packet, then it goes further and count that as 1 and when a missed field is received in the next packet then it updates as count 2 with other fields. So I want if a field is missed then it doesn't update count until all the fields I got. When I get all the fields then it updates it as 1 and so on else waits

import pyshark
from collections import Counter

# creating a live capture
capture = pyshark.LiveCapture(interface='eno1', bpf_filter='udp port 67')
# sniffing the captured packets
capture.sniff_continuously(packet_count=15)


fields = {}  # empty dictionary
already_seen_mac_ips = set()  # set of mac ip tuples
# incoming_ip = set()
count = Counter()

for packet in capture:  # running for loop on captured packets
    fields['mac'] = packet.dhcp.hw_mac_addr     # add mac key in empty dictionary
    try:    # using try statement to check whether any field is throwing error
        fields['vendor'] = packet.dhcp.option_vendor_class_id
        fields['h_name'] = packet.dhcp.option_hostname
        fields['ip'] = packet.dhcp.option_requested_ip_address
        fields['sub_mask'] = packet.dhcp.option_subnet_mask
        fields['server_ip'] = packet.dhcp.option_dhcp_server_id
        fields['domain_name'] = packet.option.dhcp.option_domain_name
        fields['dns'] = packet.dhcp.option_domain_name_server

    except AttributeError:  # if attribute error comes it will except
        pass    # do nothing

    # if fields['ip'] not in incoming_ip:
       # incoming_ip.add(fields['ip'])

    try:
        count.update([(fields['mac'], fields['h_name'], fields['vendor'])])
        mac_ip = (fields['mac'], fields['ip'])
        if mac_ip not in already_seen_mac_ips:
            print(packet.sniff_time, count[(fields['mac'], fields['h_name'], fields['vendor'])], fields['mac'], fields['ip'], fields['h_name'], fields['vendor'])# , fields['sub_mask'], fields['server_ip'], fields['domain_name'], fields.get('dns'))
            # already_seen_mac_ips.add(mac_ip)
    except KeyError:    # excepting Keyerror if raised
        print('key not found')  
roXx
  • 69
  • 9

0 Answers0