Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created August 10, 2019 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hermann-SW/5641a051cfca5ef6e26adb83b2900cfa to your computer and use it in GitHub Desktop.
Save Hermann-SW/5641a051cfca5ef6e26adb83b2900cfa to your computer and use it in GitHub Desktop.
Reroute traffic for eg. eth0 IP address so that traffic goes through gateway instead of localhost (based on https://unix.stackexchange.com/a/128428 with one correction, one additon, and automatic determination of ip and gw; start eg. with "sudo ./local2gw eth0")
#!/bin/bash
if=$1
gw=`ip route show | grep "^default" | grep $if | cut -f3 -d\ `
vip=`ip route show table local | grep ^local | grep $if | cut -f2 -d\ `
# from https://unix.stackexchange.com/a/128428
ip route add local $vip dev lo table 10 proto kernel scope host
ip rule add to $vip lookup 10 prio 1
#
# ip route del local $vip dev lo table local
ip route del table local $vip
#
ip rule add to $vip iif lo lookup main prio 0
route add $vip gw $gw dev $if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment