热线电话:13121318867

登录
2019-01-18 阅读量: 1384
读取os.popen()输出啥都不返回!

我正在开发一个python脚本来收集有关Linux系统的几条信息,现在我试图收集一个使用的监听UDP e TCP端口的列表,这os.popen()是该函数的样子:

def ports(self):

# Gets a few lines of information about open TCP ports

tcpOpenPorts = os.popen("netstat -tulpn | grep -P 'tcp\b'").read()

print(tcpOpenPorts)

# Gets a few lines of information about open UDP ports

udpOpenPorts = os.popen("netstat -tulpn | grep -P 'tcp\b'").read()

print(udpOpenPorts)

我面临的问题是:当我使用上面两个变量的函数执行脚本tcpOpenPorts并udpOpenPorts返回空时,即使shell命令:

netstat -tulpn | grep -P 'tcp\b'

工作正常。

这是命令的示例输出:

tcp 0 0 127.0.0.1:63342 0.0.0.0:* OUÇA 3244/java

tcp 0 0 0.0.0.0:111 0.0.0.0:* OUÇA 539/rpcbind

tcp 0 0 0.0.0.0:22 0.0.0.0:* OUÇA 686/sshd

tcp 0 0 127.0.0.1:631 0.0.0.0:* OUÇA 4466/cupsd

tcp 0 0 127.0.0.1:6942 0.0.0.0:* OUÇA 3244/java

我使用该os模块的方式有什么问题吗?

132.3500
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子