Can I get a batch file to check if the VPN connection on my computer is down and disable my internet connection if it is?

ChaoticWolf

Honorable
I want to make a batch file that constantly checks within a few seconds or minutes if the VPN connection on my PC goes offline, and if it does, I want it to disable the internet connection temporarily until the VPN goes back online or reconnects or if I just reconnect to the network myself.

Keeping a cmd window just constantly waiting for a vpn connection to go offline to cut off my internet connection may be annoying, so I know what to do. Just make the batch window run invisibly with a vbs file which is what I always do if I want to run a batch file invisibly

So really all I want is a batch file that constantly checks if the vpn adapter has a status of connected or disconnected, and if it gets disconnected, it disables my internet connection, setting it to limited or what not, unless I reconnect my vpn or just connect my network connection manually again myself.

Pretty much doing something like this:

@echo off
:loop
timeout 4 >nul
"Command checking the status of the vpn adapter here"
"commands for what to do if the status is disconnected" goto disable
: disable
"commands to disable the internet connection"
"commands for what to do if the status is connected" goto okconnection
:connection
goto loop

you know, something like that.

So if anyone knows how to do this, please let me know, thanks
 
Solution
There are many tutorials on Windows command line / batch files. The "FIND" command eg (or any of GREP clones) can scan the result of RASDIAL and set an error code which subsequent IF statement can process.
This command will report nothing if there is VPN connection
Code:
rasdial | find "No"
You can use 'rasdial' command to check if the VPN connection is alive, if isn't, then it will give you a ERROR command, after that, you need to "shutdown" the network.

I said shutdown between quotes, mainly because you need internet to check if the VPN is alive, so, (obviusly) you need internet to do that.

That my limit on bash lol, hope it helps.
 

ChaoticWolf

Honorable
[quotemsg=18863051,0,2143361]You can use 'rasdial' command to check if the VPN connection is alive, if isn't, then it will give you a ERROR command, after that, you need to "shutdown" the network.

I said shutdown between quotes, mainly because you need internet to check if the VPN is alive, so, (obviusly) you need internet to do that.

That my limit on bash lol, hope it helps.[/quotemsg]

thanks, ill try it out :p

 

ChaoticWolf

Honorable
well, I just did it and when im not on the vpn it says
no connections
the command completed successfully
and when im on the vpn it says
Connected to
Zenmate
the command completed successfully

there's no error message when its not connected to the vpn, how can I make it do a different set of commands then when there are no connections?
 

Allen_23

Prominent
Mar 10, 2017
1
0
510
[quotemsg=19231653,0,2180649]Eventually got it all to work, late update but since this has been solved and I forgot to select a best answer and it's been done for me, I just wanted to mention it[/quotemsg]

How did you get this to work? Thanks.
 

ChaoticWolf

Honorable
[quotemsg=19407786,0,2434315][quotemsg=19231653,0,2180649]Eventually got it all to work, late update but since this has been solved and I forgot to select a best answer and it's been done for me, I just wanted to mention it[/quotemsg]

How did you get this to work? Thanks.[/quotemsg]

I apologize for the late response, I never got around this, but finally here is how I did it incase you're still wondering

Basically, I made a batch file with this layout:

@echo off
:check
timeout 2 >nul
rasdial "VPN Adapter name here" >nul
if %errorlevel% == 0 goto check
) ELSE )
goto vpnoffline
)
:vpnoffline
ipconfig /release
timeout 4 >nul
Any other additional commands can be placed here

Basically that's all there is to it, boot up the vpn, start that batch file up and the rasdial command will check the vpn adapter that you inputted into it if it's running, and if it is, it will constantly execute the command, and if it does go offline, then it will go to the vpnoffline part of the program, do the ipconfig /release command to set the internet connection to limited, and then any additional things can be added, like a batch file to run that tells me that the vpn is offline, or even a message box, a batch file that has a command to turn on the internet connection again with ipconfig /renew would be ideal, for example i would have a batch file that can look like this:

Your VPN has gone offline so your internet connection is disabled
Would you like to turn on your internet connection? [y/n]

then I would input y to do the ipconfig /renew command to bring back the internet connection on the computer, and then I could turn on the vpn again if I want to

That's really all there is to it =D I apologize for taking so long to get around to it, but there you have it!


 

TRENDING THREADS