Ping test using python

I am issuing a reboot command to a client server from admin server using my python function. I am able to do that but now I need do a ping test every second and print if the ping is successful. With the below code I can check ping only once.

import os hostname = "myclient" 
response = os.system("ping -c 1 " + hostname)  

if response == 0:   
      print hostname, 'Reboot successful!'
else:
      print hostname, 'Rebooting..!'

Also if I am able to print "." (one dot) per ping check in one line instead of "Rebooting..!" line by line that would be awesome.

I am trying for the output:

Rebooting ...............
Reboot successful!

consider:

import os, sys, time
hostname = "myclient"
response = os.system("ping -c 1 " + hostname + " >/dev/null 2>&1")

if response == 0:
      print hostname, 'Reboot successful!'
      sys.exit(0)
else:
      sys.stderr.write(hostname + ' Rebooting..')

sleep_wait=1
max_ping_wait=10
count=0
while (count < max_ping_wait and os.system("ping -c 1 " + hostname + " >/dev/null 2>&1")):
      sys.stderr.write('.')
      time.sleep(sleep_wait)
      count+=1

if (count < max_ping_wait):
    print hostname, 'Reboot successful!'
    sys.exit(0)
else:
    print hostname, 'Reboot unsuccessful.'
    sys.exit(1)

Why stderr for the dots? Stderr is usually unbuffered...
Adjust max_ping_wait and sleep_wait however you like.

---------- Post updated at 11:10 AM ---------- Previous update was at 11:10 AM ----------

oh.. tweak the output, my output isn't exactly like how you wanted.

1 Like

Intelligent .. I am gonna try this right away..

subprocess is the module intended to be used instead of os.system

Maybe this might help:

import subprocess
import sys

host = 'myclient'
cmd = ['ping', '-c2', '-W 5', host ]
done = False
timeout = 10 # default time out after ten times, set to -1 to disable timeout

print "Rebooting .",
while not done and timeout:
        response = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        stdout, stderr = response.communicate()
        if response.returncode == 0:
            print "Server up!"
            done = True
        else:
            sys.stdout.write('.')
            timeout -= 1
if not done:
       print "\nServer failed to respond"
1 Like

Both solution works after I add a sleep command as my reboot command takes couple of seconds to actually bring down my client server. So I need to tweak this while loop so that it will start waiting on the first failure of ping and come out when its success. I cant rely on the sleep command as I am dealing with thousands of client servers and unnecessary sleep cannot be afforded.

sleep will will make things more interruptible. Even a sleep 1. So, it simply means you could lose one second at most vs not having it, and you can interrupt it (if that's important to you). Makes it more multiprocess friendly.

I am afraid sleep may make it complicated as some servers may take more than 10 seconds to respond and some in 1 second. Network response also matters.

---------- Post updated at 09:48 AM ---------- Previous update was at 09:45 AM ----------

Rather than sleep, having a wait for ping to fail and then print "Reboot started " then execute the while loop makes more sense to me. But that would need another loop I guess.

If you have thousands of servers what you are doing is not a good way. If you have a couple of cows you treat them as a farmer would, but if you have thousands of cows you need to start thinking as a rancher. That means multithread and/or parallelization. You cannot afford to cater to each server so much interactive sysadmin time. Even when it appears to be just a few seconds of visual attention.

Nevertheless, if you were to first fire-up a reboot to each server in serialization, (I presume you are doing that via script as well), by the time you finish issuing the reboot to the last one, you can start checking on the first one, with the script as stand, and most likely the first server has rebooted already.