Like many consultants, I am working at an enterprise client location behind the enterprise Proxy and an internal restricted wireless network
Frequently, I had to access certain websites and services that are disallowed within the enterprise proxy. Fortunately Mac allows us to create multiple Network Locations and configure proxy and preferred wireless network for a specific Location. This means one would expect that when you switch the location, the wifi network of choice configured would also be switched, but guess what, it turns out that, Mac does not switch wireless network as per the location…don’t ask me why.
Initially, like any geek, I tried complicated approaches like scripting the whole thing via bash/ruby scripts something like this..
#!/usr/bin/ruby $network_interface = 'en0' $home_network = {location: 'Home', wifi: 'MyHomeWifi'} $office_network = {location: 'Office', wifi: 'entepriseWifi'} $guest_network = {location: 'Guest', wifi: 'ExternalGuest'} def switch_to(network) `networksetup -switchtolocation #{network[:location]}` `networksetup -setairportnetwork #{$network_interface} #{network[:wifi]}` end def go_office switch_to $office_network end def go_home switch_to $home_network end def go_guest switch_to $guest_network end go_office
But, the problem with this is, when I use networksetup to switch location, I am prompted to enter the sudo credentials which defeats the purpose of reducing the steps to switch between locations easily. So I gave up this approach.
All these years, I have not been a fan of using Automator app that comes up with MacOS. Every now and then I try to use it for some mini automation of some mundane tasks that I end up doing in my mac, but the experience has always been painful. It was no different this time as well, but at this time I felt it was worth it 🙂 . And here am, thought I should document the steps if any one wants to venture into this.
- Open Automator app, and create a New Document, you will be prompted to choose a template, choose Service
- Select the Utilities section under Library
- Double click on the Run Shell Script item
- On the Run Shell Script window, overwrite any sample script present in the text area with the following commands
scselect networksetup -setairportnetwork en0
In the above script, there are two commands, scselect is to switch location and networksetup -setairportnetwork is for selecting the wifi network. Please note, en0 is the network interface, which in most cases would be en0 but you can find this out by running ifconfig in your terminal window to see which is your wireless network interface.
- Double click on the Display Notification item from the Utilities section. Add a good title and message so that you get a notification when the network switch is completed.
- Please ensure you select no input on the top of the script window where you see a drop down next to the text Service receives. See the above image for reference.
- Save this script with a meaningful name.
- Now, lets add a simple Keyboard shortcut for this service.
- Open System References->Keyboard. Go straight to Shortcuts tab.
- Select Services section in the left section. Scroll down the section on the right and go all the way down to see a section called General.
- You should find your newly added service here. Go ahead and add your keyboard shortcut.
- Thats it, you have now automated scripting the location switching steps.
- Repeat these steps for configuring another location, now you can switch between these locations at ease using your keyboard shortcuts. Have fun.