this full solution should work
import subprocess
p = subprocess.Popen([r'c:\Program Files\Wireshark\tshark.exe',
'-i','Ethernet','-i','Wi-Fi',
'-w','cap.pcapng','tshark','-nq',
'-z','endpoints,tcp','-z','endpoints,udp'],
stdout=subprocess.PIPE)
for line in p.stdout:
toks = line.decode().split() # get fields of each line like awk
s = "{},{},{},{}".format(toks[2],toks[3],toks[4],toks[5]) # format the string
if any(d in s for d in "0123456789"): # look for digits
print(s)
p.wait()
- it handles arguments containing space using
subprocess.Popen
and a list of arguments instead of a string
- it handles backslashes by using the r (raw) prefix
- it gets rid of awk and grep that aren't native in windows, so only python (and wireshark) is required to make the script run.