Host/Device Ping Utility for Windows by Corey Goldberg

Subscribe To Corey's Blog Subscribe To Goldblog goldb.org home



Python | Host/Device Ping Utility for Windows

by Corey Goldberg - 2008

This script uses your system's ping utility to send an ICMP ECHO_REQUEST to a list of hosts or devices. This is useful for measuring network latency and verifying hosts are alive.


Download:

pinger.py


Pinger Script:

#!/usr/bin/env python
#
#  pinger.py
#  Host/Device Ping Utility for Windows
#  Corey Goldberg (www.goldb.org), 2008
#
#
#  Pinger uses your system's ping utility to send an ICMP ECHO_REQUEST 
#  to a list of hosts or devices.  This is useful for measuring network
#  latency and verifying hosts are alive.


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.yahoo.com',
        'www.techcrunch.com',
        'www.this_one_wont_work.com'
       ]
    Pinger(hosts)

Output:

Round Trip Time: 14 ms - www.yahoo.com
Round Trip Time: 82 ms - www.techcrunch.com
Round Trip Time: 30 ms - www.google.com
Round Trip Time: 17 ms - www.goldb.org
Round Trip Time: 71 ms - www.foo.com
Error: Invalid Response - www.this_one_wont_work.com
Copyright © 2006-2008 Corey Goldberg  |