Sunday, July 17, 2011

VPN only when specific APP is running

So, this is neat.

I started here:

http://fardjad.wordpress.com/2010/11/11/vpn-auto-connect-script/

then added the test if my application was running.

Say you need a VPN to be connected only if a certain program is running, not just if u want to visit a certain website like the VPN on demand OSX built in feature.

build this script:
============

on idle

 if application "Mail" is running then

  tell application "System Events"
   tell current location of network preferences
    set myConnection to the service "Name_of_VPN"
    if myConnection is not null then
     if current configuration of myConnection is not connected then
      connect myConnection
     end if
    end if
   end tell
  end tell

 else
  tell application "System Events"
   tell current location of network preferences
    set myConnection to the service "Name_of_VPN"
    if myConnection is not null then
     if current configuration of myConnection is connected then
      disconnect myConnection
     end if
    end if
   end tell
  end tell
 end if
 return 2

end idle

============


Replace  - Mail - with the app you want to monitor and - Name_of_VPN - with the name of your chosen vpn connection as it appears is system preferences under "network". might be something like "VPN (PPTP)" or "VPN (LT2P)". I have several so I name then differently to be able to tell them apart.
Then save the above script as an app and check "stay open".
Run it and it will check every two seconds if your chosen app (Mail in the example above) is running and if it IS connect to your VPN, when you close the app it closes the VPN connection.
Add the app under accounts, login items, and it will just be running there in the background making sure you check your mail only through your VPN.

If you look at the link I reference finding above it tells you how to keep the script icon from showing up on the dock. I don't mind it there tho, then I know I'm protected.

+++++++++++
Edit:

check out this freeware:
http://www.pearworks.com/pages/pearPortVPN_Screenshot.html
connects to a VPN based on network parameter tests. ie, ip-range or name of wifi network.
The script I wrote tho is different in that it forces specific apps to always use VPN.

Thursday, June 30, 2011

Lock and unlock a Mac using Bluetooth detection (from Mac OS X Hints)

NOTE:
This is original post, I have updated instructions here that work better with OSX Lion.


This is really cool!
I found basic info about this here:
http://hints.macworld.com/article.php?story=20091221173111783

How I made this work to lock/unlock & displaysleep/wake my computer when I am (or my phone is, actually) near it:
The way I improved upon the above link is by making my computer display actually go to sleep, turn off, this saves power and makes it last longer. The same as pushing "shift-control-eject" at the same time.


Step 1:
Do this:
http://kprops.blogspot.com/2011/07/sleep-display-instantly-hack-found-not.html
This creates a unix executable that will sleep the display instantly when run,
save it in Applications folder
Then in system preferences, "security" check "require password....blah blah blah" to "immediately"

Step 2:
open keychain access (in utilities folder)














then file menu, new password item:




Keychain item name and account name, set to "myPass"
Password: set to the password you use to log onto your computer.
This stores your password in a secure place that we can later access via script.

Step 3:
Download the opensource free file "Proximity 1.5.zip":
from this page
http://code.google.com/p/reduxcomputing-proximity/downloads/detail?name=Proximity%201.5.zip&can=2&q=
(short link direct to file: http://goo.gl/9YATT)
extract it to Applications folder.
(read about it below before you install cause it's smart to do)
http://code.google.com/p/reduxcomputing-proximity/


Step 4:
Write 1 applescript in scriptwriter, named as below:

SleepDisplay.scpt
===========
on run
tell application "Terminal"
do script "/Applications/SleepDisplay"
end tell
end run
==========(below also)









This script just calls the executable you made in Step 1
Save it in the Applications folder

NOTE: Do ONLY step 5a or 5b, 5a for pre-LION, 5b for Lion
(since Apple removed Keychain Scripting app in Lion)
There is another problem with Lion. 5b script below only logs you in, it doesn't actually nudge the display awake. Once you sit down you have to hit any key (i use the down button) to wake up the display, but at least you don't have to enter your password.

Step 5a: (for pre "OSX Lion")

SleepWake.scpt
==========
on run
tell application "Keychain Scripting"
set thePW to the password of first key of current keychain whose name is "myPass"
tell application "System Events"
tell application process "loginwindow"
keystroke 125
delay 1
keystroke thePW
delay 0.2
keystroke return
end tell
end tell
end tell
tell application "Terminal" to quit
end run

=============== (below also)













Save it in the Applications folder

Step 5B (for Lion)

SleepWake.scpt
===============
on run
set thePW to ""
set label to "myPass" -- use the name of the password you made above
set thePW to do shell script "security 2>&1 >/dev/null find-generic-password -gl " & label
-- following several lines from: http://macscripter.net/viewtopic.php?id=36818
set oldelim to text item delimiters
set text item delimiters to "\""
set tlst to every text item of thePW
set thePW to item 2 of tlst
set text item delimiters to "\\"
set tlst to every text item of thePW
set thePW to item 1 of tlst
set text item delimiters to oldelim
-- this will now work if your password contains problematic characters or is not 7 bit ASCII
-- see this : http://www.j-schell.de/node/401
tell application "System Events"
tell application process "loginwindow"
keystroke 125
delay 1
keystroke thePW
delay 0.2
keystroke return
end tell
end tell
tell application "Terminal" to quit
end run

=============== (below also)





















Save it in the Applications folder

Step 6:
open Proximity app you just downloaded
(icon will be in upper right of screen)
pair your phone with your computer by using the "change device" button

set "Out of Range Script" to:
/Applications/SleepDisplay.scpt
(the first script you created above)

hit "test"
Screen should sleep, log back in

set "In Range Script" to:
/Applications/SleepWake.scpt
(the second script you created above)

hit "test"

You may get several repeated security warnings asking you to allow keychain access etc by several programs, just enter your password whenever it asks and click "allow always" or "allow" if always isnt available. this is giving the app proximity and the script permission to look up your password that you securely stored in keychain, so you don't have to store your password in plain text format.

Step 7: (important!)

Once it is working and tested and you are happy, open the script "SleepWake.scpt" in AppleScriptEditor, and re-save it by using the "Save As..." File command and check the option box for "Run Only" this will prevent someone from opening it up in script editor and running it and then viewing your password in the script results. Name it the same name and overwrite the original file.

Enjoy!

Leave bluetooth active on your phone. When you walk away from your computer with your phone in your pocket the display/s will sleep and lock, requiring a password to login, (in case you come back without your phone)

when you walk back with your phone in your pocket the screen will automatically unlock and light up.

You could also tell the wake up script to play a sound file that says hello to you, and the sleep script to say goodbye, use text to voice engines etc, etc... :)

sleep display instantly hack (not mine, found via googles)

Source code copied from the file SleepDisplay.zip found here:
http://forums.macrumors.com/showthread.php?p=8864913#post8864913
link to file here:
http://goo.gl/3C1ww

you can just download this file above and extract SleepDisplay into your Applications folder and be done, but I wanted to see the source and then compile it myself, so I did the steps below...

10.5: Search multiple locations in Leopard - Mac OS X Hints

10.5: Search multiple locations in Leopard - Mac OS X Hints