Вот написал скриптик для переключения на другого провайдера если основной упал.
#!/usr/local/bin/python
# tested on FreeBSD 5.4
# written by Sergey Poddubchak, 2005
# put it to root's crontab
# * * * * * /usr/local/bin/python /usr/local/sbin/if_switcher.py
import os
import sys
import time
GateArray = ['81.30.164.94','195.5.5.160']
# List of gates descending by priority
RemoteInternetTestUrl = 'www.ru'
# url to test if connection is alive
GateStatus = {}
InternetStatus = 0
def Sound(Num):
for i in range(0,Num):
# print 'Beep',Num,'times'
if sys.platform[:3] == 'win':
sys.stdout.write('\07')
else:
os.system('echo \"\a\" > /dev/console')
time.sleep(0.2)
time.sleep(1)
def Warning(Text,Num=1):
print 'Warning: ' + Text
Sound(Num)
def Error(Text,Num=5):
print 'Error: ' + Text
Sound(Num)
def PingCmd():
if sys.platform[:3] == 'win':
PingCmdStr = "ping -n 1 %s"
else:
PingCmdStr = "/sbin/ping -c 1 %s"
return PingCmdStr
def TempFileName():
if sys.platform[:3] == 'win':
FileName = 'c:\CurrentIP.txt'
else:
FileName = '/tmp/CurrentChannel'
return FileName
def RouteSwitchCmd(IP):
if sys.platform[:3] == 'win':
Cmd = 'routing switch to '+IP
else:
Cmd = '/sbin/route delete default && /sbin/route add default '+IP
return Cmd
def SaveLinkInfo(LinkInfo):
print 'Saving link info...: ' + LinkInfo
file(TempFileName(),'w+').write(LinkInfo)
def ReadLinkInfo():
print 'Reading link info...'
try:
Result = open(TempFileName(),'r').read()
return Result
except IOError, (errno, strerror):
print "I/O error(%s): %s" % (errno, strerror)
except ValueError:
print "Could not convert data to an integer."
except:
print "Unexpected error:", sys.exc_info()[0]
def SwitchLinkTo(Gate):
print 'Switching routing to ' + NearestWorkingIP
os.system(RouteSwitchCmd(NearestWorkingIP))
SaveLinkInfo(NearestWorkingIP)
if __name__ == '__main__':
GateIndex = 0
NearestWorkingIP = ''
for IP in GateArray:
GateIndex = GateIndex + 1
CmdLine = PingCmd() % IP
GateStatus[IP] = {'stat':0}
GateStatus[IP]['stat'] = os.system(CmdLine)
Result = "Host %s --> %d" % (IP,GateStatus[IP]['stat'])
if GateStatus[IP]['stat'] != 0:
Warning('Gate '+IP+' is down',GateIndex)
print Result+'\n'
else:
if len(NearestWorkingIP) == 0:
NearestWorkingIP = IP
# получили состояния линков
CmdLine = PingCmd() % RemoteInternetTestUrl
InternetStatus = os.system(CmdLine)
if InternetStatus != 0:
print "Bad link!\n"
if len(NearestWorkingIP) > 0:
SwitchLinkTo(NearestWorkingIP)
else:
Error('No working gate.',10)
else:
if NearestWorkingIP != ReadLinkInfo():
if len(NearestWorkingIP) > 0:
print 'Switching to more optimal gate...'
SwitchLinkTo(NearestWorkingIP)
else:
print "Everything Ok!"
print 'Done!'