Script to ping computer

Hi everyone!

I’d like to do a page that will show the health of our main system. I need to ping some devices and show the response time.

Is it possible to do this using in a script?


There is a 3rd party ping module that has been created.

Yes I found it but the link is not working…

You can do this natively in python

import os
online = os.system("ping") == 0

This os.system("ping ") will return 0 if successful. So comparing it to 0 will get you a boolean value of if the device can be pinged or not.

Edit: I might add there are some nuances to this depending on OS the script is running on, but in general it should work.

I just tested this on Windows, and it returns a 0 when the ping passes or fails.

Kymera has a ping driver that might suit your needs, take a look

This is the script you are looking for:

def ping(address):
	import subprocess
	command = 'ping', '%s' % address
	# shell=True hides the shell window, stdout to PIPE enables communicate() to get the ping command result
	process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
	# trimming it to the actual lines with information
	process_out = process.communicate()[0].strip().split('\r\n')
	# print out the return of the ping
	print process_out[1:5]

now just call it inside your script ping('') and it will ping the address and print out the output


This is working for me in windows. Although I just realized the OP asked for response time too. @c.bertulli looks like he has the best answer.

import os

def ping(ip):
     return os.system("ping " + ip) == 0

print ping("")
print ping("")
print ping("")

If you are running in Unix you’ll need to change the first part of the os.system call to ping -c 5 to limit the number of ping attempts.

This should print out the results of the command and also return a boolean value for each function call. Results should be True, False, False

1 Like

Working good!

Thanks everone!

1 Like

Well that is weird, I tested your code last night, and it didn’t work, and today it does. I stand corrected.

1 Like

Shelling out for this makes me feel very icky, especially when Java already has the native ability to do this and that’s an invitation to accidentally send horrible things to the command line. Why not just:

def ping(address,timeout=2000):
	from import InetAddress
	ip = InetAddress.getByName(address)
	return ip.isReachable(timeout)

This will throw UnknownHostException if the host can’t be looked up, or IOException if the network is broken, so you can handle that case as well, instead of not even knowing it happened unless you parse unexpected command-line output. Or just swallow exceptions, of course.* can do all the fun things you thought you needed command-line tools for, and more.


Nice! I like this a lot more.

Another downside to using os.system for ping in Windows is that it writes the ping results to the wrapper.log file. I’m pinging 100 devices every few minutes and this clogs up the log files like so:

INFO | jvm 1 | 2020/10/09 17:45:00 | Pinging with 32 bytes of data:
INFO | jvm 1 | 2020/10/09 17:45:00 | Reply from bytes=32 time<56ms TTL=64
INFO | jvm 1 | 2020/10/09 17:45:00 |
INFO | jvm 1 | 2020/10/09 17:45:00 | Ping statistics for
INFO | jvm 1 | 2020/10/09 17:45:00 | Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
INFO | jvm 1 | 2020/10/09 17:45:00 | Approximate round trip times in milli-seconds:
INFO | jvm 1 | 2020/10/09 17:45:00 | Minimum = 0ms, Maximum = 0ms, Average = 0ms

1 Like