Skip to content

Instantly share code, notes, and snippets.

@logan2211
Created May 26, 2016 12:43
Show Gist options
  • Save logan2211/7facf8d9ed387e6366026d19ff857fa5 to your computer and use it in GitHub Desktop.
Save logan2211/7facf8d9ed387e6366026d19ff857fa5 to your computer and use it in GitHub Desktop.
#!/bin/sh -e
#this script requires a routing table named $IFACE (ie. bond0) exists in /etc/iproute2/rt_tables
#the $IFACE routing table is used to place the default route in for the source routing table
#ip route list table $IFACE will list the routing table for this interface
set_netinfo() {
IPADDR=$(ifconfig "$IFACE" | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
NETMASK=$(ifconfig "$IFACE" | grep 'inet addr:' | cut -d: -f4 | awk '{ print $1}')
OLDIFS=$IFS
IFS=.
set -- $IPADDR
local IPADDR1=$1
local IPADDR2=$2
local IPADDR3=$3
local IPADDR4=$4
set -- $NETMASK
local NETMASK1=$1
local NETMASK2=$2
local NETMASK3=$3
local NETMASK4=$4
IFS=$OLDIFS
NETWORK=$(printf "%d.%d.%d.%d\n" "$((IPADDR1 & NETMASK1))" "$((IPADDR2 & NETMASK2))" "$((IPADDR3 & NETMASK3))" "$((IPADDR4 & NETMASK4))")
GATEWAY=$(printf "%d.%d.%d.%d\n" "$((IPADDR1 & NETMASK1))" "$((IPADDR2 & NETMASK2))" "$((IPADDR3 & NETMASK3))" "$(((IPADDR4 & NETMASK4)+1))")
}
set_netinfo
ip route flush table "$IFACE"
ip route add "$NETWORK/$NETMASK" dev "$IFACE" proto kernel scope link table "$IFACE"
ip route add default via "$GATEWAY" dev "$IFACE" table "$IFACE"
ip rule del lookup "$IFACE" || true
ip rule add from "$NETWORK/$NETMASK" lookup "$IFACE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment