免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1646 | 回复: 0
打印 上一主题 下一主题

[网络相关] CENTOS下面多外线路由策略应用测试脚本! [复制链接]

论坛徽章:
2
羊年新春福章
日期:2015-04-27 16:56:53射手座
日期:2015-04-27 16:58:18
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-05-22 16:14 |只看该作者 |倒序浏览
[ 本帖最后由 古丁高手 于 2015-05-22 16:34 编辑 ]

环境:
OS:CENTOS6.5
版本:x64
多外线:电信、联通、移动

写了个粗劣的脚本,添加策略路由、默认网关和明细路由条目。大神可以给点意见修改一下。
#!/bin/bash
#################################
#autor:xwy                                          #
#date:v1.0                                               #               
#date:2015.05.22                                                #
#################################
#1.添加默认网关;
if [ -f /etc/iproute2/rt_tables ] ;then

        echo "150        EDU" >>/etc/iproute2/rt_tables
        echo "160        YD" >>/etc/iproute2/rt_tables
        echo "170        LT" >>/etc/iproute2/rt_tables
        echo "180        DX" >>/etc/iproute2/rt_tables
        echo "190        CK" >>/etc/iproute2/rt_tables
else
        echo '/etc/iproute2/rt_tables is not exit! warning!'
        exit 1
fi
sleep 1
#2.查出有多少张网卡
#网卡张数:ifconfig |awk '{print $1}'|grep -E 'eth|em'
#网卡ip地址:ip=`ifconfig a | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`
#设置array_eth和arry_ip数组代表网卡和对应的IP
echo '###########################################'
echo "The localhost Apapter and IP is as flow:"
declare -a array_eth array_ip
i=1
for eth in `ifconfig |awk '{print $1}'|grep -E 'eth|em'|cat`
do       
        array_eth[i]="$eth"
        ip=`ifconfig $eth | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`
        array_ip[i]="$ip"
        echo "NO.$i        ${array_eth[i]}        ${array_ip[i]}"

        i=`expr $i + 1`
done
echo '###########################################'
#3.用户交付
j=1
while [ $j -le ${#array_eth[@]} ]
do
echo ""
echo '###########################################'
echo 'Be carefule to confirm the flowing:'
echo "${array_eth[$j]}        ${array_ip[$j]}"
echo "###########################################"
echo  'This eth line is: DX/LT/YD/CK? Please input:'
read sp
case $sp in
        DX) echo 'Please input DX Gateway:'
        read dx
        ip route flush table DX
        ip route add default via $dx table DX
        ip rule add from ${array_ip[$j]}/32 table DX
        if [ -f ./dx.ips ];then
                for ip1 in `cat ./dx.ips`
                do
                        echo $ip1 via $dx >> /etc/sysconfig/network-scripts/route-${array_eth[$j]}
                done
        else
                echo 'dx.ips file is not exit!'
                exit 1
        fi
        ping -I${array_eth[$j]} baidu.com -c 4
        if [ $? -ne 0 ];then
                echo "Can not access Internet from ${array_eth[$j]} please check it now!"       
                exit 1
        else
                echo 'The DX ip route input is OK!'
        fi
        sleep 1
        ;;
        LT) echo echo 'Please input LT Gateway:'
        read lt
        ip route flush table LT
        ip route add default via $lt table LT
        ip rule add from ${array_ip[$j]}/32 table DX
        if [ -f ./lt.ips ];then
                for ip2 in `cat ./lt.ips`
                do
                        echo $ip2 via $lt >> /etc/sysconfig/network-scripts/route-${array_eth[$j]}
                done
        else
                echo 'lt.ips file is not exit!'
                exit 1
        fi
        ping -I${array_eth[$j]} baidu.com -c 4
        if [ $? -ne 0 ];then
                echo "Can not access Internet from ${array_eth[$j]} please check it now!"
                exit 1
        else
                echo 'The LT ip route input is OK!'
        fi
        sleep 1
        ;;
        YD) echo "Please input YD Gateway:"
        read yd
        ip route flush table YD
        ip route add default via $yd table YD
        ip rule add from ${array_ip[$j]}/32 table YD
        if [ -f ./yd.ips ];then
                for ip3 in `cat ./yd.ips`
                do
                        echo $ip3 via $yd >> /etc/sysconfig/network-scripts/route-${array_eth[$j]}
                done
        else
                echo 'yd.ips file is not exit!'
                exit 1
        fi
        echo 'The YD ip route input is OK!'
        sleep 1
        ;;
        CK) echo "Please input CK Gateway:"
        read ck
        ip route flush table CK
        ip route add default via $ck table CK
        ip rule add from ${array_ip[$j]}/32 table CK
        if [ -f ./CK.ips ];then
                for ip4 in `cat ./lt.ips`
                do
                        echo $ip4 via $ck >> /etc/sysconfig/network-scripts/route-${array_eth[$j]}
                done
        else
                echo 'ck.ips file is not exit!'
                exit 1
        fi
        ping -I${array_eth[$j]} baidu.com -c 4
        if [ $? -ne 0 ];then
                echo "Can not access Internet from ${array_eth[$j]} please check it now!"
                exit 1
        else
                echo 'The CK ip route input is OK!'
        fi
        sleep 1
        ;;
        *) echo "usage:input[DX|LT|YD|CK]"
        exit 1
        ;;
esac
j=`expr $j + 1`
done

service network restart
if [ $? -ne 0 ];then
echo 'The script is running wrong,please check it agin!'
exit 1
else
echo 'ALL the ip route input setting is OK!'
fi
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP