简体   繁体   English

Python ping循环脚本在windows上运行,报错

[英]Python ping loop script to run on windows, error

I am trying to write a python script where you input a few ips and continuously ping them, then output a result.我正在尝试编写一个 python 脚本,在其中输入几个 ips 并不断 ping 它们,然后输出结果。 I wanted to have it loop through the ips and just keep going until stopped however I cannot get that to happen.我想让它通过 ips 循环并一直运行直到停止,但是我无法做到这一点。 Also the output if statement always returns false.此外,输出 if 语句始终返回 false。 advise/help?建议/帮助?

    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!")

First, your indentation is all screwed up, please fix it.首先,你的压痕都搞砸了,请修复它。

Second, why are pinging 3 times per run with -n 3 ?其次,为什么每次使用-n 3 ping 3 次? If the first 2 pings fail, but the third one returns fine, it still counts as a success, just an FYI.如果前 2 次 ping 失败,但第三次返回正常,则仍视为成功,仅供参考。

Third, you are only looping through the IPs entered, rather than looping indefinitely.第三,您只循环输入输入的 IP,而不是无限循环。 I recommend something like this:我推荐这样的东西:

while True:

That will just loop forever.那将永远循环。 You can put your loop inside that loop to loop through the IPs.您可以将循环放入该循环中以循环访问 IP。

Fourth, What output statement is returning false?四、什么输出语句返回false? I don't see anything like that.我没有看到类似的东西。 Also, your test for failure is totally wrong.此外,您对失败的测试是完全错误的。 The ping executable will return a return code of 1, if the pings timeout, but nothing will ever output "failure".如果 ping 超时,ping 可执行文件将返回返回码 1,但不会输出“失败”。 res will only ever be a 0 or 1, never a string. res永远只会是 0 或 1,永远不会是字符串。

Fifth, try writing this question again after you've tried harder, armed with the insights I've given you.第五,在你用我给你的洞察力武装起来之后,试着再写一遍这个问题。

Not sure what you want to do when the code ends but if you catch a CalleddProcessError when a ping was unsuccessful you willl know when the ping failed, not fully sure exactly how you want to end the program so that I will leave to you:不确定代码结束时要做什么,但是如果在 ping 不成功时捕获CalleddProcessError ,您将知道 ping 何时失败,但不能完全确定您想如何结束程序,因此我将留给您:

from subprocess import check_call, CalledProcessError,PIPE

print("PingKeep will run a Ping request every 5 seconds on a round of          IP's until told to stop (using ctrl+c).")
import time
n = int(input("How many IP's are we checking: "))
ips = [input("Enter IP number {}: ".format(i)) for i in range(1, n + 1)]

while True:
    for ip in ips:
        try:
            out = check_call(['ping', '-n', '3', ip],stdout=PIPE)
        except CalledProcessError as e:
            # non zero return code will bring us here
            print("Ping to {} unsuccessful".format(ip))
            continue
        # if we are here ping was successful
        print("Ping to {} ok".format(ip))
    time.sleep(5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM