Python: 如何 ping 一个 IP 地址范围?
Python: How do I ping a range of IP addresses?
我想 ping Python 中的 运行 个 IP 地址并打印:
"The IP is reachable with a X% percent package loss" 或
"The IP isn't reachable with a X% package loss"
我想尝试的 运行ge 是 192.168。0.X 其中 X 是 0-255
的 运行ge
到目前为止,这是我的代码;
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 -cl 192.168.0.2" 时,它说 -cl 是一个仅限管理员的命令。我是计算机的管理员,我 运行 以管理员身份执行命令,这有什么问题吗?
我正在使用 Windows 8.1
Python v2.7.9
如果您正在使用 linux 并且只想查看丢包情况:
import shlex
from subprocess import PIPE, Popen
cmd1 = shlex.split("grep -oP '\d+(?=% packet loss)'")
for x in range(1, 256):
cmd = "ping -c 4 192.168.43.{}".format(x).split()
p1 = Popen(cmd, stdout=PIPE, stderr=PIPE)
p2 = Popen(cmd1, stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()
output = p2.communicate()[0].rstrip()
if output == "100":
print("{}% percent packet loss from unreachable ip {}".format(output, cmd[-1]))
else:
print("{}% percent packet loss from reachable ip {}".format(output, cmd[-1]))
免责声明我不使用 windows 所以愿意接受更正,但这可能有效:
for x in range(1, 256):
cmd = "ping -c 1 192.168.43.{}".format(x).split()
p1 = Popen(cmd, stdout=PIPE, stderr=PIPE)
lines = p1.communicate()[0].splitlines()
output = (lines[-2]).rsplit(None,5)
output = output[-5].rstrip()
if output == "100%":
print("{} percent packet loss from unreachable ip {}".format(output, cmd[-1]))
else:
print("{} percent packet loss from reachable ip {}".format(output, cmd[-1]))
如果您安装了 scapy 模块,您应该尝试使用:
clients = arping("192.168.1.*")
print clients
速度很快,还会打印与 ip 关联的 mac 地址,因此非常有用。
我想 ping Python 中的 运行 个 IP 地址并打印: "The IP is reachable with a X% percent package loss" 或 "The IP isn't reachable with a X% package loss"
我想尝试的 运行ge 是 192.168。0.X 其中 X 是 0-255
的 运行ge到目前为止,这是我的代码;
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 -cl 192.168.0.2" 时,它说 -cl 是一个仅限管理员的命令。我是计算机的管理员,我 运行 以管理员身份执行命令,这有什么问题吗?
我正在使用 Windows 8.1 Python v2.7.9
如果您正在使用 linux 并且只想查看丢包情况:
import shlex
from subprocess import PIPE, Popen
cmd1 = shlex.split("grep -oP '\d+(?=% packet loss)'")
for x in range(1, 256):
cmd = "ping -c 4 192.168.43.{}".format(x).split()
p1 = Popen(cmd, stdout=PIPE, stderr=PIPE)
p2 = Popen(cmd1, stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()
output = p2.communicate()[0].rstrip()
if output == "100":
print("{}% percent packet loss from unreachable ip {}".format(output, cmd[-1]))
else:
print("{}% percent packet loss from reachable ip {}".format(output, cmd[-1]))
免责声明我不使用 windows 所以愿意接受更正,但这可能有效:
for x in range(1, 256):
cmd = "ping -c 1 192.168.43.{}".format(x).split()
p1 = Popen(cmd, stdout=PIPE, stderr=PIPE)
lines = p1.communicate()[0].splitlines()
output = (lines[-2]).rsplit(None,5)
output = output[-5].rstrip()
if output == "100%":
print("{} percent packet loss from unreachable ip {}".format(output, cmd[-1]))
else:
print("{} percent packet loss from reachable ip {}".format(output, cmd[-1]))
如果您安装了 scapy 模块,您应该尝试使用:
clients = arping("192.168.1.*")
print clients
速度很快,还会打印与 ip 关联的 mac 地址,因此非常有用。