LoBySS

Edit; Op dit moment gebruik ik LoBySS zelf niet meer. Indien je zonder alteveel gedoe aan de slag wilt (qua installeren) raad ik je zeker aan ook ControlPlane te proberen. Let wel dat je nog wat scripties nodig zult hebben om bijvoorbeeld je tunnels op te laten bouwen.

Hieronder find je de AppleScript code van LoBYSS, inclusive wat inline documentatie over way het diet. Je kunt in m’n weblog terugvinden waarom ik het heb geschreven. Ik heb ook wat voorbeeldcommando’s opgenomen, gebaseerd op het feit dat ik zelf OpenVPN server gebruik voor enkele VPNs. Op de Mac heb ik Viscosity als client in gebruik. Helaas heb ik locaties waar ik niet met OpenVPN wil of zelfs mag verbinden, voor de eerste wil ik dus geen VPN voor de tweede heb ik een ‘sshbytunnel’ script wat me met ‘ssh -NL1194:localhost:1194’ wel naar de OpenVPN server kan šŸ™‚ (En ja ik gebruik TCP voor OpenVPN). De voorbeelden van mijn in OSX geconfigureerde locaties zijn ‘home’ waar ik geen tunnel wil, ‘cafe’ waar ik OpenVPN waarschijnlijk niet mag gebruiken en ‘friend’ waar ik OpenVPN direct kan gebruiken. Uiteraard kun je zelf meer commando’s toevoegen naar wens. Ik vond AppleScript relatief makkelijk in gebruik, probeer het maar eens uit!

Hoe te installeren?

  • installeerĀ sleep watcherĀ (volg de documentatie)
  • maak ‘script’ en ‘tmp’ directories in je gebruikersdirectory
  • Sla LoBySS op in de script directory en wijzig LoBySS naar wens (met name de VerifySSID(ActionState) subroutine)
  • Aan de praat krijgen? Creeer een “.wakeup” script in je gebruikersdirectory metĀ “/usr/bin/osascript ~/script/wakeup.scpt” en chmod 755 het

Tips en configuratie?

  • LoBySS werkt op basis van locates – in je systeeminstellingen, netwerkinstellingen kun je meerdere locaties aanmaken Ā (e.g. ‘home’, ‘cafe’ and ‘friend’).
  • Tip; maak een ‘vrij lege’ locatie aan voor ‘home’ en een met zelfs proxyservers voor ‘cafe’
  • Gebruik de AppleScript editor (standaard meegeleverd met OSX) en gebruik start en compileer voordat je met sleepwatcher aan de slag gaat
  • Sluimer uiteindelijk je mac en kijk of de sleepwatcher magie werkt
  • Het script logt rechtstreeks inĀ ~/tmp/wakeup.results zodat je kunt zien waar LoBySS mee bezig is geweest
Code:

# 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

This post is also available in: Engels