Created
August 10, 2019 21:05
-
-
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")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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