Friday, February 4, 2011

add static route after vpn interface up on Mac OSX

to do this on mac is simply create a script and put it under /etc/ppp/ , and name it ip-up, here is my example, on script you can see I need both 10.10.20.0/24 and 10.10.10.0/24 to be routed to ppp interface after vpn interface get connected.



bash-3.2#
bash-3.2# cat /etc/ppp/ip-up
#!/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/libexec:/System/Library/CoreServices; export PATH

ifppp=$(ifconfig ppp0 | grep inet | awk '(print $2}')
/sbin/route add -net 10.10.20.0/24 $ifppp
/sbin/route add -net 10.10.10.0/24 $ifppp

bash-3.2#

4 comments:

Anonymous said...

ifppp=$(ifcanfig ppp0 | grep inet | awk '(print $2}') -> ifppp=$(ifconfig ppp0 | grep inet | awk '{print $2}')

arpol said...

thankyou for the correction...

Allan "Goldfish" Clark said...

#!/bin/sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/libexec:/System/Library/CoreServices; export PATH

#interface given as $0
#x${1} used in case ${1} is empty
case x${1} in
xppp[0-9])
#make the route on interface matching "ppp#"
/sbin/route add -net 10.10.20.0/24 -interface ${1}
/sbin/route add -net 10.10.10.0/24 -interface ${1}
;;
# otherwise, do nothing
esac

Allan "Goldfish" Clark said...

The reason I would suggest this script is that yours would activate any time the en0 or en1 LAN, Wifi) adapters came up, but would put your default route to the ppp0 anyhow (and would be silently failing if ppp0 had no inet, yielding ifppp="")

Rather:
1) check that it is ppp0 (or ppp#) that came up
2) if so, use that interface, assign defroute

-interface parameter to /sbin/route add might be recent; I thought under USL it's implicit if there's no dot in the gateway IP (meaning it's not an IP)

please combine these two comments in editing if that make sense, and my script could use some formatting (destroyed by the blog post engine)

current

last archive