Python script - log failed icmp/ping response - problems -
i'm attempting write python script ping/icmp ip address , tell me if it's alive. i'm doing because have intermittent issue. wanted ping, log outcome, sleep period , attempt ping again. tried while loop, still getting errors these:
line 33, in (module) systemping('192.168.1.1') line 30, in systemping time.sleep(30) keyboardinterrupt
i'm using python 2.6.
ideally question how loop through method/function systemping , errors there in code? script seems work, these errors when hit ctrl-c.
from subprocess import popen, pipe import datetime, time, re logfile = open("textlog.txt", "a") def getmytime(): = datetime.datetime.now() return now.strftime("%y-%m-%d %h:%m \n") starttime = "starting ..." + getmytime() logfile.write(starttime) logfile.write("\n") def systemping(x): cmd = popen("ping -n 1 " + x , stdout=pipe) #print getmytime() line in cmd.stdout: if 'timed out' in line: loggedtime = "failure detected - " + getmytime() logfile.write(loggedtime) if 'reply' in line: print "replied..." logfile.close() print "sleeping 30mins ... ctrl c end" time.sleep(30) #1800 30mins systemping('192.168.1.1') if __name__ =='__main__': systemping('192.168.1.1')
any appreciated. thank you.
it's not error per se, it's default behavior python, upon receipt of sigint
(which happens when press ctrl-c), raise keyboardinterrupt
exception.
you'll same thing if send signal kill(1)
, like...
$ kill -int <pid>
if want handle it, can change code like...
if __name__ =='__main__': try: systemping('192.168.1.1') except keyboardinterrupt: print 'finished'
...or whatever want do.
Comments
Post a Comment