LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 941|回复: 1

linkAlive? (rubbish)

[复制链接]
发表于 2006-9-23 09:08:59 | 显示全部楼层 |阅读模式
[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配合使用。
 楼主| 发表于 2006-10-8 16:33:27 | 显示全部楼层
有重大问题……
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表