MPLS雙線自動配置腳本

#!/bin/bash
#本腳本爲mpls_routetable 配置
#注意事項:
#1.服務器配置香港IP和國內IP地址,香港爲默認網關,國內IP不要配置網關,rc.local中不需要添加任何策略,如有其他策略請執行完腳本後添加。

yum -y install wget vim net-tools
mv /etc/rc.local.51idc.bak /etc/rc.local
mv /etc/iproute2/rt_tables.51idc.bak /etc/iproute2/rt_tables

input_address()
{
read -p "please input HK IP adderss:"HKADDR
read -p "please input CN IP adderss:"CNADDR
read -p "please input CN Gateway IP adderss:"cngateway
read -p "please input HK Gateway IP adderss:"hkgateway
#寫入rc.local
#ifconfig -a |egrep '^eth|^em|^en' |awk '{ print $1}' > 1.txt
#ip addr | grep inet |grep -v inet6|grep -v "127.0.0.1" | awk '{ print $2 }' |cut -d/ -f1 >2.txt
#paste 1.txt 2.txt >3.txt
ip addr | grep -w inet | grep -v 127.0.0.1 |awk '{print $NF"\t"$2}'|awk -F '/' '{print $1}' >iptab.txt
if [ cat /etc/rc.local| grep "ip route replace default " | wc -l -eq 0 ];then
cp /etc/rc.local /etc/rc.local.51idc.bak
echo "ip route replace default via $hkgateway dev cat iptab.txt |grep $HKADDR | awk '{ print $1}' " >> /etc/rc.local
echo "ip route flush table ctc" >> /etc/rc.local
echo "ip route add default via $cngateway dev $CNADDR src cat iptab.txt|grep $CNADDR |awk '{ print $1 }' table ctc" >>/etc/rc.local
echo "ip rule add from $CNADDR table ctc" >>/etc/rc.local
echo "ip route flush table HK" >>/etc/rc.local
echo "ip route add default via $hkgateway dev cat iptab.txt|grep $CNADDR |awk '{ print $1 }' src $HKADDR table HK" >> /etc/rc.local
echo "ip rule add from $HKADDR table HK" >>/etc/rc.local
else
echo "Routing table has been added to the rc.local"

fi
cp /etc/iproute2/rt_tables /etc/iproute2/rt_tables.51idc.bak
echo "252 ctc ">> /etc/iproute2/rt_tables
echo "251 HK" >> /etc/iproute2/rt_tables
sh /etc/rc.local
}

#判斷網卡IP地址歸屬地,及電信網關地址
auto_address()
{
ip addr | grep -w inet | grep -v 127.0.0.1 |awk '{print $NF"\t"$2}'|awk -F '/' '{print $1}' >iptab.txt
while read ethnet ADDR
do
AS=curl -s ipinfo.io/$ADDR/org |awk '{ print $1 }'
if [ "$AS" = "AS58879" ];then
HKADDR=$ADDR
elif [ "$AS" = "undefined" ]; then
echo "$ADDR is Private address"
else
CNADDR=$ADDR
fi
#break
done < iptab.txt
cngateway=${CNADDR%.}.1
hkgateway=${HKADDR%.
}.1

while
echo -e "\033[32m Please check and confirm CN Gateway IP address is $cngateway ? \033[0m"
read -p "(please input: y or n):" a
do
case "$a" in
y|Y|Yes|YES|yes|yES|yEs|YeS|yeS)
echo -e "\033[32m CN Gateway IP address is $cngateway \033[0m"
break
;;
n|N|No|NO|no|nO)
input_address
break
;;
*)
echo "input error,"
continue
esac
done
#寫入rc.local
#ifconfig -a |egrep '^eth|^em|^en' |awk '{ print $1}' > 1.txt
#ip addr | grep inet |grep -v inet6|grep -v "127.0.0.1" | awk '{ print $2 }' |cut -d/ -f1 >2.txt
#paste 1.txt 2.txt >3.txt

if [ cat /etc/rc.local| grep "ip route replace default " | wc -l -eq 0 ];then
cp /etc/rc.local /etc/rc.local.51idc.bak
echo "ip route replace default via $hkgateway dev cat iptab.txt |grep $HKADDR | awk '{ print $1}' " >> /etc/rc.local
echo "ip route flush table ctc" >> /etc/rc.local
echo "ip route add default via $cngateway dev cat iptab.txt|grep $CNADDR |awk '{ print $1 }' src $CNADDR table ctc" >>/etc/rc.local
echo "ip rule add from $CNADDR table ctc" >>/etc/rc.local
echo "ip route flush table HK" >>/etc/rc.local
echo "ip route add default via $hkgateway dev cat iptab.txt|grep $HKADDR |awk '{ print $1 }' src $HKADDR table HK" >> /etc/rc.local
echo "ip rule add from $HKADDR table HK" >>/etc/rc.local
else
echo "Routing table has been added to the rc.local"
fi
cp /etc/iproute2/rt_tables /etc/iproute2/rt_tables.51idc.bak
echo "252 ctc ">> /etc/iproute2/rt_tables
echo "251 HK" >> /etc/iproute2/rt_tables
sh /etc/rc.local >/dev/null 2>&1
}

curl -s ipinfo.io/114.114.114.114/org >/dev/null 2>&1

if [[ $? == 2 ]]; then
echo "nameserver 114.114.114.114
nameserver 8.8.8.8" > /etc/resolv.conf
fi
curl -s ipinfo.io/114.114.114.114/org >/dev/null 2>&1
if [[ $? == 2 ]]; then
input_address
else
auto_address
fi

#抓取最近日期路由文件
table=curl -s http://download.ks.51idc.com:8000/routetables/ |awk -F "[&lt;&gt;]" '/china_all*/{ print $13 }'|cut -c 11-18 |awk 'BEGIN {max = 0} {if ($1+0 &gt; max+0) max=$1} END {print "", max}' | awk '{ print $1}'
#生成路由表
wget http://download.ks.51idc.com:8000/routetables/china_all_$table.txt
echo "#!/bin/bash" > /etc/route.sh
for i in cat china_all_$table.txt
do
echo "route add -net $i gw $cngateway" >> /etc/route.sh
done
echo "sh /etc/route.sh" >> /etc/rc.local

sh /etc/route.sh >/dev/null 2>&1

rm -rf iptab.txt chinaall$table.txt

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章