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


本文转自 Bill_Xing 51CTO博客,原文链接:http://blog.51cto.com/zhanx/2044893


相关文章
|
8月前
|
网络协议 定位技术 网络架构
路由协议——直连路由、静态路由、缺省路由、路由优先级和路由度量、路由冗余和备份(浮动静态路由)
路由协议——直连路由、静态路由、缺省路由、路由优先级和路由度量、路由冗余和备份(浮动静态路由)
728 2
|
8月前
|
网络协议 数据库 网络架构
ENSP中OSPF多区域管理 (原理和配置)
OSPF 多区域的主要作用是缩小链路状态数据库和路由表的规模,减少路由更新的频率,提高网络的可扩展性,实现路由过滤和路由汇总,从而提高网络的性能、稳定性、安全性和可管理性。OSPF 多区域的主要作用如下ABR的摘要 配置命令
341 0
ENSP中OSPF多区域管理 (原理和配置)
|
网络协议 安全 算法
动态主机配置协议(DHCPv4)的无类静态路由选项
IP 协议 [1] 使用路由器将数据包从连接到一个 IP 子网的主机传输到连接到不同 IP 子网的主机。当 IP 主机(源主机)希望将数据包传输到另一台 IP 主机(目的地)时,它会查阅其路由表以确定应该用于将数据包转发到目标主机的路由器的 IP 地址。
345 0
|
网络协议 安全 网络虚拟化
华为数通HCIA小型拓扑综合实验,运用OSPF动态路由协议、ACL访问控制列表,交换机生成树协议,修改交换机根桥、交换机划分vlan、链路聚合等相关数通技术、NAT地址转换以及NAT网络地址转换的配置
华为数通HCIA小型拓扑综合实验,运用OSPF动态路由协议、ACL访问控制列表,交换机生成树协议,修改交换机根桥、交换机划分vlan、链路聚合等相关数通技术、NAT地址转换以及NAT网络地址转换的配置、ACL访问控制列表的配置方法。访问控制列表的书写技巧、ACL访问控制列表的匹配原则。...
华为数通HCIA小型拓扑综合实验,运用OSPF动态路由协议、ACL访问控制列表,交换机生成树协议,修改交换机根桥、交换机划分vlan、链路聚合等相关数通技术、NAT地址转换以及NAT网络地址转换的配置
|
网络安全 网络虚拟化 Unix