Skip to content

Instantly share code, notes, and snippets.

@e0da
Created November 25, 2012 00:15
Show Gist options
  • Save e0da/4141904 to your computer and use it in GitHub Desktop.
Save e0da/4141904 to your computer and use it in GitHub Desktop.
#!/bin/sh
# credit: http://kindlund.wordpress.com/2007/11/19/configuring-multiple-default-routes-in-linux/ "Configuring Multiple Default Routes in Linux"
start() {
start_iface
start_vlans
start_addresses
start_routes
configure_resolv
}
stop() {
stop_routes
stop_addresses
stop_vlans
stop_iface
}
restart() {
stop
start
}
start_iface() {
ip link set eth0 up
}
stop_iface() {
ip link set eth0 down
}
start_vlans() {
vconfig add eth0 10
ip link set eth0.10 up
vconfig add eth0 20
ip link set eth0.20 up
vconfig add eth0 30
ip link set eth0.30 up
}
stop_vlans() {
ip link set eth0.10 down
vconfig rem eth0.10
ip link set eth0.20 down
vconfig rem eth0.20
ip link set eth0.30 down
vconfig rem eth0.30
}
start_addresses() {
ip address add 10.1.1.10/24 dev eth0
ip address add 10.2.2.20/24 dev eth0.10
ip address add 10.3.3.30/24 dev eth0.20
ip address add 10.4.4.40/24 dev eth0.30
}
stop_addresses() {
ip address flush eth0
ip address flush eth0.10
ip address flush eth0.20
ip address flush eth0.30
}
start_routes() {
rt_tables=/etc/iproute2/rt_tables
echo '255 local' > $rt_tables
echo '1 main' >> $rt_tables
echo '253 default' >> $rt_tables
echo '0 unspec' >> $rt_tables
# Define tables
echo '1 rt1' >> $rt_tables
ip route add 10.1.1.0/24 dev eth0 src 10.1.1.10 table rt1
ip route add default via 10.1.1.1 dev eth0 table rt1
ip rule add from 10.1.1.10/32 table rt1
ip rule add to 10.1.1.10/32 table rt1
echo '2 rt2' >> $rt_tables
ip route add 10.2.2.0/24 dev eth0.10 src 10.2.2.20 table rt2
ip route add default via 10.2.2.1 dev eth0.10 table rt2
ip rule add from 10.2.2.20/32 table rt2
ip rule add to 10.2.2.20/32 table rt2
echo '3 rt3' >> $rt_tables
ip route add 10.3.3.0/24 dev eth0.20 src 10.3.3.30 table rt3
ip route add default via 10.3.3.1 dev eth0.20 table rt3
ip rule add from 10.3.3.30/32 table rt3
ip rule add to 10.3.3.30/32 table rt3
echo '4 rt4' >> $rt_tables
ip route add 10.4.4.0/24 dev eth0.30 src 10.4.4.40 table rt4
ip route add default via 10.4.4.1 dev eth0.30 table rt4
ip rule add from 10.4.4.40/32 table rt4
ip rule add to 10.4.4.40/32 table rt4
ip route add default via 10.1.1.1
}
stop_routes() {
ip route flush all
ip rule delete from 10.1.1.10
ip rule delete to 10.1.1.10
ip rule delete from 10.2.2.20
ip rule delete to 10.2.2.20
ip rule delete from 10.3.3.30
ip rule delete to 10.3.3.30
ip rule delete from 10.4.4.40
ip rule delete to 10.4.4.40
}
configure_resolv() {
resolv=/etc/resolv.conf
echo 'domain local' > $resolv
echo 'search local' >> $resolv
echo 'nameserver 10.1.1.1' >> $resolv
echo 'nameserver 8.8.8.8' >> $resolv
}
case "$*" in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
restart
;;
*)
echo "Usage: $0 {start|stop|restart}"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment