{"id":1196,"date":"2011-08-05T20:40:54","date_gmt":"2011-08-05T18:40:54","guid":{"rendered":"http:\/\/tom.scholten.nu\/weblog\/?page_id=1196"},"modified":"2012-05-13T15:30:55","modified_gmt":"2012-05-13T13:30:55","slug":"lobyss-2","status":"publish","type":"page","link":"https:\/\/tom.scholten.nu\/weblog\/lobyss-2","title":{"rendered":"LoBySS"},"content":{"rendered":"<p><strong><span style=\"color: #ff0000;\">Edit<\/span>; 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 <a href=\"http:\/\/www.controlplaneapp.com\/\">ControlPlane<\/a> te proberen. Let wel dat je nog wat scripties nodig zult hebben om bijvoorbeeld je tunnels op te laten bouwen.<\/strong><\/p>\n<p>Hieronder find je de AppleScript code van LoBYSS, inclusive wat inline documentatie over way het diet. Je kunt in m&#8217;n weblog terugvinden waarom ik het heb geschreven. Ik heb ook wat voorbeeldcommando&#8217;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 &#8216;sshbytunnel&#8217; script wat me met &#8216;ssh -NL1194:localhost:1194&#8217; wel naar de OpenVPN server kan \ud83d\ude42 (En ja ik gebruik TCP voor OpenVPN). De voorbeelden van mijn in OSX geconfigureerde locaties zijn &#8216;home&#8217; waar ik geen tunnel wil, &#8216;cafe&#8217; waar ik OpenVPN waarschijnlijk niet mag gebruiken en &#8216;friend&#8217; waar ik OpenVPN direct kan gebruiken. Uiteraard kun je zelf meer commando&#8217;s toevoegen naar wens. Ik vond AppleScript relatief makkelijk in gebruik, probeer het maar eens uit!<\/p>\n<p>Hoe te installeren?<\/p>\n<ul>\n<li>installeer\u00c2\u00a0<a title=\"SleepWatcher\" href=\"http:\/\/www.bernhard-baehr.de\/\">sleep watcher<\/a>\u00c2\u00a0(volg de documentatie)<\/li>\n<li>maak &#8216;script&#8217; en &#8216;tmp&#8217; directories in je gebruikersdirectory<\/li>\n<li>Sla LoBySS op in de script directory en wijzig LoBySS naar wens (met name de VerifySSID(ActionState) subroutine)<\/li>\n<li>Aan de praat krijgen? Creeer een &#8220;.wakeup&#8221; script in je gebruikersdirectory met\u00c2\u00a0&#8220;\/usr\/bin\/osascript ~\/script\/wakeup.scpt&#8221; en chmod 755 het<\/li>\n<\/ul>\n<p>Tips en configuratie?<\/p>\n<ul>\n<li>LoBySS werkt op basis van locates &#8211; in je systeeminstellingen, netwerkinstellingen kun je meerdere locaties aanmaken \u00c2\u00a0(e.g. &#8216;home&#8217;, &#8216;cafe&#8217; and &#8216;friend&#8217;).<\/li>\n<li>Tip; maak een &#8216;vrij lege&#8217; locatie aan voor &#8216;home&#8217; en een met zelfs proxyservers voor &#8216;cafe&#8217;<\/li>\n<li>Gebruik de AppleScript editor (standaard meegeleverd met OSX) en gebruik start en compileer voordat je met sleepwatcher aan de slag gaat<\/li>\n<li>Sluimer uiteindelijk je mac en kijk of de sleepwatcher magie werkt<\/li>\n<li>Het script logt rechtstreeks in\u00c2\u00a0~\/tmp\/wakeup.results zodat je kunt zien waar LoBySS mee bezig is geweest<\/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<\/p>\n<p>set 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; Op dit moment gebruik ik LoBySS zelf niet meer. Indien je zonder alteveel gedoe aan de slag wilt (qua installeren) raad ik je zeker<\/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-1196","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/tom.scholten.nu\/weblog\/wp-json\/wp\/v2\/pages\/1196","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=1196"}],"version-history":[{"count":6,"href":"https:\/\/tom.scholten.nu\/weblog\/wp-json\/wp\/v2\/pages\/1196\/revisions"}],"predecessor-version":[{"id":1211,"href":"https:\/\/tom.scholten.nu\/weblog\/wp-json\/wp\/v2\/pages\/1196\/revisions\/1211"}],"wp:attachment":[{"href":"https:\/\/tom.scholten.nu\/weblog\/wp-json\/wp\/v2\/media?parent=1196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}