Jump to content

Create new VPN / PPTP connection in Windows XP


Mavantix
 Share

Recommended Posts

I put this script together to automate creating PPTP VPN connections on Windows XP computers. It's only tested on Windows XP SP2 English, but seems to work well on the machines I've tested it on. It's my first AutoIT script, hope it's useful to someone. You'll need to change the name and IP info at the top.

Would love to hear feedback or code improvements too, since I don't yet know all the stuff AutoIT can do...

Thanks to the dev's for AutoIT, it seems very cool so far!

;
; AutoIt Version: 3.0
; Language:       English
; Platform:       WinXP SP2 (Tested)
; Author:         Kenneth Padgett / IT Lifesaver / kenneth@itlifesaver.com
; Version:        1.0
;
; Script Function:
;  Creates a PPTP VPN connection on Windows XP clients
;  to connect to the server info you provide
;


#NoTrayIcon
#compiler_icon=itlifesaver.ico
#include <GUIConstants.au3> 

$COMPANY_NAME = "Work VPN" ; name of the VPN icon
$COMPANY_IP = "123.123.123.123" ; can be DNS name too
$ConfigureDNS = True ; true if script should set DNS servers, false if not
$COMPANY_DNS1 = "192.168.1.10"
$COMPANY_DNS2 = "" ; optional
$NotDefaultGW = True ; true if script should uncheck the 'Use remote network as default gateway', if you want inet traffic to go through the VPN, set to false

$DELAY = 100

$answer = MsgBox(4, "VPN Connection", "This script will create a VPN connection to " & $COMPANY_NAME & ", Ready?")
If $answer = 7 Then
    Exit
EndIf

; Prompt user for VPN login info
$frmInformation = GUICreate("Enter Information", 287, 194, 193, 115)
$lblUserName = GUICtrlCreateLabel("User Name:", 16, 40, 60, 17)
$lblPassword = GUICtrlCreateLabel("Password:", 16, 80, 53, 17)
$txtUserName = GUICtrlCreateInput("", 112, 40, 153, 21)
$txtPassword = GUICtrlCreateInput("", 112, 80, 153, 21, BitOR($ES_PASSWORD,$ES_AUTOHSCROLL))
$lblPassword2 = GUICtrlCreateLabel("Confirm Password:", 16, 120, 91, 17)
$txtPassword2 = GUICtrlCreateInput("", 112, 120, 153, 21, BitOR($ES_PASSWORD,$ES_AUTOHSCROLL))
$btnOK = GUICtrlCreateButton("&OK", 200, 160, 75, 25, 0)
$lblInfo = GUICtrlCreateLabel("Enter your VPN Login Information Below!", 48, 8, 196, 17)
GUISetState(@SW_SHOW)
  
While 1 
    $nMsg = GUIGetMsg() 
    Switch $nMsg 
    Case $btnOK
        If GUICtrlRead($txtPassword) <> GUICtrlRead($txtPassword2) Then
            MsgBox (16, "Error", "Passwords do not match! Try again.")
        Else
            $Username = GUICtrlRead($txtUsername)
            $Password = GUICtrlRead($txtPassword)
            ExitLoop
        EndIf
    Case $GUI_EVENT_CLOSE 
        Exit
  
    EndSwitch 
WEnd
GUISetState(@SW_HIDE)
 
; Run Network Setup
Run("control ncpa.cpl")
WinWaitActive("Network Connections")

; Check if VPN by same name already exists, since it'll break script later if Windows add's a number at the end of the name...
$ControlID = ControlListView("Network Connections", "", "SysListView321", "FindItem", $COMPANY_NAME, "Virtual Private Network")
If $ControlID <> -1 Then
    $answer = MsgBox(4404, "Error", "VPN Connection to " & $COMPANY_NAME & " already exists! Remove it and recreate it?")
    If $answer = 6 Then
        ControlListView("Network Connections", "", "SysListView321", "Select", $ControlID)
        Send("{DEL}")
        WinWaitActive("Confirm Connection Delete")
        Send("!y")
        Sleep($DELAY)
    Else
        MsgBox(16, "Exit", "Script stopped by user")
        Exit
    EndIf
