From 1298970849f205725467dfdd3999fc95bda0a1a7 Mon Sep 17 00:00:00 2001 From: recluse27 Date: Sat, 30 Oct 2021 23:54:38 -0700 Subject: [PATCH] traffic sniffer is added --- traffic_sniffer/traffic_sniffer.py | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 traffic_sniffer/traffic_sniffer.py diff --git a/traffic_sniffer/traffic_sniffer.py b/traffic_sniffer/traffic_sniffer.py new file mode 100644 index 0000000..3c0034f --- /dev/null +++ b/traffic_sniffer/traffic_sniffer.py @@ -0,0 +1,34 @@ +import pyshark +import argparse +from datetime import datetime + + +def sniff(interface, n_packet, filename): + capture = pyshark.LiveCapture(interface=interface) + capture.sniff(packet_count=n_packet) + + with open(filename + '.txt', 'a+') as file: + for n, pkt in enumerate(capture): + file.write("Packet #" + str(n + 1) + ':\n') + file.write(str(pkt) + '\n') + capture.close() + + +if __name__ == "__main__": + programDescription = ''' + Command Line Tool: python sniffer using pyshark; Need to run with `sudo` + ''' + + parser = argparse.ArgumentParser(description=programDescription) + parser.add_argument("--interface", "-i", help="interface") + parser.add_argument("--number", "-n", help="number of packets") + parser.add_argument("--filename", "-f", help="filename to save") + args = parser.parse_args() + + interface = args.interface if args.interface else 'en0' + number = abs(int(args.number)) if args.number else 50 + filename = args.filename if args.filename else "result" + + sniff(interface, number, filename) + + print('Sniffing is completed at %s' % datetime.now())