I bounce back and forth between work and home with a MacBook Pro. At work, I’m behind a firewall, and have to go through a web proxy to get to public internet sites. I have 2 network locations setup, home and work. When I get to work I manually set my location to work, and when I get home, I manually set it to home again.
I got tired of remembering to do this so I looked around, and found scselect.
scselect is a command line utility that comes with
OS X that lets you set your location by name
Now all I needed was a way to determine whether I was at work or home and then run scselect with the corresponding location. My work assigned ip address has a set prefix, so I can use that to say I’m at work and anything else would default to home.
# exit code is 0 if on work network and 1 otherwise ifconfig | grep -i "inet 59.33" > /dev/null
In order to prevent setting the location to the same location we need to find out
what the current location is. Running
scselect with no arguments displays a list
of locations and the active one is marked with an asterisk (*). Here’s my list.
> scselect Defined sets include: (* == current set) 575844ED-8466-479C-9567-3F0B7D767EE9 (home) 3DF4B8B9-2E92-4F61-B684-74E0D0D38DEE (Automatic) * 6064213B-532D-43C9-8941-DC72B6487955 (work)
The output is a bit messy, and we need to parse out the active location.
#!/usr/bin/env ruby # example output: * 6064213B-532D-43C9-8941-DC72B6487955 (work) output = `scselect 2>&1 | grep ' \\* '` # parse out the location. location = output.scan(/\(.*\)/).first.gsub(/[\(\)]/, "") # location = work
We now compare the active location with the location our ip address tells us. If different,
scselect with the new location.
Complete script is available here
Twitter • Reddit