Skip to content

Instantly share code, notes, and snippets.

@loupzeur
Created August 24, 2021 14:09
Show Gist options
  • Save loupzeur/6b29d0aa5598c933a4070cd3509c3559 to your computer and use it in GitHub Desktop.
Save loupzeur/6b29d0aa5598c933a4070cd3509c3559 to your computer and use it in GitHub Desktop.
Generate rule table for all non l interface having an ip address (for mptcp support)
#!/usr/bin/bash
IP=$(ip addr | awk '
/^[0-9]+:/ {
sub(/:/,"",$2); iface=$2 }
/^[[:space:]]*inet / {
split($2, a, "/")
print iface" "a[1]
}'|egrep -v ^lo)
IFS=$'\n'
count=0
for i in $IP
do
IFS=' '
INT=($i)
count=$((count+1))
LAST_IP=${INT[1]}
LAST_IN=${INT[0]}
LAST_GW=$(ip route|grep ${INT[0]} |grep default|cut -f3 -d' ')
echo "ip rule add from ${INT[1]} table $count"
echo "ip route add $(ip route|grep ${INT[0]}|grep src|cut -d' ' -f1) dev ${INT[0]} scope link table $count"
echo "ip route add default via $LAST_GW dev ${INT[0]} table $count"
done
echo "ip route add default scope global nexthop via $LAST_GW dev $LAST_IN"
@loupzeur
Copy link
Author

Another script to do the same (adding rule and route for table)

#!/bin/bash
#echo 101 adsl1 >> /etc/iproute2/rt_tables
#echo 102 adsl2 >> /etc/iproute2/rt_tables
createRule(){
    if [ $# -ne 2 ]; then
        echo "Missing parameters"
        exit
    fi
    DEV=$1
    TABLE=$2
    NET=$(ip route|grep $DEV|grep src|egrep -o [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]+)
    ROUTE=$(ip route |grep default|grep $DEV|egrep -o [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)

    ip rule add from $NET table $TABLE
    ip route add $NET dev $DEV scope link table $TABLE
    ip route add default via $ROUTE dev $DEV table $TABLE
}

getAllDefault(){
    count=1
    for i in $(ip route|grep default|egrep -o "dev [a-Z0-9]+"|cut -d" " -f2);do
        createRule $i adsl$count
        count=$((count+1))
    done
}

getAllDefault

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment