Linux 运维----第一个shell脚本

change_route.sh.jpg

                                                                                                                                                                                                                                                                                    环境需求

如上图所示:需要编写shell脚本来达成图片中的条件,自动检测***线路是否正常,如果线路不通,自动切换路由等操作。

脚本如下所示:

#!/bin/bash

export PATH=${PATH}:/bin:/sbin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin     

LOG_DIR=/tmp/route.log                                          #定义日志文件存放的位置

LINES=`cat /tmp/route.log | wc -l`                           #取得当前日志文件得行数

×××_JP=192.168.203.1                                               #***-jp的地址

×××_GH=192.168.205.1                                             #***-gh的地址             

RULE_IP=`/sbin/ip rule list |awk '$3=="all" && $7 ~ /***/{print $5}'|sort|uniq`        # 截取出ip rule list 指令当中列出的IP,这些ip就是需要添加到ip rule add ***-jp或***-gh中的ip

export RULE_IP                                                            

RULE_JP=/tmp/RULE_JP                                            #定义该变量用来存放当前ip rule list列出的***-jp规则

RULE_GH=/tmp/RULE_GH                                        #定义该变量用来存放当前ip rule list列出的***-gh规则

/sbin/ip rule list |awk '$3=="all" && $7=="***-jp"{print $5}'|sort|uniq >/tmp/RULE_JP                                   # 只截取ip rule list中的***-jp规则

/sbin/ip rule list |awk '$3=="all" && $7=="***-gh"{print $5}'|sort|uniq >/tmp/RULE_GH                                # 只截取ip rule list中的***-gh规则

CP=`/sbin/ip rule list|grep 1.1.1.1`                                                                                                                                 # 定义CP变量用来判断当前ip rule list中有没有1.1.1.1的ip存在

export CP

#定义***-jp函数,用来将指定的ip加到***-jp中,并且删除***-gh中的ip

function ***_jp {

for ip in $RULE_IP                                       #RULE_IP这个变量的内容是ip rule list中的所有ip,不管后跟***-jp还是***-gh

do

grep $ip $RULE_GH                                    #判断IP是否在***-gh中存在

[ $? -eq 0 ] && ip rule del to ${ip} table ***-gh         #如果存在,则将该ip从***-gh中删除

grep $ip $RULE_JP                                      #判断IP是否在***-jp中存在

[ $? -eq 0 ] || ip rule add to ${ip} table ***-jp           #如果不存在,则将该ip添加到***-jp中         

done

[ -z "$CP" ] && /sbin/ip rule add to 69.60.161.245 table ***-jp         #判断1.1.1.1是否存在,如果不存在则将1.1.1.1加到***-jp

/home/hwei/bin/google.sh tun3 del;/home/hwei/bin/google.sh tun0 add     #调用另外一个脚本,该脚本将路由表中的出接口从tun3(***-gh)修改成tun0(***-jp)

ip route show table ***-jp|grep default || ip route add default dev tun0 table ***-jp     #判断***-jp表中有没有默认规则,没有则加上默认规则

ip route show table ***-jp|grep 197.4 || ip route add to 192.168.197.0/24 dev eth1 proto kernel scope link src 192.168.197.4 table ***-jp    #同上判断

ip route show table ***-jp|grep 196.3 || ip route add to 192.168.196.0/24 dev eth2 proto kernel scope link src 192.168.196.3 table ***-jp    #同上判断

ip route show table ***-jp|grep 198.1 || ip route add to 192.168.198.0/24 dev eth2 proto kernel scope link src 192.168.198.1 table ***-jp    #同上判断

}

#定义***-gh函数,用来将指定的ip加到***-gh中,并且删除***-jp中的ip,内容同***-jp函数,就是相应的位置做调换

function ***_gh {

for ip in $RULE_IP

do

grep $ip $RULE_JP

[ $? -eq 0 ] && ip rule del to $ip table ***-jp

grep $ip $RULE_GH

[ $? -eq 0 ] || ip rule add to $ip table ***-gh

done

[ -z "$CP" ] && /sbin/ip rule add to 69.60.161.245 table ***-gh

/home/hwei/bin/google.sh tun0 del;/home/hwei/bin/google.sh tun3 add

ip route show table ***-gh|grep default || ip route add default dev tun3 table ***-gh

ip route show table ***-gh|grep 197.4 || ip route add to 192.168.197.0/24 dev eth1 proto kernel scope link src 192.168.197.4 table ***-gh

ip route show table ***-gh|grep 196.3 || ip route add to 192.168.196.0/24 dev eth2 proto kernel scope link src 192.168.196.3 table ***-gh

ip route show table ***-gh|grep 198.1 || ip route add to 192.168.198.0/24 dev eth2 proto kernel scope link src 192.168.198.1 table ***-gh

}

