Sign in to follow this  
Followers 0
royalmarine

help with Autoit script to connect / disconnect vpn, and check if already connected

4 posts in this topic

#1 ·  Posted (edited)

hi, very new to using autoit.

ill briefly describe what im trying to make.

program on desktop, when double clicked, will show a small gui.

Posted Image

when "connect" is clicked, it will open rasphone, and connect a vpn.

it will then map 2 network drives to a server.

when "disconnect" is clicked, it will disconnect the vpn, and remove the net use links to the network drives.

when "exit" is clicked, it closes the program.

everything works fine.

however, i wanted to change the program a small bit, so when its connected, if someone clicks connect again, it will give a prompt saying "already connected"

and when disconnect is clicked while the vpn is disconnected, it will prompt saying "your not connected"

not going to well, and as i said, im very new to this, so im sure my code could be a LOT better.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=C:\Users\mike\Desktop\Form1.kxf
$Form1_1 = GUICreate("server vpn", 288, 238, 192, 124)
$Pic1 = GUICtrlCreatePic("C:\logo.bmp", 0, 0, 284, 132, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
$connect = GUICtrlCreateButton("Connect", 0, 136, 147, 49, $WS_GROUP)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetBkColor(-1, 0x00FF00)
GUICtrlSetCursor (-1, 0)
$disconnect = GUICtrlCreateButton("Disconnect", 0, 184, 147, 49, $WS_GROUP)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlSetCursor (-1, 0)
$exit = GUICtrlCreateButton("Exit", 160, 136, 123, 49, $WS_GROUP)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetBkColor(-1, 0xC0C0C0)
GUICtrlSetCursor (-1, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $connect
                Sleep(500)
                If FileExists("C:\connect.exe") Then
                Run("C:\connect.exe")
                EndIf


        Case $disconnect
                    Sleep(500)
                If FileExists("C:\disconnect.exe") Then
                Run("C:\disconnect.exe")
                EndIf


            Case $exit
                Sleep(200)
                Send("!{F4}")

    EndSwitch
WEnd

here is the connect.exe code

$connect = _GetNetworkConnect()

If $connect Then
    MsgBox(64, "Connections", $connect)

Run("rasphone")
Sleep(300)
Send("{ENTER}")
Sleep(300)
Send("{ENTER}")
Sleep(5000)
Run("cmd")
Sleep(300)
Send("net use x: \\********\*********")
Sleep(100)
Send("{ENTER}")
Sleep(500)
Send("username")
Sleep(100)
Send("{ENTER}")
Sleep(300)
Send("password")
Sleep(100)
Send("{ENTER}")
Sleep(100)
Send("net use y: **********\********")
Sleep(100)
Send("{ENTER}")
Sleep(300)
Send("exit")
Sleep(100)
Send("{ENTER}")
Sleep(2000)
MsgBox(0, "vpn server", "Connected Succesfully" & @CRLF & "Please go to My Computer to access the Drives")

EndIf

Func _GetNetworkConnect()
    Local Const $NETWORK_ALIVE_WAN = 0x2  ;RAS (internet) connection

    Local $aRet, $iResult

    $aRet = DllCall("sensapi.dll", "int", "IsNetworkAlive", "int*", 0)

    If BitAND($aRet[1], $NETWORK_ALIVE_WAN) Then $iResult &= "VPN Server is currently connected" & @LF

    Return $iResult
EndFunc

and finally, here is the disconnect.exe code

Run("rasphone")
Sleep(100)
Send("{ENTER}")
Sleep(100)
Send("{ENTER}")
Sleep(500)
Send("!{F4}")
Sleep(100)
Run("cmd")
Sleep(100)
Send("net use x: /delete")
Sleep(100)
Send("{ENTER}")
Sleep(100)
Send("net use y: /delete")
Sleep(100)
Send("{ENTER}")
Sleep(100)
Send("exit")
Sleep(100)
Send("{ENTER}")
Sleep(300)
MsgBox(0, "vpn server", "Disconnected Succesfully")

as i said, everything works fine the way i want it to, but when i add the extra to connect.exe to check if wan is already connected, it kicks up problems and wont load.

im sure there are much better methods to go about this!

so, if anyone can shed some light, id really appreciate it!

Edited by royalmarine

Share this post


Link to post
Share on other sites



One simple thing to do is to have a flag set to zero on startup. The flag could control the visibility of the connect and disconnect buttons. When it's zero, then it shows the connect button. When it's 1, it shows the the disconnect button. While it doesn't actually poll the connection, it should work just fine and it'll shorten up you form a bit.

Share this post


Link to post
Share on other sites

not quite sure how to implement that, but would it be something similiar to this?

If $iConnect = 1 Then

GUICtrlSetState($hConnect, $GUI_DISABLE)

Else

GUICtrlSetState($hConnect, $GUI_ENABLE)

EndIf

Share this post


Link to post
Share on other sites

bump. hoping someone could shed some light on my program.

thanks!

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