EndIf

; open new connection wizard from file menu
Send("!f")
Send("n")

WinWaitActive("New Connection Wizard")
Send("!n")
Sleep($DELAY)

; What do you want to do?
Send("!o")
Sleep($DELAY)
Send("!n")
Sleep($DELAY)

; How do you want to connect to the network at your workplace?
Send("!v")
Sleep($DELAY)
Send("!n")
Sleep($DELAY)

; Specifiy a name for this connection to your workplace.
Send($COMPANY_NAME)
Send("!n")
Sleep($DELAY)

; Windows can make sure the public network is connected first.
Send("!d")
Sleep($DELAY)
Send("!n")
Sleep($DELAY)

; What is the name or address of the VPN server?
Send($COMPANY_IP)
Send("!n")
Sleep($DELAY)

; Wizard Complete, do we want a desktop shortcut?
Send("!s")
Sleep($DELAY)
Send("{ENTER}")

WinWaitClose("New Connection Wizard")

WinWaitActive("Connect " & $COMPANY_NAME)

Send($Username)
Send("{TAB}")
Send($Password)
Sleep($DELAY)
Send("!s") ; save password...
Send("!a") ; for anyone who uses this computer, use "!n" for 'Me only'
Sleep($DELAY)

If $ConfigureDNS Or $NotDefaultGW Then

    Send("!o") ; open Properties

    WinWaitActive($COMPANY_NAME & " Properties")
    Send("^{TAB 3}")
    Sleep($DELAY)

    Send("!o") ; highlight 'This connection uses the following items:'...
    Sleep($DELAY)
    ; select TCP/IP from the listview:
    $ControlID = ControlListView($COMPANY_NAME & " Properties", "", "SysListView321", "FindItem", "Internet Protocol (TCP/IP)")
    If $ControlID = -1 Then
        MsgBox(16, "Error", "Could not select TCP/IP, please finish setup manually")
        Exit
    EndIf
    ControlListView($COMPANY_NAME & " Properties", "", "SysListView321", "Select", $ControlID)
    Sleep($DELAY)

    Send("!r") ; open properties
    WinWaitActive("Internet Protocol (TCP/IP) Properties")

    If $ConfigureDNS Then
        Send("!e") ; Use the following DNS server addresses
        Sleep($DELAY)
        Send($COMPANY_DNS1)
        Sleep($DELAY)

        If $COMPANY_DNS2 <> "" Then
            Send("{TAB}")
            Send($COMPANY_DNS2)
            Sleep($DELAY)
        Endif
    EndIf

    If $NotDefaultGW Then
        Send("!v")
        WinWaitActive("Advanced TCP/IP Settings")

        Send("!u") ; Uncheck 'Use default gateway on remote network'
        ControlClick("Advanced TCP/IP Settings", "", 1) ; click OK
    EndIf

    WinWaitActive("Internet Protocol (TCP/IP) Properties")
    ControlClick("Internet Protocol (TCP/IP) Properties", "", 1) ; click OK

    WinWaitActive($COMPANY_NAME & " Properties")
    ControlClick($COMPANY_NAME & " Properties", "", 1) ; click OK
EndIf

WinClose("Network Connections")
Link to comment
Share on other sites

  • 2 weeks later...

Seems nice and probably usefull for me too as i will have to do similar thing at work, but maybe you could try achieving same result thru registry instead of doing it thru GUI navigation?

I'd immagine you can, though I wouldn't know where to begin to find what registry keys would need to be setup. I gess google and a registry change watcher would probably do it... For me, the GUI navigation works for now.

Link to comment
Share on other sites

  • 1 year later...

I have created an executable which connects to a pptp vpn server and falls back to another one when it does not connect without the need to add a pptp connection it even connects to citrix and closes the connection when you exit citrix.

This executable saves me lots of time helping users to start work remote. Al they need is this compiled executable on an USB stick! :)

The solution exists of a couple of files

The first thing you need is Thinica available for download here http://www.thinstall.com/products/citrix_i...nt_download.php

