【发布时间】:2015-03-03 18:19:27
【问题描述】:
我正在尝试编写一个 python 脚本,您可以在其中输入几个 ip 并不断 ping 它们,然后输出结果。我想让它循环通过 ips 并继续运行直到停止,但是我无法做到这一点。输出 if 语句也总是返回 false。建议/帮助?
import os
import subprocess
print ("PingKeep will run a Ping request every 5 seconds on a round of IP's until told to stop (using ctrl+c)." )
ips=[]
n=int(input("How many IP's are we checking: "))
for i in range(1,n+1):
x=str(input("Enter IP number "+str(i)+": "))
ips.append(x)
for ping in range(0,n):
ipd=ips[ping]
res = subprocess.call(['ping', '-n', '3', ipd])
if ipd in str(res):
print ("ping to", ipd, "OK")
elif "failure" in str(res):
print ("ping to", ipd, "recieved no responce")
else:
print ("ping to", ipd, "failed!")
【问题讨论】:
-
您需要注意缩进。
-
我知道,我试图直接从我的代码中复制它,但格式错误。
标签: python windows loops ping continuous