Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Logdan
Gerbil
Topic Author
Posts: 25
Joined: Mon Mar 26, 2007 7:33 pm

Script to determine if connected to a specific VPN

Wed Jul 02, 2008 12:13 pm

Hello all,

First off, sorry if I'm posting in the wrong forum. If this is the case, please direct me to where this might be better posted.

I'm trying to create a script (it can be in .vbs, .bat, .cmd or .js) to check if the computer is connected to a specific named VPN. This will be used in conjunction with software that performs backups and can use scripts to determine if it should run or not (specifically ViceVersa). I figured I'd be able to Google around and put something together, but I keep getting set up the bomb at every turn (the scripts I find usually return everything else about the connection except for what its name is). The VPN’s name shows up in Task Manager’s Networking tab, so I assumed this information would be available though some script.

I started looking at the Win32_NetworkAdapter WMI class, as I thought this might be the correct way to go. I’d post what I have so far if it would help, but most of my scripts either return errors or are just to Frankenstein’d to make much sense. :/

Anyone know what I should be looking for to return this information? Thanks in advance.
 
titan
Grand Gerbil Poohbah
Posts: 3376
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains
Contact:

Re: Script to determine if connected to a specific VPN

Sun Jul 06, 2008 8:13 pm

You could write a script that looks for a specific server name, and if it can find it to initiate the backup.
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
 
Master Kenobi
Gerbil First Class
Posts: 127
Joined: Wed Aug 15, 2007 9:08 am

Re: Script to determine if connected to a specific VPN

Tue Dec 23, 2008 9:47 am

Logdan thought I would throw you a bone here, but it really depends on how you have the VPN set up. If you use a dedicated client like Cisco's VPN, this would be considerably more difficult. Microsoft' VPN might be easier, anyways you would need to have it connected to find the exact value but this is the location you would want to check in registry to see what the active network connections are.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\

Under this tree you will find a bunch of hashes and subfolders all called "connection" in here is a key designated "Name", this is the text you see in the network places. The best way to nail it down exactly would be to connect to your VPN, drill down to find it and put that in the script to check for before executing. That should work for you I would think.

Something like

X = HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\Placeholder\Connection\Name
Y = "My VPN Connection"

If X = Y Then
Run Script
End If

I use vbscript myself but you could replicate this in a few different ones (Batch file can not look into the registry without a custom exe file).

Hope this helps you a bit.
i7-6700k @ 4.00GHz | Gigabyte Gaming-5 | GTX 980 4GB | 4x8 (32GB) DDR4-2400 Corsair Vengeance LPX | Windows 10 Pro x64 | 1x 1TB Samsung EVO SSD | 1x 3TB WD Green HDD
 
Usacomp2k3
Gerbil God
Posts: 23043
Joined: Thu Apr 01, 2004 4:53 pm
Location: Orlando, FL
Contact:

Re: Script to determine if connected to a specific VPN

Tue Dec 23, 2008 9:54 am

titan wrote:
You could write a script that looks for a specific server name, and if it can find it to initiate the backup.

That is exactly what I was thinking myself.
 
Master Kenobi
Gerbil First Class
Posts: 127
Joined: Wed Aug 15, 2007 9:08 am

Re: Script to determine if connected to a specific VPN

Tue Dec 23, 2008 10:42 am

Yes, I suppose querying for a server on VPN would be the easy solution, but that would likely trigger a backup if the user was in the office as well. So unless you wanted to back them up only when they are on VPN and not in the office, go with the easy method of server checking.
i7-6700k @ 4.00GHz | Gigabyte Gaming-5 | GTX 980 4GB | 4x8 (32GB) DDR4-2400 Corsair Vengeance LPX | Windows 10 Pro x64 | 1x 1TB Samsung EVO SSD | 1x 3TB WD Green HDD
 
Usacomp2k3
Gerbil God
Posts: 23043
Joined: Thu Apr 01, 2004 4:53 pm
Location: Orlando, FL
Contact:

Re: Script to determine if connected to a specific VPN

Tue Dec 23, 2008 10:49 am

Master Kenobi wrote:
Yes, I suppose querying for a server on VPN would be the easy solution, but that would likely trigger a backup if the user was in the office as well. So unless you wanted to back them up only when they are on VPN and not in the office, go with the easy method of server checking.

Perhaps you could check how many hops you were away from the server? For example, if you're in the office, you might be just 2 hops away from the server, but over VPN it is 4 hops due to the addition of the VPN router/appliance. I don't know, just thinking off the top of my head.
 
Master Kenobi
Gerbil First Class
Posts: 127
Joined: Wed Aug 15, 2007 9:08 am

Re: Script to determine if connected to a specific VPN

Tue Dec 23, 2008 11:03 am

That might pose a problem if the users in the office are a different number of hops away. I suppose you could have it check the hops for the VPN gateway if you wanted to do it that way though.
i7-6700k @ 4.00GHz | Gigabyte Gaming-5 | GTX 980 4GB | 4x8 (32GB) DDR4-2400 Corsair Vengeance LPX | Windows 10 Pro x64 | 1x 1TB Samsung EVO SSD | 1x 3TB WD Green HDD
 
DLHM
Gerbil First Class
Posts: 172
Joined: Thu Sep 06, 2007 12:23 pm
Location: Florida

Re: Script to determine if connected to a specific VPN

Tue Dec 23, 2008 11:22 am

If you know the gateway of the connected VPN you can check for the connection like this.
put this in a dos batch.
change the echo command to the actual command you want to run if the test is true or false
also change the IP to the one you wish to monitor.
If all the VPN gateways are the same then I suggest trying to map a drive letter to a mount point and if it succeds then do whatever
you can use the same script changed a little to test for map success or failure.


--edit fixed a script problem
set var=0
for /f "delims=." %%I in ('ping -n 1 10.10.15.1') do if "%%I" == "Request timed out" (set var=1)
IF %var%==0 Echo Connected
IF NOT %var%==0 Echo Not Connected
Ad eundum quo nemo ante iit!

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On