python执行shell获取结果_Python 执行系统的shell cmd并取结果

我们可以使用os.popen(cmd)来执行系统的命令并取到结果。

下面分别是Windows和Linux上的示例。

Windows:>>> import os

>>> print os.popen("ping www.qiansw.com -n 1").read()

正在 Ping dc3abcfc18d1eff8.cdn.fhldns.com [61.182.137.3] 具有 32 字节的数据:

来自 61.182.137.3 的回复: 字节=32 时间=14ms TTL=49

61.182.137.3 的 Ping 统计信息:

数据包: 已发送 = 1,已接收 = 1,丢失 = 0 (0% 丢失),

往返行程的估计时间(以毫秒为单位):

最短 = 14ms,最长 = 14ms,平均 = 14ms

Linux:shell > python

Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import os

>>> print os.popen("ping www.qiansw.com -c 1").read()

PING dc3abcfc18d1eff8.cdn.fhldns.com (61.155.149.104) 56(84) bytes of data.

64 bytes from 61.155.149.104: icmp_seq=1 ttl=50 time=32.1 ms

--- dc3abcfc18d1eff8.cdn.fhldns.com ping statistics ---

1 packets transmitted, 1 received, 0% packet loss, time 60ms

rtt min/avg/max/mdev = 32.176/32.176/32.176/0.000 ms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值