Search code examples
pythonshellsubprocesssniffing

python - Error when trying to receive output from check_output


My code is supposed to track down a packet sniffed with scapy and check which program sent/received the packet, put "Unknown" if the program isn't found

Code :

source_software = check_output(["netstat", "-nb"], shell = True).decode()
source_software = source_software[source_software.find(str(packet_dictionary["Port"])) : ]
source_software = source_software[source_software.find("\n") + 2 : source_software.find("]")]
if "Can not" not in source_software and len(source_software) > MINIMUM_LENGTH:
    packet_dictionary["Software"] = source_software
else:
    packet_dictionary["Software"] = "Unknown"

Errors :

File "Client.py", line 44, in add_to_list
source_software = check_output(["netstat", "-nb"], shell = True).decode()
File "C:\Python36\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Python36\lib\subprocess.py", line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['netstat', '-nb']' returned non-zero 
exit status 1.

Solution

  • it could be python doesn't have permission to run netstat or any else, but you can debug it with the following command

    source_software = check_output("netstat -nb;  exit 0", stderr=subprocess.STDOUT, shell=True).decode()
    print source_software