- 论坛徽章:
- 2
|
[ 本帖最后由 古丁高手 于 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 |
|