Command Line VPN Connection Management in Windows XP: Scripting rasdial for Batch Automation


1 views

Windows XP Professional includes built-in VPN functionality through the Point-to-Point Tunneling Protocol (PPTP) client. The connection management can be fully controlled via command line using rasdial.exe, which ships with the operating system. This approach works for both dial-up and VPN connections.

The basic syntax for managing connections is:

rasdial "connection_name" [username [password | *]] [/domain:domain] [/phone:phonenumber] [/callback:callbacknumber] [/phonebook:phonebookfile] [/prefixsuffix]

To establish a VPN connection:

rasdial "MyVPN" vpn_user mypassword /domain:corp

To disconnect:

rasdial "MyVPN" /DISCONNECT

Here's a complete batch script example with error handling:

@echo off
SET VPN_NAME="Corporate VPN"
SET USERNAME=vpn_user
SET PASSWORD=secure123

:CONNECT
rasdial %VPN_NAME% %USERNAME% %PASSWORD%
if %ERRORLEVEL% neq 0 (
    echo VPN connection failed
    pause
    goto :EOF
)

echo Successfully connected to %VPN_NAME%
timeout /t 30

:DISCONNECT
rasdial %VPN_NAME% /DISCONNECT
if %ERRORLEVEL% neq 0 (
    echo VPN disconnection failed
) else (
    echo Successfully disconnected from %VPN_NAME%
)

For automated scripts, you might want to run silently and log output:

rasdial "MyVPN" vpn_user mypassword > "%TEMP%\vpn.log" 2>&1

If you encounter problems:

  • Verify the connection name exactly matches what's in Network Connections
  • Check credentials and domain settings
  • Ensure the VPN service is running (use net start "Remote Access Connection Manager")
  • For detailed logging, run with /log switch

While rasdial is the native solution, you can also use:

netsh rasdial "connection_name" user password

Or leverage the Windows Script Host with VBScript for more complex scenarios.


For system administrators and developers maintaining legacy Windows XP systems, automating VPN connections through batch scripts remains a crucial requirement. The native Windows VPN client (rasdial.exe) provides command-line functionality perfect for scripting scenarios.

The primary utility for VPN operations is rasdial.exe, which ships with Windows XP Professional. Its basic syntax:

rasdial "ConnectionName" [username] [password]

Basic connection (storing credentials in script):

rasdial "CorporateVPN" vpnuser P@ssw0rd123

Connection with dynamic credentials (safer approach):

rasdial "CorporateVPN" %VPN_USER% %VPN_PASS%

To terminate the VPN session:

rasdial "CorporateVPN" /DISCONNECT

Or disconnect any active VPN:

rasdial /DISCONNECT

A robust implementation should include error checking:

rasdial "CorporateVPN" %1 %2
if errorlevel 1 (
    echo VPN connection failed
    exit /b 1
) else (
    echo VPN connected successfully
)

Creating a reconnect script that waits between attempts:

:RETRY
rasdial "CorporateVPN" %1 %2
if %errorlevel% neq 0 (
    timeout /t 30
    goto RETRY
)

Checking connection status first:

rasdial | find "CorporateVPN"
if %errorlevel% equ 0 (
    echo VPN is already connected
) else (
    rasdial "CorporateVPN" %1 %2
)

For production environments, consider these security practices:

  • Store credentials in encrypted form using Windows DPAPI
  • Use group policy to limit VPN connection attempts
  • Implement connection timeout monitoring