def run_live_capture(wireless_interface, capture_file, sample_seconds): pcap_dev = pcapy.create(wireless_interface) pcap_dev.set_snaplen(65535) pcap_dev.set_timeout(0) pcap_dev.set_promisc(True) pcap_dev.set_buffer_size(15 * 1024 * 1024) procedure_logger.info("Opening capture file: {0}".format(capture_file)) procedure_logger.info("Arming and activating live capture...") pcap_dev.activate() timer_fd = timerfd.create(timerfd.CLOCK_MONOTONIC, 0) try: timerfd.settime(timer_fd, 0, sample_seconds, 0) start_time = time.time() dumper = pcap_dev.dump_open(capture_file) hdr, data = pcap_dev.next() while hdr and not select.select([timer_fd], [], [], 0)[0]: dumper.dump(hdr, data) hdr, data = pcap_dev.next() dumper.close() pcap_dev.close() end_time = time.time() return start_time, end_time, sample_seconds finally: os.close(timer_fd)
def __init__(self, device, snaplen, promisc, to_ms, monitor=False): # noqa: E501 if monitor: try: self.pcap = pcap.create(device) self.pcap.set_snaplen(snaplen) self.pcap.set_promisc(promisc) self.pcap.set_timeout(to_ms) if self.pcap.set_rfmon(1) != 0: warning("Could not set monitor mode") if self.pcap.activate() != 0: raise OSError("Could not activate the pcap handler") # noqa: E501 except AttributeError: raise OSError("Your pcapy version does not support" "monitor mode ! Use pcapy 0.11.4+") else: self.pcap = pcap.open_live(device, snaplen, promisc, to_ms) # noqa: E501
import pcapy import dpkt def pktHandler(header, payload): pkt = dpkt.ieee80211.IEEE80211(payload) # print(pkt.__sizeof__()) print(pkt) if __name__ == '__main__': devList = pcapy.findalldevs() # print(devList) dev = 'wlan0mon' cap = pcapy.open_live(dev, 65536, True, 0) readerDev = pcapy.create(dev) cap.loop(10000, pktHandler) # cap.dispatch(10000, pktHandler)
import codecs import logging import os import pcapy import struct DEFAULT_TIMEOUT = 3 BPF_START = 0 logger = logging.getLogger(__name__) frame_cache = {} pc = pcapy.create("lo0") pc.set_snaplen(1504) pc.set_promisc(False) pc.set_timeout(1) # in millisecods pc.set_buffer_size(32 * 1024 * 1024) pc.activate() def send_frame(cine, count, dest, ssrc): global frame_cache script_path = os.path.dirname(__file__) raw_path = os.path.join(script_path, "./takes/{}.raw".format(cine)) try: frame_bytes = frame_cache[raw_path] except KeyError: frame_bytes = open(raw_path, "rb").read() frame_cache[raw_path] = frame_bytes
#!/usr/bin/env python3 """ List Interaces Command line tool that lists the interfaces from which pcapy is able to capture packets """ import pcapy interfaces = pcapy.findalldevs() for interface in interfaces: temp_reader = pcapy.create(interface) print(interface + " " + str(temp_reader.getnet()) + " " + str(temp_reader.getmask())) temp_reader.close()