Reputation: 53
I'm looking to create a series of batch files that check for the state of a specific VPN before starting a VPN if it isn't already running, and finally starting a specific application.
I have a working example, but I'm trying to modify it as both a learning exercise, and practical expansion.
I'm currently using ipconfig and piping to find/i and looking for the VPN name, forcing a disconnect and then reconnecting.
I'd like to do the same, but skip the disconnect if the VPN is up.
Working example:
@echo OFF
::Check to see if the VPN is already connected and disconnect it
echo Checking for VPN connection...
@ipconfig|find/i "myVPN" && rasdial myVPN /disconnect
echo.
echo.
::Connect to the VPN server
@rasdial.exe myVPN "username" "password"
echo.
echo.
As I said, this works, but the forced disconnect of the VPN upsets some of the other services that might be running over the VPN, as you can well imagine.
This is what I have been playing with, but I seem to be stuck and not getting anywhere, you advice is greatly appreciated!
Progress:
@echo OFF
::Check to see if the VPN is already connected and disconnect it
echo Checking for VPN connection...
IF @ipconfig|find/i "VPN" (GOTO startAPP) ELSE (GOTO connectVPN)
echo.
echo.
::Connect to the VPN server
:connectVPN
echo Connecting VPN
@rasdial.exe VPN "username" "password"
echo.
echo.
GOTO startAPP
:startAPP
echo Starting App
@C:\app.ext
:END
I've most recently been trying to store the various options as variables to make changes easier, and having some user input for the username and password. This is the eventual progression, once I have this initial part sorted.
Upvotes: 2
Views: 5554
Reputation: 9545
You can use the conditional operators &&
and ||
like this :
ipconfig|find /i "VPN" && GOTO startAPP || GOTO connectVPN
or
ipconfig|find /i "VPN"
if %errorlevel%==1 goto:connectVPN
echo Starting APP !
exit/b
:connectVPN
echo connecting to VPN
Upvotes: 5