netstat与popen无法正常工作

时间:2015-05-04 18:47:10

标签: python popen netstat

使用Python运行此命令时

p = Popen(['netstat', '-atunp'], shell=True, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
print stdout

我得到了不同的输出。也就是说,如果我从命令行运行相同的命令,我可以看到IP地址和应用程序名称,而使用Python我不能。

我还需要做些什么来查看IP地址和应用名称吗?

1 个答案:

答案 0 :(得分:1)

I think I ran your command without the -p flag. This flag expects a protocol. This is probably why your stdout string is empty; stderr on the other hand would have the error -

In [86]: stderr
Out[86]: 'netstat: option requires an argument -- p\nUsage:\tnetstat [-AaLlnW] [-f address_family | -p protocol]\n\tnetstat [-gilns] [-f address_family]\n\tnetstat -i | -I interface [-w wait] [-abdgRt]\n\tnetstat -s [-s] [-f address_family | -p protocol] [-w wait]\n\tnetstat -i | -I interface -s [-f address_family | -p protocol]\n\tnetstat -m [-m]\n\tnetstat -r [-Aaln] [-f address_family]\n\tnetstat -rs [-s]\n\n'

So update your command like this (removing shell=True and adding a protocol as an argument) -

In [87]: paste
p = Popen(['netstat', '-atunp', 'tcp'], stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
print stdout

## -- End pasted text --
Active Internet connections (including servers)
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)    
tcp4       0      0  192.168.1.100.53076    198.252.206.140.80     ESTABLISHED
tcp4       0      0  192.168.1.100.53075    198.252.206.140.80     ESTABLISHED
tcp4       0      0  192.168.1.100.53004    216.58.220.3.443       ESTABLISHED
tcp4       0      0  192.168.1.100.52984    74.125.200.155.443     ESTABLISHED
tcp4       0      0  192.168.1.100.52954    198.252.206.149.443    ESTABLISHED
tcp4       0      0  192.168.1.100.52927    91.189.89.222.443      FIN_WAIT_1 
tcp4       0      0  192.168.1.100.52925    173.194.120.144.443    ESTABLISHED
tcp4       0      0  192.168.1.100.52869    74.125.130.155.443     ESTABLISHED
tcp4       0      0  192.168.1.100.52863    74.125.68.139.443      ESTABLISHED
tcp4       0      0  192.168.1.100.52858    74.125.130.102.443     ESTABLISHED
tcp4       0      0  192.168.1.100.52851    74.125.68.93.443       ESTABLISHED
tcp4       0      0  192.168.1.100.52847    74.125.200.132.443     ESTABLISHED
tcp4       0      0  192.168.1.100.52814    216.58.216.163.443     ESTABLISHED
tcp4       0      0  192.168.1.100.52812    74.125.68.189.443      ESTABLISHED
tcp4       0      0  192.168.1.100.52794    74.125.130.17.443      ESTABLISHED
tcp4       0      0  192.168.1.100.52793    198.252.206.149.443    ESTABLISHED
tcp4       0      0  192.168.1.100.52761    74.125.68.138.443      ESTABLISHED
tcp4       0      0  192.168.1.100.52737    74.125.68.132.443      ESTABLISHED
tcp4       0      0  192.168.1.100.52577    17.172.232.205.443     ESTABLISHED
tcp4       0      0  192.168.1.100.52571    74.125.130.188.443     ESTABLISHED
tcp6       0      0  fd9a:f3a0:3346:7.4488  *.*                    LISTEN     
tcp4       0      0  *.*                    *.*                    CLOSED     
tcp4       0      0  127.0.0.1.631          *.*                    LISTEN     
tcp6       0      0  ::1.631                *.*                    LISTEN