Skip to content

Instantly share code, notes, and snippets.

@g3rhard
Created June 21, 2017 09:36
Show Gist options
  • Save g3rhard/cc2291371001de46e909385af64da3a7 to your computer and use it in GitHub Desktop.
Save g3rhard/cc2291371001de46e909385af64da3a7 to your computer and use it in GitHub Desktop.
Bat script for reconnect VPN connection in Windows
@echo off
:loop
echo testing IP address
ping 123.456.78.90 >nul || (
echo ping failure - disconnecting
rasdial name_of_your_vpn_connect /disconnect
timeout /t 10 /nobreak
echo reconnecting
rasdial name_of_your_vpn_connect
timeout /t 10 /nobreak
)
timeout /t 30 /nobreak
goto :loop
@g3rhard
Copy link
Author

g3rhard commented Jun 22, 2017

In the year 2017, system administrators still have to reconnect the VPN connection using loops in the bat script ;(
No, I know about the task scheduler, and about carrying out tasks on events - but they do not work stably.
If you know a way how to make this kind of tasks through the scheduler more stable, please write in the comments.

@g3rhard
Copy link
Author

g3rhard commented Jun 22, 2017

@picolino
Copy link

picolino commented Aug 31, 2020

Hello from 2020 =)
A little improved version:

@echo off

set VPN=Your_VPN_Name
set USERNAME=Your_VPN_User_Name
set PASSWORD=Your_VPN_Password

:loop
    cls
    echo Checking connection availability...
    ipconfig|find /i "%VPN%"
    if %errorlevel%==1 goto:reconnect
    timeout /t 30 /nobreak
    goto :loop

:reconnect
    echo Disconnecting...
    rasdial "%VPN%" /disconnect
    timeout /t 10 /nobreak
    echo Connecting...
    rasdial "%VPN%" "%USERNAME%" "%PASSWORD%"
    timeout /t 10 /nobreak
    goto :loop

This version checking existing ipconfig adapter connection to detect current VPN connection

@g3rhard
Copy link
Author

g3rhard commented Aug 31, 2020

@picolino, thank you, good job!

@helpdesk-itd
Copy link

excellent job @picolino , thank you

@SourceSurfer
Copy link

@echo off
set VPNName="YourVPNConnectionName"

rasdial %VPNName%

@keshav225
Copy link

It works perfectly thank you @picolino

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment