#!/bin/bash# Program# Use ping command to check the network's PC state.PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHmaster_ip="172.16.1.154"slave_ip="172.16.1.110"vip="172.16.1.90"function check_host{# master_host=("172.16.1.71" )# network="192.168.1" # 先定义一个网域的前面部分!# 底下的程序在取得 ping 的回传值是正确的还是失败的!count=0for i in $(seq 1 10)do ping -c 1 -w 10 $1 &> /dev/null && result=0 || result=1if [ "$result" == 1 ]; then #表示没有ping通count=$[count + 1]elsecount=0breakfisleep 5done# 开始显示结果是正确的启动 (UP) 还是错误的没有连通 (DOWN)if [ "$count" == 0 ]; thenecho "Server virtual_ip is UP."elseecho "Server virtual_ip is DOWN."add_vip $vipfi}function add_vip{ip addr add local "$1/24" brd + label eth0:1 dev eth0 #添加虚拟IP}function del_vip{ip addr del "$1/24" brd + dev eth0:1 #删除虚IP}function main{add_vip $vipwhile [ 1 ]docheck_host $vipsleep 30done}main