Copy codeThe Code is 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 ', 192 ))
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 codeThe Code is as follows:
#! /Bin/bash
Vip = 8.8.8.8
Vip_local = 'ifconfig | grep-A 1 "eth0: 0" | awk '/inet addr/{print $2}' | cut-d ":"-f2'
If [! $ Vip_local]; then ifconfig eth0: 0 $ vip netmask 255.255.255.0 up; fi