{"id":1191,"date":"2011-08-05T20:40:54","date_gmt":"2011-08-05T18:28:22","guid":{"rendered":"http:\/\/tom.scholten.nu\/weblog\/?page_id=1191"},"modified":"2012-05-13T15:32:09","modified_gmt":"2012-05-13T13:32:09","slug":"lobyss","status":"publish","type":"page","link":"https:\/\/tom.scholten.nu\/weblog\/lobyss","title":{"rendered":"LoBySS"},"content":{"rendered":"<p><strong><span style=\"color: #ff0000;\">Edit<\/span>; Currently I&#8217;m not using LoBySS at the moment, whenever you want to quickly accommodate (install-wise) my advise is to try out\u00c2\u00a0<strong><a href=\"http:\/\/www.controlplaneapp.com\/\">ControlPlane<\/a>. Note that you will have to do some scripting to start your tunnels.<\/strong><\/strong><\/p>\n<p>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&#8217;m using an OpenVPN server and Viscosity on my OSX device. As some locations don&#8217;t allow OpenVPN I&#8217;m using my &#8216;sshbytunnel&#8217; script to provide me with a &#8216;ssh -NL1194:localhost:1194&#8217; towards the OpenVPN server \ud83d\ude42 (And yes, I&#8217;m using TCP for OpenVPN). So the examples include &#8216;home&#8217; where I don&#8217;t need any tunnel, &#8216;cafe&#8217; where I might not be able to use OpenVPN directly and &#8216;friend&#8217; where I use OpenVPN. Obviously you can script some more commands etc. to suit your situation. I&#8217;ve found AppleScript to be pretty simple and straightforward, just try!<\/p>\n<p>So how to install?<\/p>\n<ul>\n<li>install <a title=\"SleepWatcher\" href=\"http:\/\/www.bernhard-baehr.de\/\">sleep watcher<\/a>\u00c2\u00a0(follow the documentation that comes with it)<\/li>\n<li>create the &#8216;script&#8217; and &#8216;tmp&#8217; directories in your user directory<\/li>\n<li>Save LoBySS to the script directory and adjust LoBySS to your needs (mainly adjust VerifySSID(ActionState) subroutine)<\/li>\n<li>Making it work, create a &#8220;.wakeup&#8221; script in your home directory containing &#8220;<span class=\"Apple-style-span\" style=\"font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;\">\/usr\/bin\/osascript ~\/script\/wakeup.scpt&#8221; and chmod 755 it.<\/span><\/li>\n<\/ul>\n<p>Tips and configuration?<\/p>\n<ul>\n<li>LoBySS works by having Locations &#8211; go to your system preferences, network settings and create multiple locations (e.g. &#8216;home&#8217;, &#8216;cafe&#8217; and &#8216;friend&#8217;).<\/li>\n<li>Tip; you can set-up nothing much for &#8216;home&#8217; while you have your own proxy servers for &#8216;cafe&#8217;<\/li>\n<li>Use AppleScript editor (provided with OSX) use to run and compile before heading for sleepwatcher<\/li>\n<li>Then put your mac to sleep and look if it works (sleep watcher magic)<\/li>\n<li>The script blundly logs into ~\/tmp\/wakeup.results so you can see when it tried to maintain\/switch locations<\/li>\n<\/ul>\n<div>Code:<\/div>\n<p><code> # Applescript<br \/>\n# LoBySS - Location By SSID<\/code><\/p>\n<p># About LoBySS :<br \/>\n# This applescript is to be used with SleepWatcher<br \/>\n# It&#8217;s purpose is to adjust your Macs settings depending on where you are<br \/>\n# There are a lot of other tools doing this but at time of writing none were<br \/>\n# available for OS X Lion &#8211; as such I decided to write my own, unfortunately<br \/>\n# none of the other tools are Open Source (some not even free), so I&#8217;ve newly<br \/>\n# invented the wheel for now. It&#8217;s yours to adjust as long as you obey the<br \/>\n# license<\/p>\n<p>##########################################<\/p>\n<p># Copyright \u00c2\u00a9 August 2011 by Tom Scholten, http:\/\/tom.scholten.nu .<br \/>\n# All rights reserved.<br \/>\n#<br \/>\n# Redistribution and use in source and binary forms, with or without modification,<br \/>\n# are permitted provided that the following conditions are met:<br \/>\n#<br \/>\n# 1. Redistributions of source code must retain the above copyright notice,<br \/>\n# this list of conditions and the following disclaimer.<br \/>\n#<br \/>\n# 2. Redistributions in binary form must reproduce the above copyright notice,<br \/>\n# this list of conditions and the following disclaimer in the documentation<br \/>\n# and\/or other materials provided with the distribution.<br \/>\n#<br \/>\n# THIS SOFTWARE IS PROVIDED BY Tom Scholten &#8221;AS IS&#8221; AND ANY EXPRESS OR IMPLIED<br \/>\n# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF<br \/>\n# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO<br \/>\n# EVENT SHALL Tom Scholten OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,<br \/>\n# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT<br \/>\n# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR<br \/>\n# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF<br \/>\n# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING<br \/>\n# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,<br \/>\n# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br \/>\n#<br \/>\n# The views and conclusions contained in the software and documentation are those<br \/>\n# of the authors and should not be interpreted as representing official policies,<br \/>\n# either expressed<\/p>\n<p>##########################################<\/p>\n<p># Prep applescript to split strings<br \/>\nset AppleScript&#8217;s text item delimiters to &#8220;: &#8221;<\/p>\n<p># Set script global variables<br \/>\nglobal WiFiNetwork, NewLocation, Additional, ActionTaken<br \/>\nset Additional to &#8220;&#8221;<br \/>\nset TimeStamp to current date<\/p>\n<p>##########################################<br \/>\n# Create subroutine to change to new location #<br \/>\n##########################################<br \/>\non ChangeLocation(Target)<br \/>\ndo shell script &#8220;networksetup -switchtolocation &#8221; &amp; Target<br \/>\nend ChangeLocation<\/p>\n<p>###########################################<br \/>\n# Create subroutine to determine location #<br \/>\n###########################################<br \/>\non VerifySSID(ActionState)<br \/>\n# Cleanup running applications<br \/>\nif ActionState is &#8220;switch&#8221; then<br \/>\n# Stop existing VPN connections<br \/>\ntell application &#8220;Viscosity&#8221; to disconnectall<br \/>\n# Find and clean existing SSH tunnels and sessions<br \/>\ndo shell script &#8220;killall ssh; echo 0&#8221;<br \/>\n# Remove VPN default route<br \/>\ndo shell script &#8220;sudo \/sbin\/route delete 0\/1 172.16.32.64; echo 0&#8221;<br \/>\ndo shell script &#8220;sudo \/usr\/sbin\/arp -d 172.16.32.64 echo 0&#8221;<br \/>\nend if<\/p>\n<p># Determine location from SSID<br \/>\nif (WiFiNetwork is &#8220;Home&#8221; or WiFiNetwork is &#8220;Shed&#8221;) then<br \/>\nset NewLocation to &#8220;home&#8221;<br \/>\nif ActionState is &#8220;switch&#8221; then<br \/>\nChangeLocation(NewLocation)<br \/>\nend if<br \/>\nend if<\/p>\n<p>if WiFiNetwork is &#8220;FriendsWiFi&#8221; then<br \/>\nset NewLocation to &#8220;friend&#8221;<br \/>\nif ActionState is &#8220;switch&#8221; then<br \/>\nChangeLocation(NewLocation)<br \/>\ntell application &#8220;Viscosity&#8221; to connect &#8220;OpenVPN&#8221;<br \/>\nend if<br \/>\nset Additional to &#8220;started VPN to OpenVPN&#8221;<br \/>\nend if<\/p>\n<p>if WiFiNetwork is &#8220;CafeWiFi&#8221; then<br \/>\nset NewLocation to &#8220;cafe&#8221;<br \/>\nif ActionState is &#8220;switch&#8221; then<br \/>\nChangeLocation(NewLocation)<br \/>\ndo shell script &#8220;~\/script\/sshbytunnel &gt; \/dev\/null 2&gt;&amp;1 &amp;&#8221;<br \/>\ntell application &#8220;Viscosity&#8221; to connect &#8220;OpenVPN-by-ssh&#8221;<br \/>\nend if<br \/>\nset Additional to &#8220;started sshBYtunnel and VPN to OpenVPN-by-ssh&#8221;<br \/>\nend if<\/p>\n<p>end VerifySSID<\/p>\n<p>###############<br \/>\n# Main routine #<br \/>\n###############<\/p>\n<p># Get current location and network connection<br \/>\nrepeat<br \/>\nset WiFiNetwork to do shell script &#8220;networksetup -getairportnetwork en0&#8221;<br \/>\nif WiFiNetwork is not &#8220;You are not associated with an AirPort network.&#8221; then exit repeat<br \/>\nend repeat<br \/>\nset WiFiNetwork to second text item of WiFiNetwork<br \/>\nset CurrentLocation to do shell script &#8220;networksetup -getcurrentlocation&#8221;<br \/>\nset CurrentLocation to first text item of CurrentLocation<\/p>\n<p># Go through verification\/match<br \/>\nVerifySSID(&#8220;check&#8221;)<\/p>\n<p>if CurrentLocation is not NewLocation then<\/p>\n<p>set ActionTaken to &#8220;Switched to&#8221;<\/p>\n<p># Switch to automatic, let OSX connect to desired network<br \/>\nChangeLocation(&#8220;Automatic&#8221;)<\/p>\n<p># Determine which SSID we are connected to by now<br \/>\nrepeat<br \/>\nset WiFiNetwork to do shell script &#8220;networksetup -getairportnetwork en0&#8221;<br \/>\nif WiFiNetwork is not &#8220;You are not associated with an AirPort network.&#8221; then exit repeat<br \/>\nend repeat<br \/>\nset WiFiNetwork to second text item of WiFiNetwork<\/p>\n<p># Determine and switch to the correct location for the SSID<br \/>\nVerifySSID(&#8220;switch&#8221;)<\/p>\n<p>else<br \/>\nset ActionTaken to &#8220;Remained at&#8221;<br \/>\n# Determine and switch to the correct location for the SSID<br \/>\nVerifySSID(&#8220;switch&#8221;)<br \/>\nend if<\/p>\n<p># Put log information<br \/>\ndo shell script &#8220;echo &#8216;&#8221; &amp; TimeStamp &amp; &#8221; &#8221; &amp; ActionTaken &amp; &#8221; location &#8221; &amp; NewLocation &amp; &#8221; with SSID &#8221; &amp; WiFiNetwork &amp; Additional &amp; &#8220;&#8216; &gt;&gt; ~\/tmp\/wakeup.result&#8221;<\/p>\n<p># Always get iChat and Adium \u00c2\u00a0back online<br \/>\ntell application &#8220;System Events&#8221; to if (processes whose name is &#8220;iChat&#8221;) exists then tell application &#8220;iChat&#8221; to log in<br \/>\ntell application &#8220;System Events&#8221; to if (processes whose name is &#8220;Adium&#8221;) exists then tell application &#8220;Adium&#8221; to go online<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Edit; Currently I&#8217;m not using LoBySS at the moment, whenever you want to quickly accommodate (install-wise) my advise is to try out\u00c2\u00a0ControlPlane. Note that you<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-1191","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/tom.scholten.nu\/weblog\/wp-json\/wp\/v2\/pages\/1191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tom.scholten.nu\/weblog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/tom.scholten.nu\/weblog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/tom.scholten.nu\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tom.scholten.nu\/weblog\/wp-json\/wp\/v2\/comments?post=1191"}],"version-history":[{"count":7,"href":"https:\/\/tom.scholten.nu\/weblog\/wp-json\/wp\/v2\/pages\/1191\/revisions"}],"predecessor-version":[{"id":1195,"href":"https:\/\/tom.scholten.nu\/weblog\/wp-json\/wp\/v2\/pages\/1191\/revisions\/1195"}],"wp:attachment":[{"href":"https:\/\/tom.scholten.nu\/weblog\/wp-json\/wp\/v2\/media?parent=1191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}