SerialPing.py

From dftwiki3
Revision as of 13:24, 4 February 2010 by Thiebaut (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 16:07, 24 January 2010 (UTC)

# serialping.py
# pings a range of IPs for answers
# taken from http://www.wellho.net/solutions/python-python-threads-a-first-example.html
# information returned by ping:
#
#   % ping -q -c2 131.229.72.40
#   PING 131.229.72.40 (131.229.72.40) 56(84) bytes of data.
#
#   --- 131.229.72.40 ping statistics ---
#   2 packets transmitted, 2 received, 0% packet loss, time 1005ms
# 
# The program reads the output of the ping command and captures the number (\d)
# appearing before the word "received".  The number is 0, 1, or 2.  0 means
# no packets received.  1 means one out of two.  2 means two sent, two received:
# node is alive.
# This number is used as an index in the array report to qualify the result.
#
# rtt min/avg/max/mdev = 0.348/0.525/0.702/0.177 ms
# 
# --- 131.229.72.40 ping statistics ---
# 2 packets transmitted, 2 received, 0% packet loss, time 1005ms
# rtt min/avg/max/mdev = 0.348/0.525/0.702/0.177 ms

import os
import re
import time
import sys

lifeline = re.compile(r"(\d) received")
report = ( "No response", "Partial Response", "Alive" )

for host in range( 1, 50 ):
   ip = "131.229.72." + str( host )
   pingaling = os.popen( "ping -q -c2 " + ip, "r" )
   print "Testing ", ip,
   sys.stdout.flush()
   while True:
      line = pingaling.readline()
      if not line: break
      igot = re.findall( lifeline, line )
      if igot:
           print report[ int( igot[0] ) ]