Second thing you need is an .ica file to launch the desktop or citrix application, in my script i name it desktop.ica

Run Thinica for the first time and choose to start Program Neighbourhood by default.

create a pbk connection file for you VPN connection (example below) It seems very long but it are all default values except the name and the server adress

[VPN]
Encoding=1
Type=2
AutoLogon=0
UseRasCredentials=1
DialParamsUID=194593
Guid=F707C18E0BE93A4F9E547A15A96F89F2
BaseProtocol=1
VpnStrategy=2
ExcludedProtocols=0
LcpExtensions=1
DataEncryption=256
SwCompression=1
NegotiateMultilinkAlways=0
SkipNwcWarning=0
SkipDownLevelDialog=0
SkipDoubleDialDialog=0
DialMode=1
DialPercent=75
DialSeconds=120
HangUpPercent=10
HangUpSeconds=120
OverridePref=15
RedialAttempts=3
RedialSeconds=60
IdleDisconnectSeconds=0
RedialOnLinkFailure=0
CallbackMode=0
CustomDialDll=
CustomDialFunc=
CustomRasDialDll=
AuthenticateServer=0
ShareMsFilePrint=1
BindMsNetClient=1
SharedPhoneNumbers=0
GlobalDeviceSettings=0
PrerequisiteEntry=
PrerequisitePbk=
PreferredPort=VPN3-0
PreferredDevice=WAN-minipoort (L2TP)
PreferredBps=0
PreferredHwFlow=1
PreferredProtocol=1
PreferredCompression=1
PreferredSpeaker=1
PreferredMdmProtocol=0
PreviewUserPw=1
PreviewDomain=0
PreviewPhoneNumber=0
ShowDialingProgress=1
ShowMonitorIconInTaskBar=1
CustomAuthKey=-1
AuthRestrictions=608
TypicalAuth=2
IpPrioritizeRemote=1
IpHeaderCompression=0
IpAddress=0.0.0.0
IpDnsAddress=0.0.0.0
IpDns2Address=0.0.0.0
IpWinsAddress=0.0.0.0
IpWins2Address=0.0.0.0
IpAssign=1
IpNameAssign=1
IpFrameSize=1006
IpDnsFlags=0
IpNBTFlags=1
TcpWindowSize=0
UseFlags=0
IpSecFlags=0
IpDnsSuffix=

NETCOMPONENTS=
ms_server=1
ms_msclient=1

MEDIA=rastapi
Port=VPN3-0
Device=WAN-minipoort (L2TP)

DEVICE=vpn
PhoneNumber=x.x.x.x
AreaCode=
CountryCode=1
CountryID=1
UseDialingRules=0
Comment=
LastSelectedPhone=0
PromoteAlternates=0
TryNextAlternateOnFail=1

[VPNfallback]
Encoding=1
Type=2
AutoLogon=0
UseRasCredentials=1
DialParamsUID=13698671
Guid=3727D69CF847FE4680B5B4FF1C29D970
BaseProtocol=1
VpnStrategy=2
ExcludedProtocols=0
LcpExtensions=1
DataEncryption=256
SwCompression=1
NegotiateMultilinkAlways=0
SkipNwcWarning=0
SkipDownLevelDialog=0
SkipDoubleDialDialog=0
DialMode=1
DialPercent=75
DialSeconds=120
HangUpPercent=10
HangUpSeconds=120
OverridePref=15
RedialAttempts=3
RedialSeconds=60
IdleDisconnectSeconds=0
RedialOnLinkFailure=0
CallbackMode=0
CustomDialDll=
CustomDialFunc=
CustomRasDialDll=
AuthenticateServer=0
ShareMsFilePrint=1
BindMsNetClient=1
SharedPhoneNumbers=0
GlobalDeviceSettings=0
PrerequisiteEntry=
PrerequisitePbk=
PreferredPort=VPN3-0
PreferredDevice=WAN-minipoort (L2TP)
PreferredBps=0
PreferredHwFlow=1
PreferredProtocol=1
PreferredCompression=1
PreferredSpeaker=1
PreferredMdmProtocol=0
PreviewUserPw=1
PreviewDomain=0
PreviewPhoneNumber=0
ShowDialingProgress=1
ShowMonitorIconInTaskBar=1
CustomAuthKey=-1
AuthRestrictions=608
TypicalAuth=2
IpPrioritizeRemote=1
IpHeaderCompression=0
IpAddress=0.0.0.0
IpDnsAddress=0.0.0.0
IpDns2Address=0.0.0.0
IpWinsAddress=0.0.0.0
IpWins2Address=0.0.0.0
IpAssign=1
IpNameAssign=1
IpFrameSize=1006
IpDnsFlags=0
IpNBTFlags=1
TcpWindowSize=0
UseFlags=0
IpSecFlags=0
IpDnsSuffix=

