-2

I am trying to execute commands in python, but it is not working. The command syntax work fine in command prompt but not in python.

The code I used is import os os.system('"c:\\Program Files\\Wireshark\\tshark.exe" -i Ethernet -i Wi-Fi -w cap.pcapng tshark -nq -z endpoints,tcp -z endpoints,udp | gawk "{ print $3,$4,$5,$6 }"') | grep [0-9]

2 Answers2

1

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.
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
-1

Try escaping the double quotes

'\"c:\\Program Files\\Wireshark\\tshark.exe\" ...

Seazor
  • 1
  • 2
  • Why would you need to do that? – DarkKnight Oct 07 '22 at 14:25
  • The message tells you that he try to execute c:\program with the 1st argument after the blank. You have to tell the shell that all inside the double quote is the path to executable. – Seazor Oct 07 '22 at 14:33
  • Seems that there is better solution : https://stackoverflow.com/questions/35817/how-to-escape-os-system-calls – Seazor Oct 07 '22 at 14:55
  • There must be nothing __tried__? [os.system](https://docs.python.org/3/library/os.html#os.system) is deprecated since many years and should not be used anymore in newly coded Python scripts as there is the [subprocess module](https://docs.python.org/3/library/subprocess.html) which is on Windows a Python wrapper module for the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw). – Mofi Oct 07 '22 at 15:00
  • `os.system` just calls `CreateProcess` to run `%ComSpec% /c` with the string passed to `os.system` which means there is executed on Windows `C:\Windows\System32\cmd.exe /c` and for that reason the command line to execute must fulfill the requirements of two scripts interpreters: *Python* and *CMD*. How the Windows Command Processor `cmd.exe` interprets the arguments after option `/C` (run command line and close) or option `/K` (run command line and keep running) is explained by the `cmd` usage help output on running `cmd /?` in a command prompt window. So reading the docs would help a lot. – Mofi Oct 07 '22 at 15:04
  • `cmd.exe` like `explorer.exe` and `cscript.exe` and `wscript.exe` and `powershell.exe` and `java.exe` and `perl.exe` and ... calls `CreateProcess` with a filled out [STARTUPINFO](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow) structure to run an executable. So there is never the need to run an executable from within a Python script using `os.system` and therefore using `cmd.exe` as it is possible to do the same as `cmd.exe` with using the `subprocess` module. – Mofi Oct 07 '22 at 15:06