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.
Copyright © 2006-2008 Corey Goldberg
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.