|
[php]
#!/usr/bin/python
#Filename:checkLink.py
import os,time
from socket import *
#FODDER = ('www.waven.com',80)
FODDER = ('82.165.144.41',80)
LOCK = '/root/lock.chklive'
class CheckLink:
def __init__(self):
self.MAXRETRY = 2
self.RESULT = 0 #0 for OK, -1 for error
self.sfd = socket(AF_INET,SOCK_STREAM)
self.sfd.settimeout(1.5)
self.checking_tcp()
self.process()
def checking(self):
for i in range(self.MAXRETRY):
err = os.system("ping 82.165.144.41-c 1 -W 2")
if err == 0:
return 0
else:
print str(i+1) + ' times ERROR!'
self.RESULT = -1
def checking_tcp(self):
for i in range(self.MAXRETRY):
try:
self.sfd.connect(FODDER)
print 'connected OK! Link alive!'
self.sfd.close()
return 0
except:
print str(i+1) + ' times ERROR!'
pass
self.RESULT = -1
def process(self):
if self.RESULT == 0:
print 'I am so happy.Link is survival.'
else:
print 'Link down? I must dial again..'
self._process_redial()
def _process_redial(self):
print 'redialing up.....'
try:
open(LOCK)
print 'still dialing..pass'
except:
#first dial
file(LOCK,'w')
os.system('/usr/bin/killall -9 pppd;echo "ATZ" > /dev/modem;/usr/bin/pon')
time.sleep(25)
os.remove(LOCK)
if not self.checking():
_process_redial()
########################
# main routine #
########################
if __name__ == '__main__':
run = CheckLink()
[/php]
检查ppp0是否有效,无效重播。
需要cron配合使用。 |
|