Jump to content

Autoconnect to VPN with hidden cmd window


Recommended Posts

Hello Everyone,

I'm trying to automatically connect to a vpn at logon with a hidden cmd window. It's my understanding a regular batch file doesn't have the capability to hide the cmd window, which is why I believe I need AutoIT3.

So far my attempts at coding have failed. Again, the goal is to connect to a vpn transparently; not having to type anything, click a shortcut, or even look at a window (minimized or otherwise.)

These are the only pieces of code I have:

code to connect to vpn - rasdial <my_vpn> <username> <password>

code to hide window - @SW_Hide (where does it go???)

So, I'm missing the proper way to open cmd (there is run and runwait?), how to input that text (send perhaps?), and where to attach the sw_hide.

Of course, I'm interested in best practice. If there is an easier way other than AutoIT3, I'd like to know. I would be very appreciative if you could attach the finished (whole) code for me to analyze as well. Thank you.

Link to comment
Share on other sites

Oh, I've done this before. Let me give you some code, but I'm going to add some more comments to make this code easier for you to understand. Since it's actual production code.

; So here's the deal here. I have a bunch of client computers all over the state running windows server connector and they use VPN to back up themselves to the
; centrally located server. Sometimes, the internet connection there is unreliable, so I have to take some extra steps to avoid disconnecting and reconnecting the VPN
; if there's only one or two dropped packets. So I make sure it's DEFINITELY down.
Func checkNetwork() ; if 0, then not active
   $result = Ping($serverAddress)
   If($result = 0) Then
   $result = Ping($serverAddress)
   If($result = 0) Then
   $result = Ping($serverAddress)
   If($result = 0) Then ; VPN is disconnected

   ; Here's the meat of what you're looking for.  VPN Connection is the literal name of
   ; the configuration I have setup. You should out YOUR stuff there. 
   RunWait("rasdial ""VPN Connection"" /disconnect", @WorkingDir, @SW_HIDE)
   Sleep(10000)
   ; Then redial the program. 
   RunWait("rasdial ""VPN Connection"" username password", @WorkingDir, @SW_HIDE)
   EndIf
   EndIf
   EndIf
EndFunc
#cs -- OLD
; This is some old production code. 
Func checkNetwork() ; if 0, then not active
   $result = Ping($serverAddress)
   If($result = 0) Then
   RunWait("rasdial ""VPN Connection"" /disconnect", @WorkingDir, @SW_HIDE)
   Sleep(10000)
   RunWait("rasdial ""VPN Connection"" password password", @WorkingDir, @SW_HIDE)
   EndIf
EndFunc
#ce -- OLD

Now, for these operations, I suggest that you use a run wait in accompaniment with checking if the network is down or not. I only check whether or not the VPN connection is down because I don't care if the internet is down or not. When the internet goes back up, this script will connect the computer back to the server and everything will be gravy.

I've got to make some changes to this code to reboot the server connector if it goes into offline mode or something like that, but I haven't figured out how to do that.

Anywho, best of luck to you. Let us know the end results... don't just leave us hanging if the problem is fixed!

-- Alexander Alvonellos

alex@dougwoody.com

Link to comment
Share on other sites

Perfect! Couldn't be happier. These things are amazing when they work. Hope you don't mind I trimmed your code; here's what I used:

RunWait("rasdial ""my_vpn"" username password", @WorkingDir, @SW_HIDE)

All I needed for a simple autoconnect at logon; just pop the saved script into the startup folder.

I do have a lingering question though; what does the @WorkingDir do?

Again, thank you! Means a lot to be able to come to a forum and get such a quick, helpful response.

Link to comment
Share on other sites

@WorkingDir is a macro for the current directory (consider it as a pre-defined variable varying from system to system)

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Link to comment
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
 Share

  • Recently Browsing   0 members

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