Copy Code code as follows:
#coding: Utf-8
Import socket
Import Select
Import time
Import OS
Import threading
Def ser ():
s = Socket.socket (socket.af_inet,socket. SOCK_DGRAM)
s.bind (("", 43244)
while 1:
Infds,outfds,errfds = Select.select ([s],[],[],5)
If Infds:
SMS = S.RECV (1024)
if sms== "alived":
print "Peer is alived"
else:
print "Can ' t hear peer!"
Os.system ("./failover.sh")
def CLT ():
While 1:
Sock = Socket.socket (socket.af_inet, socket. SOCK_DGRAM)
Sock.connect ((' 192.168.10.1 ', 43244))
Sock.send ("alived")
Time.sleep (2)
If __name__== "__main__":
Ser=threading. Thread (Target=ser)
Clt=threading. Thread (TARGET=CLT)
Ser.start ()
Clt.start ()
Ser.join ()
Clt.join ()
failover.sh
Copy Code code as follows:
#!/bin/bash
vip=8.8.8.8
Vip_local= ' ifconfig |grep-a 1 "eth0:0" | awk '/inet addr/{print $} ' |cut-d ': '-f2 '
if [! $VIP _local];then ifconfig eth0:0 $vip netmask 255.255.255.0 up;fi