goldb.org home

AS OF MAY 2008, THIS BLOG IS NO LONGER BEING UPDATED.
Visit the new blog at: http://coreygoldberg.blogspot.com



 Wednesday, April 09, 2008

Python - Host/Device Ping Utility for Windows

This script uses your system's ping utility to send an ICMP ECHO_REQUEST to a list of hosts or devices. It uses a separate thread to ping each host/device. This can be useful for measuring network latency and verifying hosts are alive.

Check out more info here: http://www.goldb.org/python_pinger.html


#!/usr/bin/env python

import re
from subprocess import Popen, PIPE
from threading import Thread


class Pinger(object):
    def __init__(self, hosts):
        for host in hosts:
            pa = PingAgent(host)
            pa.start()
        
class PingAgent(Thread):
    def __init__(self, host):
        Thread.__init__(self)        
        self.host = host

    def run(self):
        p = Popen('ping -n 1 ' + self.host, stdout=PIPE)
        m = re.search('Average = (.*)ms', p.stdout.read())
        if m: print 'Round Trip Time: %s ms -' % m.group(1), self.host
        else: print 'Error: Invalid Response -', self.host
              
                             
if __name__ == '__main__':
    hosts = [
        'www.pylot.org',
        'www.goldb.org',
        'www.google.com',
        'www.this_one_wont_work.com'
       ]
    Pinger(hosts)

Output:

Round Trip Time: 14 ms - www.yahoo.com
Round Trip Time: 17 ms - www.goldb.org
Round Trip Time: 30 ms - www.google.com
Round Trip Time: 82 ms - www.pylot.org
Error: Invalid Response - www.this_one_wont_work.com

Note: I only tested this on Windows. To run on other systems, it would only require a one-line change.

#    Comments [3] |
Tuesday, April 15, 2008 5:50:54 AM (Eastern Standard Time, UTC-05:00)
This is for pinging a list taken from a file:


import re
from subprocess import Popen, PIPE
from threading import Thread


class Pinger(object):
def __init__(self, hosts):
for host in hosts:
pa = PingAgent(host)
pa.start()

class PingAgent(Thread):
def __init__(self, host):
Thread.__init__(self)
self.host = host

def run(self):
p = Popen('ping -n 1 ' + self.host, stdout=PIPE)
m = re.search('Average = (.*)ms', p.stdout.read())
if m: print 'Round Trip Time: %s ms -' % m.group(1), self.host
else: print 'Error: Invalid Response -', self.host


if __name__ == '__main__':
serverlist=open('serverlist.txt','r')
hosts=[i.strip() for i in serverlist.readlines()]
serverlist.close()
Pinger(hosts)
Sunday, April 20, 2008 4:29:45 PM (Eastern Standard Time, UTC-05:00)
Hi Corey!

To be able to operate in other than English language versions of Windows your code has to be modified a little.

The last line of output in the English version is:
Minimum = 32ms, Maximum = 37ms, Average = 34ms
and in the Finnish version:
Pienin = 32 ms, Suurin = 37 ms, Keskiarvo = 34 ms

Consequently we can't match on the word 'Average'. Assuming the format is the same for all language versions and that there are no numbers on the last line except those in the response time values we get the modified run method shown below.

<code>
def run(self):
p = Popen('ping -n 5 ' + self.host, stdout=PIPE)
answer = p.stdout.read()
last_line = answer.split('\n')[-2]
m = re.search('(?:[^\d]+(\d+)){3}', last_line)
if m: print 'Round Trip Time: %s ms -' % m.group(1), self.host
else: print 'Error: Invalid Response -', self.host
</code>

Cheers,
Jussi
Monday, April 21, 2008 8:38:15 AM (Eastern Standard Time, UTC-05:00)
@Jussi:

thanks! I never even thought of non-english versions.
Comments are closed.