峰回路转 哪来回哪去 LINUX 路由脚本 ERIKXUE
[root@ERIKXUE Desktop]# ip ru sh
0: from all lookup local
32766: from all lookup main
32767: from all lookup default
[root@ERIKXUE Desktop]#
实现流量从哪个网卡来,就从哪个网卡回去
[root@ERIKXUE Desktop]#
[root@ERIKXUE Desktop]# for i in `ip addr | awk -F: ‘{ print $2 }‘ | grep -v lo | grep -v 0c | grep -v 00 `; do echo $i; done | grep -v `route | grep default | awk -F" " ‘{print $8}‘` > /tmp/route_inside //本机除了默认路由以外的网卡
[root@ERIKXUE Desktop]# for i in `ip addr | awk -F: ‘{ print $2 }‘ | grep -v lo | grep -v 0c | grep -v 00 `; do echo $i; done | grep -v `route | grep default | awk -F" " ‘{print $8}‘` | wc -l > /tmp/number_route //统计网卡数目
[root@ERIKXUE Desktop]# w=`cat /tmp/route_inside | wc -l` //给读取的数量赋值
[root@ERIKXUE Desktop]# for p in `cat /tmp/route_inside`; //开始循环读取网卡名称(已经排除默 认路由)
> do
> TN=`echo $p | sed ‘s/[^0-9]//g‘` //截取网卡内部数字,方便后面给路由表起名字,因为 路由表只允许数字,而且有的网卡是 eth1 eth2 但有 的网卡是p2p1 p2p2 等。所以只取数字。
> ip route flush table 12$TN; 清空路由表12$TN
> ip route add default via `ifconfig $p |grep "inet addr" |cut -d: -f2 |cut -d" " -f1 | cut -d"." -f1,2,3`.254 dev $p src `ifconfig $p |grep "inet addr" |cut -d: -f2 | cut -d" " -f1` table 12$TN;
> ip rule add from `ifconfig $p |grep "inet addr" |cut -d: -f2 |cut -d" " -f1` table 12$TN; done;
[root@ERIKXUE Desktop]# rm -rf /tmp/route_inside
[root@ERIKXUE Desktop]# rm -rf /tmp/number_route
[root@ERIKXUE Desktop]# ip ru sh
0: from all lookup local
32763: from 192.123.123.1 lookup 123
32764: from 10.0.0.1 lookup 122
32765: from 192.168.100.23 lookup 120
32766: from all lookup main
32767: from all lookup default
[root@ERIKXUE Desktop]# ip route
192.168.100.0/24 dev eth0 proto kernel scope link src 192.168.100.23
192.123.123.0/24 dev eth3 proto kernel scope link src 192.123.123.1
10.0.0.0/24 dev eth2 proto kernel scope link src 10.0.0.1
172.16.0.0/16 dev eth1 proto kernel scope link src 172.16.0.3
169.254.0.0/16 dev eth0 scope link metric 1002
169.254.0.0/16 dev eth1 scope link metric 1003
169.254.0.0/16 dev eth2 scope link metric 1004
169.254.0.0/16 dev eth3 scope link metric 1005
default via 172.16.0.1 dev eth1 proto static //默认路由,不需要设置从哪来就从那回
[root@ERIKXUE Desktop]#
----------------------脚本如下----------------------
for i in `ip addr | awk -F: ‘{ print $2 }‘ | grep -v lo | grep -v 0c | grep -v 00 `; do echo $i; done | grep -v `route | grep default | awk -F" " ‘{print $8}‘` > /tmp/route_inside
for i in `ip addr | awk -F: ‘{ print $2 }‘ | grep -v lo | grep -v 0c | grep -v 00 `; do echo $i; done | grep -v `route | grep default | awk -F" " ‘{print $8}‘` | wc -l > /tmp/number_route
w=`cat /tmp/route_inside | wc -l`
for p in `cat /tmp/route_inside`;
do
TN=`echo $p | sed ‘s/[^0-9]//g‘`
ip route flush table 12$TN;
ip route add default via `ifconfig $p |grep "inet addr" |cut -d: -f2 |cut -d" " -f1 | cut -d"." -f1,2,3`.254 dev $p src `ifconfig $p |grep "inet addr" |cut -d: -f2 | cut -d" " -f1` table 12$TN;
ip rule add from `ifconfig $p |grep "inet addr" |cut -d: -f2 |cut -d" " -f1` table 12$TN; done;
rm -rf /tmp/route_inside
rm -rf /tmp/number_route
————————————备用------写了好久-----ERIKXUE------------
默认路由对于的IP: for i in `ifconfig |grep "inet addr" |cut -d: -f2 |cut -d" " -f1 | grep -v 127.0.0.1 `;do echo $i; done | grep `route | grep default | cut -d" " -f10 | cut -d"." -f1,2`
所有IP: for i in `ifconfig |grep "inet addr" |cut -d: -f2 |cut -d" " -f1 | grep -v 127.0.0.1 `;do echo $i; done;
除了默认路由对应的IP ifconfig |grep "inet addr" |cut -d: -f2 |cut -d" " -f1 | grep -v 127.0.0.1 | grep -v `route | grep default | cut -d" " -f10 | cut -d"." -f1,2`
遍历ECHO 三次 for i in `ifconfig |grep "inet addr" |cut -d: -f2 |cut -d" " -f1 | grep -v 127.0.0.1 | grep -v $a`; do echo $i ; done
IPADDR=`ifconfig eth1 |grep "inet addr" |cut -d: -f2 |cut -d" " -f1`
ip route flush table 121
ip route add default via 172.16.121.254 dev eth1 src $IPADDR table 121
ip rule add from $IPADDR table 121
route | grep default | awk -F" " ‘{print $8}‘
判断出 默认路由的网卡 route | grep default | awk -F" " ‘{print $8}‘
本机的网卡数目 ip addr | awk -F: ‘{ print $2 }‘ | grep -v lo | grep -v 0c | grep -v 00
本机除了默认路由以外的网卡
for i in `ip addr | awk -F: ‘{ print $2 }‘ | grep -v lo | grep -v 0c | grep -v 00 `; do echo $i; done | grep -v `route | grep default | awk -F" " ‘{print $8}‘` > /tmp/route_inside
统计网卡数目
for i in `ip addr | awk -F: ‘{ print $2 }‘ | grep -v lo | grep -v 0c | grep -v 00 `; do echo $i; done | grep -v `route | grep default | awk -F" " ‘{print $8}‘` | wc -l > /tmp/number_route
本文出自 “奋斗在京(ErikXUE)” 博客,请务必保留此出处http://erikxue.blog.51cto.com/3681540/1557468
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。