Skip to content

Instantly share code, notes, and snippets.

@GabLeRoux
Last active January 9, 2025 14:36
Show Gist options
  • Save GabLeRoux/c7d4c9046d9b5ec7bce822426613912a to your computer and use it in GitHub Desktop.
Save GabLeRoux/c7d4c9046d9b5ec7bce822426613912a to your computer and use it in GitHub Desktop.

Attempt to manage vpn routing table with /etc/ppp/ip-up

Related Stackoverflow Question:
https://superuser.com/a/206826/55267

Related answer:
https://superuser.com/a/206826/55267

# setup
sudo touch /etc/ppp/ip-up
sudo vim /etc/ppp/ip-up # fill the script
sudo chmod +x /etc/ppp/ip-up
# tail the logs
sudo tail -f /var/log/ip-up.log

Tested on MacOS 10.13 and didn't work for me. Script doesn't get called.
This works, shebang was wrong. Thanks to athairus and Mark Gaensicke in the comments. ๐Ÿ™Œ

References

Edits

  • 2022-11-02: I've updated the script to reflect suggested changes in comments and I've added logging of the command
#!/bin/sh
now=`date +%Y-%m-%d_%Hh%Mm%Ss`
logfile=/var/log/ip-up.log
echo "$0 called at $now with following params:" >> $logfile
echo "The VPN interface (e.g. ppp0): $1" >> $logfile
echo "Unknown, was 0, in my case: $2" >> $logfile
echo "IP of the VPN server: $3" >> $logfile
echo "VPN gateway address: $4" >> $logfile
echo "Regular (non-vpn) gateway for your lan connections: $5" >> $logfile
# Add 192.168.0.0 to 192.168.0.255 range to routing table on VPN interface
/sbin/route add -net 192.168.0.0/16 -interface $1 >> $logfile 2>&1
@algal
Copy link

algal commented Sep 8, 2018

With this fix, it works for me on macOS 10.13.16, I needed to update the command to this for my purposes:

/sbin/route add -host 192.168.1.100 -interface $1

@markgaensicke
Copy link

First line should be:

#!/bin/sh

instead of #/bin/sh. Without correct Shebang the script will not be executed.

@cptfixit
Copy link

cptfixit commented Nov 2, 2022

The script still isn't right :)

You wrote:
#/sbin/route add 192.168.0.0/16 -interface $1

The actual call to route should be:
/sbin/route add -net 192.168.0.0/16 -interface $1

Note that:

  • Your original made it a comment (by starting with #)
  • Without -net it becomes a route for a single address, not for a network.

@GabLeRoux
Copy link
Author

Thanks I've updated the gist accordingly ๐Ÿ‘

@tuergeist
Copy link

tuergeist commented Jan 9, 2025

@GabLeRoux It's much easier nowadays.
As user run networksetup -listnetworkserviceorder to find out the name of the Network.

Run this to permanently add 172.20.20.0/24 with gateway 172.20.19.1
It's save to leave out the gateway :)

networksetup -setadditionalroutes "network name" 172.20.20.0 255.255.255.0 172.17.19.1

No need to rerun it :)

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