NETCOMPONENTS=
ms_server=1
ms_msclient=1

MEDIA=rastapi
Port=VPN3-0
Device=WAN-minipoort (L2TP)

DEVICE=vpn
PhoneNumber=x.x.x.x
AreaCode=
CountryCode=31
CountryID=31
UseDialingRules=0
Comment=
LastSelectedPhone=0
PromoteAlternates=0
TryNextAlternateOnFail=1

Ip adresses are translated to x.x.x.x the file name for this conectionfile in my script is vpn.pbk

this are all the requirements for this script to work.

script source can be found below.

#include <GUIConstants.au3>
Global $GUIWidth
Global $GUIHeight

$GUIWidth = 220
$GUIHeight = 150
; install necessary files into temp dir
DirCreate(@TempDir & "\VPNCLIENT")
fileinstall("c:\vpnclient\thinica.exe",@TempDir & "\VPNCLIENT\thinica.exe",1)
fileinstall("c:\vpnclient\vpn.pbk",@TempDir & "\VPNCLIENT\vpn.pbk",1)
fileinstall("c:\vpnclient\desktop.ica",@TempDir & "\VPNCLIENT\desktop.ica",1)
DirCreate(@TempDir & "\VPNCLIENT\ThinICAData")
fileinstall("c:\vpnclient\ThinICAData\wfclient.ini",@TempDir & "\VPNCLIENT\ThinICAData\wfclient.ini",1)
;Create window
GUICreate("VPN Client", $GUIWidth, $GUIHeight)
GUICtrlCreateLabel("VPN Gebruikersnaam",10,10)
$username = GUICtrlCreateInput("", 10, 30, 200)
GUICtrlCreateLabel("VPN wachtwoord",10,60)
$password = GUICtrlCreateInput("", 10, 80, 200,20,0x20)

;Create an "OK" button
$OK_Btn = GUICtrlCreateButton("OK", 40, 120, 70, 25)

;Create a "CANCEL" button
$Cancel_Btn = GUICtrlCreateButton("Cancel", 120, 120, 70, 25)
GUISetState(@SW_SHOW)
While 1
 ;After every loop check if the user clicked something in the GUI window
    $msg = GUIGetMsg()

   Select
   
    ;Check if user clicked on the close button
      Case $msg = $GUI_EVENT_CLOSE
       ;Destroy the GUI including the controls
         GUIDelete()
       ;Exit the script
         Exit
         
    ;Check if user clicked on the "OK" button
 Case $msg = $OK_Btn
    $user = GUICtrlRead($username)
    $pass = GUICtrlRead($password)
    GUISetState(@SW_HIDE)
;start vpn connection script
    $result = RunWait ("rasdial vpn " & $user & " " & $pass & ' /phonebook:"' & @tempdir & '\vpnclient\vpn.pbk"')
; if the first server does not respond failover to second server
    If $result = 800 Then
        $result = RunWait ("rasdial vpnfallback " & $user & " " & $pass & ' /phonebook:"' & @tempdir & '\vpnclient\vpn.pbk"')
    endif
    if $result == 691 Then
        MsgBox(4096, "gebruikersnaam of wachtwoord onjuist", "Uw gebruikersnaam of wachtwoord is niet juist")
    else
