Linux 运维----第一个shell脚本_系统/运维

                                                                                                                                                                                                                                                                                    环境需求

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

脚本如下所示:

#!/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                                               #vpn-jp的地址

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

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

export RULE_IP                                                            

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

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

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

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

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

export CP

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

function vpn_jp {

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

do

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

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

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

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

done

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

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

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

ip route show table vpn-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 vpn-jp    #同上判断

ip route show table vpn-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 vpn-jp    #同上判断

ip route show table vpn-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 vpn-jp    #同上判断

}

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

function vpn_gh {

for ip in $RULE_IP

do

grep $ip $RULE_JP

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

grep $ip $RULE_GH

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

done

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

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

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

ip route show table vpn-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 vpn-gh

ip route show table vpn-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 vpn-gh

ip route show table vpn-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 vpn-gh

}

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

function ping_vpn {

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

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

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

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

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

}


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

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

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

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

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

exit 0

else

vpn_jp

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

exit 0

fi

fi

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

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

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

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

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

exit 0

else

vpn_gh

echo "excute vpn_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}              #如果vpn_gh也不通,则输出这段内容到日志中

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

ip rule del to 69.60.161.245 table vpn-jp || ip rule del to 69.60.161.245 table vpn-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