Batch file to “Keep Alive” a CMAK generated VPN connection in Windows 7
Posted by jpluimers on 2012/05/08
Some clients generate their VPN connection settings using the CMAK (Connection Manager Administration Kit). Apart from the cumbersome way to support both x86 and x64 at the same time, they usually add in some kind of time-out feature, and often route the whole 10.0.0.0/8 network over the VPN to just host a couple of dozen machines. Having done most of my VPN connections by hand, and automating them using rasdial to dial these from the commandline, I also found out the hard way that you cannot use rasdial for CMAK generated VPN connections: it will give you the error “This function is not supported on this system.”. The funny thing is: you can disconnect using rasdial. Luckily, the far less well documented rasphonedoes work for dialing. The batch file below uses a few tricks, and you can set the parameters in the top of the batch file.
- it uses :: as comment markers
- is uses the && and || conditional execution constructs to either continue on success or failure.
- all variables are local using setlocal/endlocal
- ping is used to wait a certain time
@echo off setlocal enableextensions :begin :: we use 0.0.0.0 for wait, see http://www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xml set WaitIP=0.0.0.0 :: usually from a private range: 192.168.*.*, 172.16-31.*.*, 10.*.*.* http://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces set CheckIP=my.remote.host set PingWaitMilliseconds=1000 set LoopWaitSecondsPlus1=31 set RasDialName=CMAK VPN NAME echo Keep-alive: to keep connected to "%RasDialName%", do not close this window :checkLoop :: check if connection is still alive ping %CheckIP% -n 1 -w %PingWaitMilliseconds% > nul && goto :showAlive :recheck ping %CheckIP% -n 1 -w %PingWaitMilliseconds% > nul && goto :showAlive :dead echo dead at %date% %time%: "%RasDialName%" :( :disconnect rasdial "%RasDialName%" /disconnect :connect :: rasdial "%RasDialName%" || goto :noDial :: rasdial fails on Vista and Windows 7 with the message "This function is not supported on this system." on CMAK generated VPN connections :: rasdial is able to disconnect these sessions. rasphone is able to dial them. rasphone -d "%RasDialName%" || goto :noDial echo dialed at %date% %time%: "%RasDialName%" :( goto :checkLoop :noDial echo dial failed at %date% %time%: "%RasDialName%" :( goto :disconnect :showAlive echo alive at %date% %time%: "%RasDialName%" :wait ping -n %LoopWaitSecondsPlus1% %WaitIP% > mul goto :checkLoop :end endlocal