标签:
需求:现在IDC需要改造万兆网络,由于是单链路,前期需要利用现有的千兆网络来做冗余,当万兆网络出问题以后自动切换到千兆环境,确保网络自动恢复;
#!/bin/bash # * ******************************************************************************************************* # * Date: 2015-10-16 # * Author: Created by Ctry # * Email: 554248852@qq.com # * Description: OS7.1 When the network problems,automatic network switching. # * Filename: network_switch.sh # * Version: 1.1 # * Last modified: 2015-10-16 14:24 # * ******************************************************************************************************* LIP="172.16.34.143" WIP=`cat /tmp/p1p1.tmp` echo ${WIP} QIP=`cat /tmp/em1.tmp` echo ${QIP} ping -c10 172.16.34.1 > /tmp/ping.log if [[ $? -ne 0 && ${WIP} == ${LIP} ]] then echo "$(date +%F_%T) Now, network switch to 1GB link...!" echo "$(date +%F_%T) Now, network switch to 1GB link...!" >> /tmp/$(hostname -s).switch.log /sbin/ifdown p1p1 ; /sbin/ifup em1 ; /sbin/ifdown p1p1 ; /sbin/ifup em1 /sbin/ifconfig em1|awk -F"[: ]+" ‘NR==2{print $3}‘ > /tmp/em1.tmp true > /tmp/p1p1.tmp WPLOSS=`cat /tmp/ping.log|grep loss|awk ‘{print $6}‘|awk -F"%" ‘{print $1}‘` if [[ "${WPLOSS}" -gt 50 && "${WIP}" == ${LIP} ]] then echo "$(date +%F_%T) Now, network switch to 1GB link...!" echo "$(date +%F_%T) Now, network switch to 1GB link...!" >> /tmp/$(hostname -s).switch.log /sbin/ifdown p1p1 ; /sbin/ifup em1 ; /sbin/ifdown p1p1 ; /sbin/ifup em1 /sbin/ifconfig em1|awk -F"[: ]+" ‘NR==2{print $3}‘ > /tmp/em1.tmp true > /tmp/p1p1.tmp fi fi ping -c10 172.16.34.1 > /tmp/ping.log if [[ $? -ne 0 && ${QIP} == ${LIP} ]] then echo "$(date +%F_%T) Now, network switch to 10GB link...!" echo "$(date +%F_%T) Now, network switch to 10GB link...!" >> /tmp/$(hostname -s).switch.log /sbin/ifdown em1 ; /sbin/ifup p1p1 ; /sbin/ifdown em1 ; /sbin/ifup p1p1 /sbin/ifconfig p1p1|awk ‘NR==2{print $2}‘ > /tmp/p1p1.tmp true > /tmp/em1.tmp QPLOSS=`cat /tmp/ping.log|grep loss|awk ‘{print $6}‘|awk -F"%" ‘{print $1}‘` if [[ "${QPLOSS}" -gt 50 && "${QIP}" == ${LIP} ]] then echo "$(date +%F_%T) Now, network switch to 10GB link...!" echo "$(date +%F_%T) Now, network switch to 10GB link...!" >> /tmp/$(hostname -s).switch.log /sbin/ifdown em1 ; /sbin/ifup p1p1 ; /sbin/ifdown em1 ; /sbin/ifup p1p1 /sbin/ifconfig p1p1|awk ‘NR==2{print $2}‘ > /tmp/p1p1.tmp true > /tmp/em1.tmp fi fi W=`/sbin/ifconfig p1p1|awk ‘NR==2{print $2}‘` Q=`/sbin/ifconfig em1|awk -F"[: ]+" ‘NR==2{print $3}‘` if [[ "${W}" == ${LIP} && "${Q}" == ${LIP} ]] then /sbin/ifdown em1 ; /sbin/ifup p1p1 fi if [[ "${W}" == ${LIP} ]] then echo ${LIP} > /tmp/p1p1.tmp else true > /tmp/p1p1.tmp fi if [[ "${Q}" == ${LIP} ]] then echo ${LIP} > /tmp/em1.tmp else true > /tmp/em1.tmp fi
最后利用nohup配合while循环来实现实时监控网络情况:
#!/bin/bash # * ******************************************************************************************************* # * Date: 2015-10-16 # * Author: Created by Ctry # * Email: 554248852@qq.com # * Description: Run a network_switch script every ten seconds # * Filename: network_sleep1.sh # * Version: 1.1 # * Last modified: 2016-1-8 18:24 # * ******************************************************************************************************* while true do /bin/sh /server/scripts/network_switch.sh >> /tmp/$(hostname -s).switch.log 2>&1 sleep 10 done
标签:
原文地址:http://www.cnblogs.com/Ctry/p/5122706.html