# Ping test using python

**URL:** <https://community.unix.com/t/ping-test-using-python/356965>\
**Category:** Shell Programming and Scripting\
**Created:** [November 24, 2015, 12:23am UTC](https://community.unix.com/t/ping-test-using-python/356965 "2015-11-24T00:23:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctrld](https://community.unix.com/letter_avatar/ctrld/32/6_22734026db12803ebb3e059622ac3534.png) [@ctrld](https://community.unix.com/u/ctrld)\
**Post date:** [November 24, 2015, 12:23am UTC](https://community.unix.com/t/ping-test-using-python/356965/1 "2015-11-24T00:23:56Z")

</div>

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.

```nohighlight
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:

```nohighlight
Rebooting ...............
Reboot successful!

```

---

<div class="post-metadata">

**Author:** ![cjcox](https://community.unix.com/user_avatar/community.unix.com/cjcox/32/1232_2.png) [@cjcox](https://community.unix.com/u/cjcox)\
**Post date:** [November 24, 2015, 12:10pm UTC](https://community.unix.com/t/ping-test-using-python/356965/2 "2015-11-24T12:10:53Z")

</div>

consider:

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![ctrld](https://community.unix.com/letter_avatar/ctrld/32/6_22734026db12803ebb3e059622ac3534.png) [@ctrld](https://community.unix.com/u/ctrld)\
**Post date:** [November 24, 2015, 12:18pm UTC](https://community.unix.com/t/ping-test-using-python/356965/3 "2015-11-24T12:18:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Aia](https://community.unix.com/user_avatar/community.unix.com/aia/32/917_2.png) [@Aia](https://community.unix.com/u/Aia)\
**Post date:** [November 24, 2015, 2:46pm UTC](https://community.unix.com/t/ping-test-using-python/356965/4 "2015-11-24T14:46:34Z")

</div>

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

Maybe this might help:

```nohighlight
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"

```

---

<div class="post-metadata">

**Author:** ![ctrld](https://community.unix.com/letter_avatar/ctrld/32/6_22734026db12803ebb3e059622ac3534.png) [@ctrld](https://community.unix.com/u/ctrld)\
**Post date:** [November 24, 2015, 10:27pm UTC](https://community.unix.com/t/ping-test-using-python/356965/5 "2015-11-24T22:27:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cjcox](https://community.unix.com/user_avatar/community.unix.com/cjcox/32/1232_2.png) [@cjcox](https://community.unix.com/u/cjcox)\
**Post date:** [November 24, 2015, 10:50pm UTC](https://community.unix.com/t/ping-test-using-python/356965/6 "2015-11-24T22:50:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ctrld](https://community.unix.com/letter_avatar/ctrld/32/6_22734026db12803ebb3e059622ac3534.png) [@ctrld](https://community.unix.com/u/ctrld)\
**Post date:** [November 24, 2015, 11:18pm UTC](https://community.unix.com/t/ping-test-using-python/356965/7 "2015-11-24T23:18:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Aia](https://community.unix.com/user_avatar/community.unix.com/aia/32/917_2.png) [@Aia](https://community.unix.com/u/Aia)\
**Post date:** [November 25, 2015, 1:52am UTC](https://community.unix.com/t/ping-test-using-python/356965/8 "2015-11-25T01:52:37Z")

</div>

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.
