网卡切换脚本
#!/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:~/bin export PATH master_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=0 for i in $(seq 1 10) do ping -c 1 -w 10 $1 &> /dev/null && result=0 || result=1 if [ "$result" == 1 ]; then #表示没有ping通 count=$[count + 1] else count=0 break fi sleep 5 done # 开始显示结果是正确的启动 (UP) 还是错误的没有连通 (DOWN) if [ "$count" == 0 ]; then echo "Server virtual_ip is UP." else echo "Server virtual_ip is DOWN." add_vip $vip fi } 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 $vip while [ 1 ] do check_host $vip sleep 30 done } main