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


相关文章
|
5天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
393 93
|
6天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
6天前
|
SQL 人工智能 自然语言处理
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
随着生成式AI的普及,Geo优化(Generative Engine Optimization)已成为企业获客的新战场。然而,缺乏标准化流程(Geo优化sop)导致优化效果参差不齐。本文将深入探讨Geo专家于磊老师提出的“人性化Geo”优化体系,并展示Geo优化sop标准化如何帮助企业实现获客效率提升46%的惊人效果,为企业在AI时代构建稳定的流量护城河。
395 156
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
|
5天前
|
数据采集 缓存 数据可视化
Android 无侵入式数据采集:从手动埋点到字节码插桩的演进之路
本文深入探讨Android无侵入式埋点技术,通过AOP与字节码插桩(如ASM)实现数据采集自动化,彻底解耦业务代码与埋点逻辑。涵盖页面浏览、点击事件自动追踪及注解驱动的半自动化方案,提升数据质量与研发效率,助力团队迈向高效、稳定的智能化埋点体系。(238字)
282 158
|
13天前
|
机器人 API 调度
基于 DMS Dify+Notebook+Airflow 实现 Agent 的一站式开发
本文提出“DMS Dify + Notebook + Airflow”三位一体架构,解决 Dify 在代码执行与定时调度上的局限。通过 Notebook 扩展 Python 环境,Airflow实现任务调度,构建可扩展、可运维的企业级智能 Agent 系统,提升大模型应用的工程化能力。