def tcpdump(): parser = argparse.ArgumentParser(description='tcpdump') #parser.add_argument('filter', type=str, help="Specifies filter") parser.add_argument('-i', metavar='interface', dest='interface', required=True, type=str, help="Specifies the interface listen on") args = parser.parse_args() try: index = int(args.interface) except ValueError: device = args.interface else: dev_names = [] alldevs = pcap.pcap_findalldevs() dev = alldevs while True: if not dev: break dev_names.append(dev.contents.name.decode()) dev = dev.contents.next device = dev_names[index] pcap.pcap_freealldevs(alldevs) hpcap = pcap.pcap_open_live(device, 65535, False, 0) pf = pcap.pcap_compile(hpcap, 'icmp', False, 0) #pcap.pcap_setfilter(hpcap, pf) # #pcap.pcap_freecode(pf) pcap.pcap_loop(hpcap, -1, callback, None)
def test_pcap_loop(self): self.dump_data_index = 0 pcap.pcap_loop(self.hpcap, len(dump_data), TestPcapReadOffline.callback, self)
def capture(): hpcap = pcap.pcap_open_live(device, 65535, True, 0) pcap.pcap_loop(hpcap, -1, callback, None) pcap.pcap_close(hpcap)