Which way back and forth? LINUX Routing Script Erikxue

Source: Internet
Author: User

[[email protected] desktop]# IP ru sh

0:from all lookup local

32766:from all lookup main

32767:from all lookup default

[Email protected] desktop]#

To realize the traffic from which network card, from which Nic back

[Email protected] desktop]#

[[email protected] desktop]# for i in ' IP addr | awk-f: ' {print $} ' | grep-v lo | grep-v 0c | grep-v xx '; do echo $i; Done | Grep-v ' route | grep Default | Awk-f "" ' {print $8} ' >/tmp/route_inside //natively network card except for default route

[[email protected] desktop]# for i in ' IP addr | awk-f: ' {print $} ' | grep-v lo | grep-v 0c | grep-v xx '; do echo $i; Done | Grep-v ' route | grep Default | Awk-f "" ' {print $8} ' | Wc-l >/tmp/number_route //statistics NIC number

[[email protected] desktop]# w= ' cat/tmp/route_inside | wc-l ' //Assign a value to the number of reads

[[email protected] desktop]# for p in ' cat/tmp/route_inside '; Start loop to read NIC name (default route already excluded)

> do

> tn= ' echo $p | sed ' s/[^0-9]//g '//intercept the internal number of the NIC, so that it is easy to name the routing table later because The routing table only allows numbers, and some network cards are eth1 eth2 but some network cards are p2p1 p2p2 and so on. So just take the numbers.

> IP route flush table 12$tn; emptying the routing 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

[Email protected] desktop]# rm-rf/tmp/route_inside

[Email protected] desktop]# rm-rf/tmp/number_route

[[email protected] 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

[[Email protected] 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//defaults route, no settings from where to come from

[Email protected] desktop]#



The----------------------script is as follows----------------------

For i in ' IP addr | Awk-f: ' {print $} ' | 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 $} ' | 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



———————————— spare------wrote for a long time-----erikxue------------

Default route for Ip:for i in ' ifconfig |grep "inet addr" |cut-d:-f2 |cut-d ""-f1 | Grep-v 127.0.0.1 ';d o echo $i; Done | grep ' route | grep Default | Cut-d ""-F10 | Cut-d "."-f1,2 '


All ip:for i in ' ifconfig |grep "inet addr" |cut-d:-f2 |cut-d ""-f1 | Grep-v 127.0.0.1 ';d o echo $i; Done



In addition to the default route for the 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 '



Traverse Echo three times 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} '




Determine the default route of the network card route | grep Default | Awk-f "" ' {print $8} '


Number of NICs on this machine IP addr | Awk-f: ' {print $} ' | Grep-v Lo | Grep-v 0c | grep-v 00



This machine has a network card other than the default route

For i in ' IP addr | Awk-f: ' {print $} ' | 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


Count the number of network cards

For i in ' IP addr | Awk-f: ' {print $} ' | 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


This article is from the "Struggle in Beijing (Erikxue)" blog, please be sure to keep this source http://erikxue.blog.51cto.com/3681540/1557468

Which way back and forth? LINUX Routing Script Erikxue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.