Thank you James for the guidelines. It helped me resolve the problem ... Though I added error trapping, no errors were thrown. But it made me think that the print command in check status might be flooding the log ( I check for about 10 devices). Commenting the print made it work. now I have script running for over 3 hours ...
I modified it so that it can take an IP or device name ... Since ping accept both ... That makes it flexible. I added error traping thought I am not sure I have done it right (first time in Python)
The on/off issue might be from my router as I am having the same issue with the iMac that connects through wifi
Here is the version I am using
import os,thread
class box(object):
def __init__(self, name=None, present=None):
self.name = name
self.present = present
def checkStatus(self):
try:
ping = os.popen("ping %s -n 1 -w 450" %(self.name))
pingResult= ping.read()
#print pingResult
present = "bytes=32" in pingResult
if(present!=self.present):
eventName='OnLine.%s.%s' %(present,self.name)
vc.triggerEvent(eventName,None)
self.present=present
return self.present
except:
e = sys.exc_info()[0]
print (str (e))
def pingLoop(delay):
x = 0
while 1:
#vc.triggerEvent("Ping Loop." + str (x), None)
x = x +1
for target in boxList:
try:
target.checkStatus()
except:
e = sys.exc_info()[0]
vc.triggerEvent("Ping Loop error.", List [str](str (x),str (e)))
time.sleep(delay) # pause between loops
def getPingList():
mylist=""
for target in boxList:
mylist+=target.name
if target.present:
mylist+=" on-line, "
else:
mylist+=" off-line, "
mylist = mylist.strip()
mylist = mylist.strip(',')
return mylist
boxList = []
#----------------------- LAN Devices -----------------------
boxList.append(box("devicename", None)) #replace device name with actual device name
boxList.append(box("192.168.1.1", None)) #replace IP with actual fixed IP of device
# add more devices here
thread.start_new_thread( pingLoop, (60,) )