from winpcapy import WinPcapDevices from winpcapy import WinPcapUtils import dpkt import time import datetime # list_device = WinPcapDevices.list_devices() # print(list_device) def packet_callback(win_pcap, param, header, pkt_data): eth = dpkt.ethernet.Ethernet(pkt_data) # # 判断是否为IP数据报 if not isinstance(eth.data, dpkt.ip.IP): print("Non IP packet type not supported ", eth.data.__class__.__name__) return # 抓IP数据包 packet = eth.data # 取出分片信息 df = bool(packet.off & dpkt.ip.IP_DF) mf = bool(packet.off & dpkt.ip.IP_MF) offset = packet.off & dpkt.ip.IP_OFFMASK # 输出数据包信息:time,src,dst,protocol,length,ttl,df,mf,offset,checksum output1 = {'time':time.strftime('%Y-%m-%d %H:%M:%S',(time.localtime()))} output2 = {'src':'%d.%d.%d.%d'%tuple(packet.src) , 'dst':'%d.%d.%d.%d'%tuple(packet.dst)} output3 = {'protocol':packet.p, 'len':packet.len, 'ttl':packet.ttl} output4 = {'df':df, 'mf':mf, 'offset':offset, 'checksum':packet.sum} print() print(output1) print(output2) print(output3) print(output4) #WinPcapUtils.capture_on(pattern="Realtek PCIe GBE Family Controller", callback=packet_callback) WinPcapUtils.capture_on(pattern="Intel(R) Dual Band Wireless-AC 3165", callback=packet_callback)
标签:python,IP,.%,packet,dpkt,import,print,winpcap From: https://www.cnblogs.com/xkdn/p/16885385.html