自动漂移VIP-1

#!/bin/bash
#2020-03-16 14:24
#auto change nginx vip.sh
#By Precious
################################
NGX_PRE="24"
ETH_ENS="ens33:0"
NGX_VIP="192.168.1.188"
ETH_DIR="/etc/sysconfig/network-scripts/"
while true
do
NGX_NUM="$(ps -ef|grep nginx |grep -v grep|grep -v change|wc -l)"
if [ $NGX_NUM -ne 0 ];then
    ping -c 2 $NGX_VIP
    if [ $? -ne 0 ];then
    cd $ETH_DIR
    cp ifcfg-ens33 ifcfg-$ETH_ENS
cat >>ifcfg-$ETH_ENS<<EOF
BOOTPROTO=none
DEFROUTE=yes
DEVICE=$ETH_ENS
IPADDR=$NGX_VIP
PREFIX=$NGX_PRE
EOF
    cat ifcfg-$ETH_ENS
    ifup ifcfg-$ETH_ENS
    fi
else
    ifdown ifcfg-$ETH_ENS
    rm -rf $ETH_DIR/ifcfg-$ETH_ENS
fi
sleep 2 
done

自动漂移VIP-2

#!/bin/bash
#2020年1月7日20:42:50
#auto change service VIP
#by author www.jfedu.net
########################
ETH_NAME="ens33:1"
NGX_VIP="192.168.1.188"
NGX_MASK="255.255.255.0"
ETH_DIR="/etc/sysconfig/network-scripts"
NGX_NUM=`ps -ef|grep nginx|grep -v grep|grep -v check|wc -l`

start(){
while sleep 4 
do
if [ $NGX_NUM -eq 0 ];then
    ifdown $ETH_NAME
    exit 0    
else
    ping -c 2 $NGX_VIP >/dev/null 2>&1
    if [ $? -ne 0 ];then
cat>$ETH_DIR/ifcfg-$ETH_NAME<<EOF
TYPE="Ethernet"
BOOTPROTO="static"
DEVICE="$ETH_NAME"
IPADDR=$NGX_VIP
NETMASK=$NGX_MASK
ONBOOT="yes"
EOF
    ifup $ETH_NAME
    fi
fi
date
done
}

stop(){
    ifdown $ETH_NAME
    rm -rf $ETH_DIR/ifcfg-$ETH_NAME
}

case $1 in
    start)
    start
    ;;
    stop)
    stop
    ;;
    *)
    echo -e "\033[32m------------------\033[0m"
    echo -e "\033[32mUsage: /bin/sh $0 {start|stop|help}\033[0m"
        exit 1 
esac