Random Stuff About Stuff

Set up static routes on startup on the mac

February 26, 2016

I needed to add some static routes to only one interface on my Mac. One route for ethernet and then default to wireless.

To do it go to the directory

cd /Library/LaunchDaemons

Create a plist file here that will run a script at launch to setup our custom route

vi local.static.routes.plist

And this to the plist file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>local.static.routes</string>
    <key>Program</key>
    <string>/usr/local/bin/static</string>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/Users/David/mycommand.err</string>
    <key>StandardOutPath</key>
    <string>/Users/David/mycommand.out</string>
</dict>
</plist>

This will run the script at the given location at startup, it also sets up some logging

You now need to create the script for doing the routes So whatever location makes sense

cd /usr/local/bin/

works for me.

Create the script

vi static

Then add

#!/bin/bash

# max number of retries
retries=10
echo "Starting the script"
# include Startup commons
. /etc/rc.common

# wait for network to become available
CheckForNetwork
echo "Net status $NETWORKUP"
echo "Check for network"
while [ "$NETWORKUP" != "-YES-" ]
do
       echo "Net status $NETWORKUP"
        retries=$((retries - 1))
        if [ $retries -le 0 ] ; then
                exit 1
        fi
        sleep 2
        NETWORKUP=
        CheckForNetwork
done

echo "Trying to set the network"
networksetup -setadditionalroutes "Thunderbolt Ethernet" 192.168.210.0  255.255.255.0  192.168.208.1 192.168.215.0  255.255.255.0  192.168.208.1 192.168.216.0  255.255.255.0  192.168.208.1 192.168.217.0  255.255.255.0  192.168.208.1 192.100.200.0  255.255.255.0  192.168.208.1  192.168.230.0  255.255.255.0  192.168.208.1
exit 0

This uses the networksetup command to set the routes on a particular interface. the definition for the method is

-setadditionalroutes networkservice [dest1 mask1 gate1] [dest2 mask2 gate2] ... [destN maskN gateN]
             Use this command to set the list of IPv4 additional routes configured for the service. Each route is specified as a (destination address, subnet mask,
             gateway address) tuple. Specifying no tuples clears the list of routes.

Lots of tuples can be fun so beware the copy and paste demon.

That should be it, set the routes up as you need them.


Written by David Kerwick who lives and works Dublin as a Java Technical Lead.