#定义检测两条***连通性的函数,等

function ping_*** {

NUMBER1=$(ping -c 2 $1 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')                         #ping检测***-jp连通性,判断有没有收到回应包

NUMBER2=$(ping -c 2 $2 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')                         #ping检测***-gh连通性,判断有没有收到回应包  

ROUTE=`route -n | grep 172.217|awk '{print $8}'`                                                                                       #判断当前路由表中的出接口是tun还是tun3

IPJP=`ip rule list | awk '$3=="all" {print $7}'| awk '{if ($1=="***-gh")print "GH"}'|awk 'NR==1{print $1}'`                       #判断ip ruel list 中的规则有没有***-gh

IPGH=`ip rule list | awk '$3=="all" {print $7}'| awk '{if ($1=="***-jp")print "JP"}'|awk 'NR==1{print $1}'`                        #判断ip ruel list 中的规则有没有***-jp

}


ping_*** ${×××_JP} ${×××_GH}                  #调用ping_***函数,像函数传递两个参数,***-jp和***-gh的地址       

#判断***-jp连通性,是否需要执行***_jp函数

if [ ${NUMBER1} -ne 0 ];then                        #判断ping ***_jp是否收到回应?如果收到回应,则执行then后面的内容

if [ ${ROUTE} == "tun0" -a "$IPJP" != "GH" ];then     #继续判断路由表出接口是不是tun0(***-jp)、ip rule list是不是没有GH(***-gh),如果都满足就执行then后面的内容,不满足执行***_jp函数

[ -z "$CP" ] && /sbin/ip rule add to 69.60.161.245 table ***-jp    #判断ip rule list里有没有1.1.1.1,如果没有就加上该规则,然后退出脚本

exit 0

else

***_jp

echo "excute ***_jp-function`date +%Y-%m-%d/%R`" >>${LOG_DIR}       

exit 0

fi

fi

echo "***-jp is close try ***-gh--`date +%Y-%m-%d/%R`" >>${LOG_DIR}                 #***_jp不通的话将这段内容输出到日志中

#判断***_gh的连通性,是否需要执行***_gh函数

if [ ${NUMBER2} -ne 0 ];then                    #  #判断ping ***_gh是否收到回应?如果收到回应,则执行then后面的内容

if [ ${ROUTE} == "tun3" -a "$IPGH" != "JP" ];then      #继续判断路由表出接口是不是tun3(***-gh)、ip rule list是不是没有JP(***-jp),如果都满足就执行then后面的内容,不满足执行***_gh函数

[ -z "$CP" ] && /sbin/ip rule add to 69.60.161.245 table ***-gh     #判断ip rule list里有没有1.1.1.1,如果没有就加上该规则,然后退出脚本

exit 0

else

***_gh

echo "excute ***_gh-function`date +%Y-%m-%d/%R`" >>${LOG_DIR}

exit 0

fi

fi

echo "must to run proxy--`date +%Y-%m-%d/%R`" >>${LOG_DIR}              #如果***_gh也不通,则输出这段内容到日志中

#如果***_jp和***_gh都不通,将1.1.1.1从ip rule list中删除,并追加到日志中

ip rule del to 69.60.161.245 table ***-jp || ip rule del to 69.60.161.245 table ***-gh && echo "delete-69.60.161.245-`date +%Y-%m-%d/%R`" >>${LOG_DIR}

#清空日志文件,保留最后50行

if [ "$LINES" -gt 100 ];then           #判断当前的日志文件行数有没有超过100行,如果超过就执行then后面的内容

cd /tmp

tail -n 50 ${LOG_DIR} > route.tmp           #将日志文件最后50行重定向到route.tmp文件

mv route.tmp ${LOG_DIR}                        #将route.tmp文件覆盖回来,即保存了最后50行

exit $?

else

exit $?

fi

猜你喜欢

转载自blog.51cto.com/pkimin3/2381343