; if both servers do not respond give error message
    if $result == 800 Then
        MsgBox(4096, "kan geen verbinding maken", "server niet bereikbaar")
    exit
    endif
; start ica and wait for it to close
    Runwait(@tempdir & "\vpnclient\thinica /app:desktop.ica",@tempdir & "\vpnclient\")
    sleep(5000)
    ProcessWaitClose("wfica32.exe")
; disconnect VPN
    RunWait ("rasdial vpn /disconnect")
    RunWait ("rasdial vpnfallback /disconnect")
    endif
    exit
;Check if user clicked on the "CANCEL" button
    Case $msg = $Cancel_Btn
           ;Exit the script
         Exit
   EndSelect
WEnd

compile this script and you are ready :P

I hope someone likes this script and can use it in production!

Edited by paulusb
Link to comment
Share on other sites

  • 1 year later...
  • 3 weeks later...

I put this script together to automate creating PPTP VPN connections on Windows XP computers. It's only tested on Windows XP SP2 English, but seems to work well on the machines I've tested it on. It's my first AutoIT script, hope it's useful to someone. You'll need to change the name and IP info at the top.

Would love to hear feedback or code improvements too, since I don't yet know all the stuff AutoIT can do...

Thanks to the dev's for AutoIT, it seems very cool so far!

;
; AutoIt Version: 3.0
; Language:       English
; Platform:       WinXP SP2 (Tested)
; Author:         Kenneth Padgett / IT Lifesaver / kenneth@itlifesaver.com
; Version:        1.0
;
; Script Function:
;  Creates a PPTP VPN connection on Windows XP clients
;  to connect to the server info you provide
;


#NoTrayIcon
#compiler_icon=itlifesaver.ico
#include <GUIConstants.au3> 

$COMPANY_NAME = "Work VPN" ; name of the VPN icon
$COMPANY_IP = "123.123.123.123" ; can be DNS name too
$ConfigureDNS = True ; true if script should set DNS servers, false if not
$COMPANY_DNS1 = "192.168.1.10"
$COMPANY_DNS2 = "" ; optional
$NotDefaultGW = True ; true if script should uncheck the 'Use remote network as default gateway', if you want inet traffic to go through the VPN, set to false

$DELAY = 100

$answer = MsgBox(4, "VPN Connection", "This script will create a VPN connection to " & $COMPANY_NAME & ", Ready?")
If $answer = 7 Then
    Exit
EndIf

; Prompt user for VPN login info
$frmInformation = GUICreate("Enter Information", 287, 194, 193, 115)
$lblUserName = GUICtrlCreateLabel("User Name:", 16, 40, 60, 17)
$lblPassword = GUICtrlCreateLabel("Password:", 16, 80, 53, 17)
$txtUserName = GUICtrlCreateInput("", 112, 40, 153, 21)
$txtPassword = GUICtrlCreateInput("", 112, 80, 153, 21, BitOR($ES_PASSWORD,$ES_AUTOHSCROLL))
$lblPassword2 = GUICtrlCreateLabel("Confirm Password:", 16, 120, 91, 17)
$txtPassword2 = GUICtrlCreateInput("", 112, 120, 153, 21, BitOR($ES_PASSWORD,$ES_AUTOHSCROLL))
$btnOK = GUICtrlCreateButton("&OK", 200, 160, 75, 25, 0)
$lblInfo = GUICtrlCreateLabel("Enter your VPN Login Information Below!", 48, 8, 196, 17)
GUISetState(@SW_SHOW)
  
While 1 
    $nMsg = GUIGetMsg() 
    Switch $nMsg 
    Case $btnOK
        If GUICtrlRead($txtPassword) <> GUICtrlRead($txtPassword2) Then
            MsgBox (16, "Error", "Passwords do not match! Try again.")
        Else
            $Username = GUICtrlRead($txtUsername)
            $Password = GUICtrlRead($txtPassword)
            ExitLoop
        EndIf
    Case $GUI_EVENT_CLOSE 
        Exit
  
    EndSwitch 
WEnd
GUISetState(@SW_HIDE)
 
; Run Network Setup
Run("control ncpa.cpl")
WinWaitActive("Network Connections")

; Check if VPN by same name already exists, since it'll break script later if Windows add's a number at the end of the name...
$ControlID = ControlListView("Network Connections", "", "SysListView321", "FindItem", $COMPANY_NAME, "Virtual Private Network")
If $ControlID <> -1 Then
    $answer = MsgBox(4404, "Error", "VPN Connection to " & $COMPANY_NAME & " already exists! Remove it and recreate it?")
    If $answer = 6 Then
        ControlListView("Network Connections", "", "SysListView321", "Select", $ControlID)
        Send("{DEL}")
        WinWaitActive("Confirm Connection Delete")
        Send("!y")
        Sleep($DELAY)
    Else
        MsgBox(16, "Exit", "Script stopped by user")
        Exit
    EndIf
EndIf

; open new connection wizard from file menu
Send("!f")
Send("n")

WinWaitActive("New Connection Wizard")
Send("!n")
Sleep($DELAY)

; What do you want to do?
Send("!o")
Sleep($DELAY)
Send("!n")
Sleep($DELAY)

; How do you want to connect to the network at your workplace?
Send("!v")
Sleep($DELAY)
Send("!n")
Sleep($DELAY)

; Specifiy a name for this connection to your workplace.
Send($COMPANY_NAME)
Send("!n")
Sleep($DELAY)

; Windows can make sure the public network is connected first.
Send("!d")
Sleep($DELAY)
Send("!n")
Sleep($DELAY)

; What is the name or address of the VPN server?
Send($COMPANY_IP)
Send("!n")
Sleep($DELAY)

; Wizard Complete, do we want a desktop shortcut?
Send("!s")
Sleep($DELAY)
Send("{ENTER}")

WinWaitClose("New Connection Wizard")

WinWaitActive("Connect " & $COMPANY_NAME)

Send($Username)
Send("{TAB}")
Send($Password)
Sleep($DELAY)
Send("!s") ; save password...
Send("!a") ; for anyone who uses this computer, use "!n" for 'Me only'
Sleep($DELAY)

If $ConfigureDNS Or $NotDefaultGW Then

    Send("!o") ; open Properties

    WinWaitActive($COMPANY_NAME & " Properties")
    Send("^{TAB 3}")
    Sleep($DELAY)

    Send("!o") ; highlight 'This connection uses the following items:'...
    Sleep($DELAY)
    ; select TCP/IP from the listview:
    $ControlID = ControlListView($COMPANY_NAME & " Properties", "", "SysListView321", "FindItem", "Internet Protocol (TCP/IP)")
    If $ControlID = -1 Then
        MsgBox(16, "Error", "Could not select TCP/IP, please finish setup manually")
        Exit
    EndIf
    ControlListView($COMPANY_NAME & " Properties", "", "SysListView321", "Select", $ControlID)
    Sleep($DELAY)

    Send("!r") ; open properties
    WinWaitActive("Internet Protocol (TCP/IP) Properties")

    If $ConfigureDNS Then
        Send("!e") ; Use the following DNS server addresses
        Sleep($DELAY)
        Send($COMPANY_DNS1)
        Sleep($DELAY)

        If $COMPANY_DNS2 <> "" Then
            Send("{TAB}")
            Send($COMPANY_DNS2)
            Sleep($DELAY)
        Endif
    EndIf

    If $NotDefaultGW Then
        Send("!v")
        WinWaitActive("Advanced TCP/IP Settings")

        Send("!u") ; Uncheck 'Use default gateway on remote network'
        ControlClick("Advanced TCP/IP Settings", "", 1) ; click OK
    EndIf

    WinWaitActive("Internet Protocol (TCP/IP) Properties")
    ControlClick("Internet Protocol (TCP/IP) Properties", "", 1) ; click OK

    WinWaitActive($COMPANY_NAME & " Properties")
    ControlClick($COMPANY_NAME & " Properties", "", 1) ; click OK
EndIf

WinClose("Network Connections")

I´m getting an error on line 83.

Variable used without being declared...

Link to comment
Share on other sites

  • 4 months later...
  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...