Tuesday, January 8, 2013

Allow application to run only when you are connected to VPN with AutoHotKey

I recently started using +AutoHotkey  to automate things and i really like it, and I'm going to post a series of my scripts that i use.

This AHK script is about checking if I'm connected to certain VPN (or one of them if there are many) and start a backup, perform a set of actions,start an application, run uTorrent :) basically anything you can think of.
It will do a check every 10 sec to see if you are connected, if the VPN connection is dropped it will close the application and start it if connection is back.

To get started download and install AutoHotKey from http://www.autohotkey.com/

I also recommend SciTE4AutoHotkey editor http://www.autohotkey.net/~fincs/SciTE4AutoHotkey_3/web/ to create, edit, debug scripts.

Copy the entire code (in blue) and paste it in a new file and save it with extension .ahk.
Tip: If you are using Notepad and want to have different file extension than .txt include the whole filename in quotas like "checkVpn.ahk" when you save the file.

To run it just double click on the file and it will run in background which you can find it in system tray.

To have this run when Windows starts make a shortcut of this file in Startup folder in Startmenu or just move the script there.

For this example i have used uTorrent.exe \m/
And only changes you have to make is your VPN IP list column delimited, e.g.:
I have three different VPN connections whose IPs start with
10.0.1.3
10.0.2.23
10.0.3.132
Since i know the first 10.0.1 won't change i use only those and make it
10.0.1:10.0.2:10.0.3
Applications filename and
Applications full path

Enjoy
VPNIP = 10.0.1:10.0.2:10.0.3 ;Write your VPN IP or part of it, if you have many separate them using column :
app = uTorrent.exe ;Write the file name of the app
appFullPath = "C:\Program Files (x86)\uTorrent\uTorrent.exe" ;Write the full path including filename of the app

Loop ;perform the main action
{
 global Connected, app, appFullPath
 Process, Exist, %app%
 AppRunning = %ErrorLevel%
 Sleep, 10000
 CheckIP()
 If (Connected = 1)
  {
  If (AppRunning = 0) ; If it is not running
   {
   Run, %appFullPath%
   }
  else
  continue
  } 
 else
 {
  If (AppRunning > 0)
  Process, Close, %app%
 }
}

CheckIP() ;checks if we are connected to VPN and set variable Connected to 0|1
{
 global VPNIP
 StringSplit, IPArray, VPNIP, :
 LocalIP = %A_IPAddress1%,%A_IPAddress2%,%A_IPAddress3%,%A_IPAddress4%
 Matches = 0
 Loop, %IPArray0%
 {
  Each := IPArray%A_Index%
  IfInString, LocalIP, %Each%
   Matches++
 }
 If Matches > 0
  Connected = 1
 else
  Connected = 0
}

1 comment:

yardleacaiazza said...

casino games, poker, roulette, slots, blackjack
There's even a good reason that 수원 출장안마 this 충청북도 출장샵 website is designed 태백 출장안마 to keep players at 경산 출장안마 home with poker and slots. With so many games and games, there's always something for everyone 경상북도 출장안마