import subprocess
p = subprocess.Popen('ping 127.0.0.1')
# Linux Version p = subprocess.Popen(['ping','127.0.0.1','-c','1',"-W","2"])
# The -c means that the ping will stop afer 1 package is replied
# and the -W 2 is the timelimit
p.wait()
print p.poll()
如果p.poll()为0,则ping成功;如果为1,则无法到达目标。
许多IP地址的版本为:
import subprocess
iplist=["127.0.0.1","8.8.8.8"]
for ip in iplist:
p = subprocess.Popen('ping '+ip,stdout=subprocess.PIPE)
# the stdout=subprocess.PIPE will hide the output of the ping command
p.wait()
if p.poll():
print ip+" is down"
else:
print ip+" is up"
# You end with a log of all the ip addresses
对于windows:
在Linux上使用python,我将使用check_output()
如果ping成功,则返回true
我知道这在Linux上有效,我认为它也可以在Windows上工作。
更新:未注释的代码也可以在Windows中工作
如果p.poll()为0,则ping成功;如果为1,则无法到达目标。
许多IP地址的版本为:
相关问题 更多 >
编程相关推荐