Skip to content

Instantly share code, notes, and snippets.

@Phineas
Created May 3, 2023 11:10
Show Gist options
  • Save Phineas/3c1a36f35b4e3aefe344ce26ab0bcef9 to your computer and use it in GitHub Desktop.
Save Phineas/3c1a36f35b4e3aefe344ce26ab0bcef9 to your computer and use it in GitHub Desktop.
#!/bin/bash
declare -A MODEMS
MODEMS=(["alpha"]="192.168.12.100" ["beta"]="192.168.10.100" ["delta"]="192.168.9.100" ["omega"]="192.168.11.100")
for modem_name in "${!MODEMS[@]}"; do
ip_address="${MODEMS[$modem_name]}"
IFACE=$(ip -o addr show | grep -w ${ip_address} | awk '{print $2}')
if [ -z "$IFACE" ]; then continue; fi
GATEWAY=$(ip route show default dev $IFACE | awk '{print $3}')
echo "$modem_name ($ip_address) gw: $GATEWAY, if: $IFACE"
rules=$(ip rule list | grep "lookup $modem_name" | awk '{print substr($1, 1, length($1)-1)}')
for priority in $rules; do
ip rule del pref $priority table $modem_name
done
ip rule add from $ip_address table $modem_name
ip route flush table $modem_name
ip route add default via $GATEWAY dev $IFACE table $modem_name
done
ip route flush cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment