2010年6月28日 星期一

ping command

Descripton :
Use ping to ping other computer.
Make sure it is alive.

Design method :
      
+------+
| ping |
| |
+------+
|
|
|
V
+------------+
+------------------------->|read command|
| +------------+
| |
| |
| |
| |
| |
+-----+ |
| B |<------------------------------+
| |
+-----+
|
|
| +-------------+
+---------------->| print Result|
+-------------+

comment :
B - determine (0,1,2) how many received
If know how many number do we got.
Then print result.
Ping next address.



Code :

import os
import re
import time
import sys

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

print time.ctime()

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

print time.ctime()

Reference :

  1. http://www.wellho.net/solutions/python-python-threads-a-first-example.html

Related Posts:

0 意見:

張貼留言