This commit is contained in:
Vitalii 2024-06-14 09:30:09 +00:00 committed by GitHub
commit 9d4d3b8d11
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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())