Persistent Static routes - macOS High Sierra

Problem:

I've got a mac at work. It's connected to two physically separate networks, one of which has a default route, but the other needs routes when the adapter is plugged in.

Surprisingly, there's little information on this.

Solution:

It's actually pretty simple. The command is as follows:

sudo networksetup -setadditionalroutes <adapter name> <network addr> <netmask> <gateway>

The <adapter name> section is a little strange, but this is easy as well. To obtain it (or change it to something easy) navigate to System Preferences -> Network -> Click the adapter on the left -> Gear icon (bottom of the list of adapters) -> Rename service. This allows you to either copy the existing name, or rename it!

If you want additional routes, you CANNOT simply run multiple instances of that command as it overwrites the entries each time, you must bundle all routes in like so:

sudo networksetup -setadditionalroutes <adapter name> <network addr 1> <netmask 1> <gateway> <network addr 2> <netmask 2> <gateway> <network addr 3> <netmask 3> <gateway>

These routes appear to be persistent on reboot - although I did have an issue of this command not working at all until I rebooted! (But my machine takes a lot of abuse).

It's worth noting that you can verify the routing table with netstat -rn to find the routing entries have actually gone in.

If you have a lot of networks you can use standard bash backslash format to separate the lines like so:

sudo networksetup -setadditionalroutes LAB \
1.1.0.0 255.255.0.0 1.0.0.254 \
1.2.0.0 255.255.0.0 1.0.0.254 \
1.3.0.0 255.255.0.0 1.0.0.254 \
1.4.0.0 255.255.0.0 1.0.0.254 \
1.5.0.0 255.255.0.0 1.0.0.254 \
1.6.0.0 255.255.0.0 1.0.0.254 \
2.1.0.0 255.255.0.0 1.0.0.254 \
2.2.0.0 255.255.0.0 1.0.0.254 \
2.3.0.0 255.255.0.0 1.0.0.254 \
2.4.0.0 255.255.0.0 1.0.0.254 \
2.5.0.0 255.255.0.0 1.0.0.254 \
2.6.0.0 255.255.0.0 1.0.0.254 \
3.1.0.0 255.255.0.0 1.0.0.254 \
3.2.0.0 255.255.0.0 1.0.0.254 \
3.3.0.0 255.255.0.0 1.0.0.254 \
3.4.0.0 255.255.0.0 1.0.0.254 \
3.5.0.0 255.255.0.0 1.0.0.254 \
3.6.0.0 255.255.0.0 1.0.0.254 \
4.1.0.0 255.255.0.0 1.0.0.254 \
4.2.0.0 255.255.0.0 1.0.0.254 \
4.3.0.0 255.255.0.0 1.0.0.254 \
4.4.0.0 255.255.0.0 1.0.0.254 \
4.5.0.0 255.255.0.0 1.0.0.254 \
4.6.0.0 255.255.0.0 1.0.0.254

That's it! Let's hope it doesn't change too much in future...