Edit; Currently I’m not using LoBySS at the moment, whenever you want to quickly accommodate (install-wise) my advise is to try out ControlPlane. Note that you will have to do some scripting to start your tunnels.
Below you can find the AppleScript code for LoBySS including documentation on the script. You can look up my blogs in both Dutch and English to find out about why I wrote this script. Included are some example commands I used, to provide you with some background I’m using an OpenVPN server and Viscosity on my OSX device. As some locations don’t allow OpenVPN I’m using my ‘sshbytunnel’ script to provide me with a ‘ssh -NL1194:localhost:1194’ towards the OpenVPN server 🙂 (And yes, I’m using TCP for OpenVPN). So the examples include ‘home’ where I don’t need any tunnel, ‘cafe’ where I might not be able to use OpenVPN directly and ‘friend’ where I use OpenVPN. Obviously you can script some more commands etc. to suit your situation. I’ve found AppleScript to be pretty simple and straightforward, just try!
So how to install?
- install sleep watcher (follow the documentation that comes with it)
- create the ‘script’ and ‘tmp’ directories in your user directory
- Save LoBySS to the script directory and adjust LoBySS to your needs (mainly adjust VerifySSID(ActionState) subroutine)
- Making it work, create a “.wakeup” script in your home directory containing “/usr/bin/osascript ~/script/wakeup.scpt” and chmod 755 it.
Tips and configuration?
- LoBySS works by having Locations – go to your system preferences, network settings and create multiple locations (e.g. ‘home’, ‘cafe’ and ‘friend’).
- Tip; you can set-up nothing much for ‘home’ while you have your own proxy servers for ‘cafe’
- Use AppleScript editor (provided with OSX) use to run and compile before heading for sleepwatcher
- Then put your mac to sleep and look if it works (sleep watcher magic)
- The script blundly logs into ~/tmp/wakeup.results so you can see when it tried to maintain/switch locations
# Applescript
# LoBySS - Location By SSID
# About LoBySS :
# This applescript is to be used with SleepWatcher
# It’s purpose is to adjust your Macs settings depending on where you are
# There are a lot of other tools doing this but at time of writing none were
# available for OS X Lion – as such I decided to write my own, unfortunately
# none of the other tools are Open Source (some not even free), so I’ve newly
# invented the wheel for now. It’s yours to adjust as long as you obey the
# license
##########################################
# Copyright © August 2011 by Tom Scholten, http://tom.scholten.nu .
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY Tom Scholten ”AS IS” AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL Tom Scholten OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those
# of the authors and should not be interpreted as representing official policies,
# either expressed
##########################################
# Prep applescript to split strings
set AppleScript’s text item delimiters to “: ”
# Set script global variables
global WiFiNetwork, NewLocation, Additional, ActionTaken
set Additional to “”
set TimeStamp to current date
##########################################
# Create subroutine to change to new location #
##########################################
on ChangeLocation(Target)
do shell script “networksetup -switchtolocation ” & Target
end ChangeLocation
###########################################
# Create subroutine to determine location #
###########################################
on VerifySSID(ActionState)
# Cleanup running applications
if ActionState is “switch” then
# Stop existing VPN connections
tell application “Viscosity” to disconnectall
# Find and clean existing SSH tunnels and sessions
do shell script “killall ssh; echo 0”
# Remove VPN default route
do shell script “sudo /sbin/route delete 0/1 172.16.32.64; echo 0”
do shell script “sudo /usr/sbin/arp -d 172.16.32.64 echo 0”
end if
# Determine location from SSID
if (WiFiNetwork is “Home” or WiFiNetwork is “Shed”) then
set NewLocation to “home”
if ActionState is “switch” then
ChangeLocation(NewLocation)
end if
end if
if WiFiNetwork is “FriendsWiFi” then
set NewLocation to “friend”
if ActionState is “switch” then
ChangeLocation(NewLocation)
tell application “Viscosity” to connect “OpenVPN”
end if
set Additional to “started VPN to OpenVPN”
end if
if WiFiNetwork is “CafeWiFi” then
set NewLocation to “cafe”
if ActionState is “switch” then
ChangeLocation(NewLocation)
do shell script “~/script/sshbytunnel > /dev/null 2>&1 &”
tell application “Viscosity” to connect “OpenVPN-by-ssh”
end if
set Additional to “started sshBYtunnel and VPN to OpenVPN-by-ssh”
end if
end VerifySSID
###############
# Main routine #
###############
# Get current location and network connection
repeat
set WiFiNetwork to do shell script “networksetup -getairportnetwork en0”
if WiFiNetwork is not “You are not associated with an AirPort network.” then exit repeat
end repeat
set WiFiNetwork to second text item of WiFiNetwork
set CurrentLocation to do shell script “networksetup -getcurrentlocation”
set CurrentLocation to first text item of CurrentLocation
# Go through verification/match
VerifySSID(“check”)
if CurrentLocation is not NewLocation then
set ActionTaken to “Switched to”
# Switch to automatic, let OSX connect to desired network
ChangeLocation(“Automatic”)
# Determine which SSID we are connected to by now
repeat
set WiFiNetwork to do shell script “networksetup -getairportnetwork en0”
if WiFiNetwork is not “You are not associated with an AirPort network.” then exit repeat
end repeat
set WiFiNetwork to second text item of WiFiNetwork
# Determine and switch to the correct location for the SSID
VerifySSID(“switch”)
else
set ActionTaken to “Remained at”
# Determine and switch to the correct location for the SSID
VerifySSID(“switch”)
end if
# Put log information
do shell script “echo ‘” & TimeStamp & ” ” & ActionTaken & ” location ” & NewLocation & ” with SSID ” & WiFiNetwork & Additional & “‘ >> ~/tmp/wakeup.result”
# Always get iChat and Adium  back online
tell application “System Events” to if (processes whose name is “iChat”) exists then tell application “iChat” to log in
tell application “System Events” to if (processes whose name is “Adium”) exists then tell application “Adium” to go online