10.6: Switch between two wireless networks in one location

Oct 07, '09 07:30:03AM

Contributed by: richardmilford

For one reason or another, I have two wireless networks available to me to use in one location. One is DHCP enabled, while the other requires manual setup.

The obvious solution for switching between these networks is to use the Locations feature in Networking to save two profiles, and then use the drop-down Location menu to switch between them. However, I have found that because my MacBook has the logins for both networks saved, it does not switch the wireless network, just the settings. Once you mix DHCP with manual settings, it just fails to work, and requires more intervention.

My solution (admittedly inelegant, but I don't know how to implement a better way), was to create a shell script that will switch the locations, and power cycle the AirPort card (which means it picks up the 'preferred' network). This is then set as a Service using Automator, and assigned a keyboard shortcut. I have also had to configure sudo to allow my user to run it without a password.

Here's the script, which I saved in my home folder and named AirPortReset:

#!/bin/ksh

# Reset AirPort location & reset AirPort

loc1=network1
loc2=network2
loc3=Automatic

# Check which Location you are in

currloc=`/usr/sbin/networksetup -getcurrentlocation`

# Change to the other location

if [ $1 = auto ]; then
    /usr/sbin/networksetup -switchtolocation $loc3
    /usr/sbin/networksetup -setairportpower en1 off
    /usr/sbin/networksetup -setairportpower en1 on
    exit 0

else
    break
fi

if [ $currloc = $loc1 ]; then
    /usr/sbin/networksetup -switchtolocation $loc2
    /usr/sbin/networksetup -setairportpower en1 off
    /usr/sbin/networksetup -setairportpower en1 on 

else
    /usr/sbin/networksetup -switchtolocation $loc1
    /usr/sbin/networksetup -setairportpower en1 off
    /usr/sbin/networksetup -setairportpower en1 on
fi

exit 0
In the /etc/sudoers file, I made the following entries:
User_Alias      NET = Richard
Cmnd_Alias      LOC = /Users/Richard/*
NET     ALL=NOPASSWD: LOC
There are two Automator Services to set up (one to cycle between network one and two, and one to reset to the Automatic location). In Automator, create a new Service. Set the top drop downs to No Input and Any Application. From the Utilities Actions, choose Run Shell Script and drag to the workflow area. For the first Service, the entry in the workflow should be:
/usr/bin/sudo /Users/yourname/AirPortReset
And for the second Service, it should be:
/usr/bin/sudo /Users/yourname/AirPortReset auto
Save these as AirportReset & AirportReset Automatic, and now you can use the keyboard shortcut feature to assign them a shortcut key -- I used Command-F5 and Command-F6. Now when I press Command-F5, I cycle between the two wireless networks effortlessly, and when I am somewhere else, I press Command-F6 to revert to the normal Automatic location.

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090917125055502