Pythononly ping 1 out of 2 IP's (Subprocess)
Pythononly pings 1 out of 2 IP's (Subprocess)
我正在尝试制作 PY 2。7.x 那里的代码应该 ping 几个地址。
我有一个列表,其中包含两个 IP 地址。但它只是最后一个被 ping 的?
import subprocess
ip = ["10.0.2.5", "8.8.8.8"]
for i in range(len(ip)):
ping_process = subprocess.Popen(['ping', '-c', '1'] + ip, stdout=subprocess.PIPE)
stdout = ping_process.stdout.read()
print stdout
首先,您尝试将字符串添加到列表中,但这是行不通的。
>>> ["23123"] + "4234"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
您需要使用 .append() 来添加元素,但在这种情况下,创建元素时只需将元素添加到列表中即可。
使用更好的变量名可能从未遇到过此问题,请考虑以下内容。
import subprocess
list_of_ips = ["10.0.2.5", "8.8.8.8"]
for ip in list_of_ips:
ping_process = subprocess.Popen(['ping', '-c', '1', ip], stdout=subprocess.PIPE)
stdout = ping_process.stdout.read()
print stdout
我正在尝试制作 PY 2。7.x 那里的代码应该 ping 几个地址。
我有一个列表,其中包含两个 IP 地址。但它只是最后一个被 ping 的?
import subprocess
ip = ["10.0.2.5", "8.8.8.8"]
for i in range(len(ip)):
ping_process = subprocess.Popen(['ping', '-c', '1'] + ip, stdout=subprocess.PIPE)
stdout = ping_process.stdout.read()
print stdout
首先,您尝试将字符串添加到列表中,但这是行不通的。
>>> ["23123"] + "4234"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
您需要使用 .append() 来添加元素,但在这种情况下,创建元素时只需将元素添加到列表中即可。
使用更好的变量名可能从未遇到过此问题,请考虑以下内容。
import subprocess
list_of_ips = ["10.0.2.5", "8.8.8.8"]
for ip in list_of_ips:
ping_process = subprocess.Popen(['ping', '-c', '1', ip], stdout=subprocess.PIPE)
stdout = ping_process.stdout.read()
print stdout