Sign in to follow this  
Followers 0
jgabriel

Connect L2TP VPN Script

1 post in this topic

Hello,

just finished my first script with AutoIt, and want to share with you all. Not fency or anything too dificult, but it might be helpfull for more people. Comments and window titles are in Brazilian Portuguese, but I will be glad to explain anything. The script just dial a previusly created VPN connection (my case is a L2TP VPN, but shoul work with any VPN or Dial-UP Connection), analises if the connection is estabilished, trying again if any error comes up, and after a successful connection, the script adds a static route (using "route.exe add" shell command, that is required by most L2TP VPN) and finishes. Here is my code:

;Script para conexão via VPN e utilização do Cisco IP-Communicator
;Autor: João Gabriel Pires Baptista
;Equipe: UNITERA VoIP

#include <GUIConstants.au3>
#include <Constants.au3>

Opt("TrayIconHide", 1)          ;0=show, 1=hide tray icon

Dim $strConnName = "VPN_CST_VOIP"
Dim $strUserName 
Dim $strUserPass 

;Cria GUI para digitacao do nome de usuario e senha da VPN
$guiVPN = GUICreate("Informações para conexão VPN L2TP", 320, 120)
GUICtrlCreateLabel("Nome de usuario:",10,  10, 80, 40)
$guiVPNUsername = GUICtrlCreateInput( "", 80,  10, 220, 24)
GUICtrlCreateLabel("Senha:", 10, 48, 50, 20)
$guiVPNPassword = GUICtrlCreateInput( "", 80, 40, 220, 24, $ES_PASSWORD)
$guiVPNConectar = GUICtrlCreateButton("Conectar-se", 30, 80, 100, 30, $BS_DEFPUSHBUTTON)
$guiVPNCancelar = GUICtrlCreateButton("Cancelar", 180, 80, 100, 30)
Mostra_GUI()

;FUNÇÔES AUXILIARES

;Função que mostra a GUI para digitação do nome de usuário e senha
Func Mostra_GUI()
    GUISetState(@SW_SHOW)   
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Quit()
            Case $msg = $guiVPNConectar
                GUISetState(@SW_HIDE)
                Conecta_VPN()  
            Case $msg = $guiVPNCancelar
                Quit()    
        EndSelect
    Wend
EndFunc

;Função que sai do script
Func Quit() 
    RunAsSet()
    Exit 0
EndFunc

;Conectar VPN
Func Conecta_VPN()
    Dim $strErroVPN
    Run(@SystemDir & "\rasphone.exe -d " & $strConnName)
    WinWaitActive("Conectar " & $strConnName)
    ControlFocus("Conectar " & $strConnName, "", 1104)
    ControlSetText("Conectar " & $strConnName, "", 1104, "")
    $strUserName = GUICtrlRead($guiVPNUsername)
    ControlSend("Conectar " & $strConnName, "", 1104, $strUserName)
    ControlFocus("Conectar " & $strConnName, "", 1103)
    $strUserPass = GUICtrlRead($guiVPNPassword)
    ControlSend("Conectar " & $strConnName, "", 1103, $strUserPass)
    ControlClick("Conectar " & $strConnName, "",1590)   
    TrataErro_VPN()
EndFunc

;Função que trata as mensagens de erro durante a chamada de uma conexão VPN
Func TrataErro_VPN()
    Sleep(100)
    WinWaitActive("Conectando-se a " & $strConnName)    
    WinWaitNotActive("Conectando-se a " & $strConnName)
    If WinExists("Erro ao conectar-se a " & $strConnName) = 1 Then
        Sleep(100)
        $strErroVPN = ControlGetText("Erro ao conectar-se a " & $strConnName, "", 1070)     
        If StringInStr($strErroVPN, "678") = 0 Then
            Sleep(100)
            ControlClick("Erro ao conectar-se a " & $strConnName, "", 2)
            MsgBox(48, "Erro de conexão", "Verifique seu nome de usuário e senha, e tente novamente.")
            Mostra_GUI()
        Else
            Sleep(50)           
            ControlClick("Erro ao conectar-se a " & $strConnName, "", 1)
            TrataErro_VPN()
        EndIf
        Sleep(100)
    Else
        $strGetStdOut = Run("rasdial.exe", @SystemDir, @SW_HIDE, $STDOUT_CHILD)
        $strIsConnected = StdoutRead($strGetStdOut)
        If StringInStr($strIsConnected, "Conectado a") = 1 Then         
            CriaRota()
        Else
            Quit()
        EndIf
    EndIf
EndFunc

;Cria uma rota estática utilizando-se do IP obtido ao se conectar a VPN
Func CriaRota()
    Dim $strAdminUser = "*****l" 
    Dim $strAdminPass = "*****"
    Dim $strInicioIP = "XXX.XXX.XXX."
    RunAsSet($strAdminUser, @ComputerName, $strAdminPass)   
    Select
        Case StringInStr(@IPAddress1, $strInicioIP) <> 0
            Run(@ComSpec & " /c " & 'route.exe add YYY.YYY.YYY.YYY mask 255.252.0.0 ' & @IPAddress1, "", @SW_HIDE)
        Case StringInStr(@IPAddress2, $strInicioIP) <> 0
            Run(@ComSpec & " /c " & 'route.exe add YYY.YYY.YYY.YYY mask 255.252.0.0 ' & @IPAddress2, "", @SW_HIDE)
        Case StringInStr(@IPAddress3, $strInicioIP) <> 0
            Run(@ComSpec & " /c " & 'route.exe add YYY.YYY.YYY.YYY mask 255.252.0.0 ' & @IPAddress3, "", @SW_HIDE)
        Case StringInStr(@IPAddress4, $strInicioIP) <> 0
            Run(@ComSpec & " /c " & 'route.exe add YYY.YYY.YYY.YYY mask 255.252.0.0 ' & @IPAddress4, "", @SW_HIDE)
    EndSelect
    Quit()
EndFunc

PS.: IP addresses chaged to XXX.XXX.XXX.XXX and usernames and passwords masked.

Cheers from the Paradise!

Share this post


Link to post
Share on other sites



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
Sign in to follow this  
Followers 0