-1

I am trying to write a python script where you input a few ips and continuously ping them, then output a result. I wanted to have it loop through the ips and just keep going until stopped however I cannot get that to happen. Also the output if statement always returns 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!")

2 Answers2

0

First, your indentation is all screwed up, please fix it.

Second, why are pinging 3 times per run with -n 3? If the first 2 pings fail, but the third one returns fine, it still counts as a success, just an FYI.

Third, you are only looping through the IPs entered, rather than looping indefinitely. 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.

Fourth, What output statement is returning 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". res will only ever be a 0 or 1, never a string.

Fifth, try writing this question again after you've tried harder, armed with the insights I've given you.

jgritty
  • 11,660
  • 3
  • 38
  • 60
  • 1. I know, I tried to copy it straight from my code and the format just came up wrong. 2. I know it only pings 3 times, I really have no problem with it pinging one or two times because I hoped to get it to ping continuously anyways so that one false positive or false negative wouldn't be end of world. 3. That is true. I should've done that and just didn't think of it. 4. thanks. Fixed :) 5. Can't we all just get along? – Wilfredo Skarface Feliberty Mar 03 '15 at 19:14
  • I wasn't trying to be mean :) – jgritty Mar 03 '15 at 19:40
0

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:

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)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321