2017-08-05 86 views
0
接收输出时

我的代码应该追查的数据包,Scapy的闻了闻,并检查该程序发送/接收数据包,把“未知”如果程序没有找到蟒蛇 - 错误试图从check_output

代码:

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" 

错误:

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. 

回答

0

它可能是蟒蛇没有权限运行netstat或其他任何,但你可以用下面的命令

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

我忘了调试它,我在几个星期前编码这段代码和今天又回来了,忘了我必须以管理员身份运行它 – NeXoR

0

正如你所看到的在例外的底线,问题是:

Command '['netstat', '-nb']' returned non-zero exit status 1. 

...这意味着它是不是check_output本身,而它仅仅是汇报,你尝试的命令来使用它运行,失败并退出代码1.

您可能希望在shell中运行该命令并检查它是否按预期工作。