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


相关文章
|
3月前
|
网络协议 定位技术 网络架构
路由协议——直连路由、静态路由、缺省路由、路由优先级和路由度量、路由冗余和备份(浮动静态路由)
路由协议——直连路由、静态路由、缺省路由、路由优先级和路由度量、路由冗余和备份(浮动静态路由)
96 2
|
3天前
|
网络协议 网络架构
ensp中默认路由和静态路由实验
默认路由的作用是将无法匹配路由表中其他路由表项的数据包转发到指定下一跳路由器。在实际网络中,默认路由通常用于简化路由配置,通常在网络边缘的路由器上配置 静态路由的作用是将特定网络的数据包转发到指定下一跳路由器。在实际网络中,静态路由通常用于更精细地控制数据包的转发,通常在网络核心路由器上配置。
ensp中默认路由和静态路由实验
|
7月前
|
网络架构
交换机与路由器技术-27-OSPF路由重分发
交换机与路由器技术-27-OSPF路由重分发
29 0
交换机与路由器技术-27-OSPF路由重分发
|
10月前
|
网络虚拟化
华为eNSP网络基础,综合练习二(vlan+mstp+vlanif+静态路由+默认路由)
华为eNSP网络基础,综合练习二(vlan+mstp+vlanif+静态路由+默认路由)
320 1
|
网络协议 安全 算法
动态主机配置协议(DHCPv4)的无类静态路由选项
IP 协议 [1] 使用路由器将数据包从连接到一个 IP 子网的主机传输到连接到不同 IP 子网的主机。当 IP 主机(源主机)希望将数据包传输到另一台 IP 主机(目的地)时,它会查阅其路由表以确定应该用于将数据包转发到目标主机的路由器的 IP 地址。
214 0
动态主机配置协议(DHCPv4)的无类静态路由选项
【驱动问题】双网卡路由规则配置
【驱动问题】双网卡路由规则配置
103 0
【驱动问题】双网卡路由规则配置
|
网络协议 网络架构
Cisco之路由重分发和配置NAT
在一个大型网络中可能存在着多种路由协议,因此关系到路由重分发的问题。网络架构如下图所示:架构说明1 R1为总公司路由器;2 R2、R5为上海分公司路由器;3 R3、R4为杭州分公司路由器;4 总公司和分公司之间使用OSPF协议,上海分公司使用RIP协议,而杭州分公司使用静态路由协议;5 所有分公司访问公网都通过总公司路由器R1实现;6 本地所带主机由Loopback1接口模拟;7 Loopback0使用192.168.255.0/24网段并且作为Router ID;一、配置基本信息1.R1配置R1(config)#hostname R1R1(config)#intR1(config-if)#n
Cisco之路由重分发和配置NAT
|
测试技术 网络架构
高速通道-物理专线-将冗余双线静态路由协议平滑切换到BGP协议的方式
*很多混合云用户已经通过高速通道完成静态路由的混合云对接。当前高速通道产品已经支持eBGP动态路由协议,通过此文档协助用户在不影响业务的情况下完成BGP改造。文档目的,静态改造为内网BGP且不断网不丢包。
2885 0

热门文章

最新文章