I would like to run tcpdump from python for x amount of minutes specified from the user. At the moment my function looks like this:
def tcpdump():
run_time = int(input("\nHow many minutes would you like the bash to run?\n"))
time_to_end = time.time() + 60 * run_time
print("\ntcpdump running...\n")
while time.time() < time_to_end:
p = subprocess.Popen("./tcpdump.bash", shell=True)
p.terminate()
However this seems to repeatedly launch tcpdump as I get the following output:
tcpdump: (all BPF devices are busy)
tcpdump: (all BPF devices are busy)
tcpdump: (all BPF devices are busy)
I am not sure how to solve this, and additionally I am not sure how to kill the process, as p.kill() doesnt seem to work.
----------------- EDITED ---------------------
I have now tried the following, however I am not sure if my subprocess gets killed correctly or if it will run infinitely at the background:
def tcpdump:
run_time = int(input("\nHow many minutes would you like to collect benign data?\n"))
time_to_end = time.time() + 60 * run_time
print("\ntcpdump running...\n")
p = subprocess.Popen(['tcpdump', '-i', 'en0', '-w', 'test.pcap']))
while time.time() < time_to_end:
p.communicate()
p.kill()