我想在Python中ping一系列IP地址并打印:
“IP可访问,包丢失率为X%”或
“IP无法访问,包丢失X%”
我想尝试的范围是192.168.0.X,X是0-255的范围
这是我目前为止的代码import shlex
import subprocess
import os
for x in range(0, 256):
cmd=shlex.split("ping -cl 192.168.0." + str(x))
try:
output = subprocess.check_output(cmd)
except subprocess.CalledProcessError,e:
#Will print the command failed with its exit status
print "The IP {0} isn't reachable".format(cmd[-1])
else:
print "The IP {0} is reachable".format(cmd[-1])
我的代码怎么了?我还注意到,当我尝试命令“ping-cl192.168.0.2”时,它显示-cl是一个仅限管理员使用的命令。我是我电脑上的管理员,我以管理员的身份运行cmd,这有什么问题吗?在
我正在使用Windows 8.1
Python 2.7.9版