2

if a field is missed in a packet it prints the previous stored field. How to stop it so it prints only associated fields with a new packet and didn't print the previous packet field if a field is missed.

import pyshark
from collections import Counter


capture = pyshark.LiveCapture(interface='wlo2', bpf_filter='udp port 67')
capture.sniff_continuously(packet_count=15)


fields = {} 
already_seen_mac_ips = dict()
count = Counter()

for packet in capture:
    fields['mac'] = packet.dhcp.hw_mac_addr    
    try:
        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:
        pass    
    try:
        count.update([(fields['mac'], fields['ip'], fields['h_name'])])
        mac_ip = (fields['mac'], fields['ip'])
        if mac_ip not in already_seen_mac_ips:
            
            print(packet.sniff_time, count[(fields['mac'], fields['ip'], fields['h_name'])], fields['mac'], fields['ip'], fields['h_name'], fields['vendor'])
            already_seen_mac_ips.append(mac_ip)

    except KeyError: 
        print('key not found')

If a field is missed then it should wait for missing field and then print, it do not print the other stored field whenever a missing field received .


2021-12-08 16:15:25.404251 2 e0:13:b5:8f:xx:xx 192.168.1.15 vivo-1807 dhcpcd-8.1.0
2021-12-08 16:26:23.344012 1 90:e8:68:f2:00:xx 192.168.1.13 LAPTOP-VDT32QHA MSFT 5.0
2021-12-08 16:29:07.086268 1 60:8e:08:33:db:xx 192.168.1.13 Galaxy-J7-Nxt android-dhcp-9
2021-12-08 16:29:07.090200 2 60:8e:08:33:db:xx 192.168.1.3 Galaxy-J7-Nxt android-dhcp-9
2021-12-08 16:29:08.621666 4 60:8e:08:33:db:xx 192.168.1.16 Galaxy-J7-Nxt android-dhcp-9

roXx
  • 69
  • 9

0 Answers0