'VB script on Cisco Anyconnect VPN

I am new to use VB script. I am using the below code to connect my VPN. But the problem is that after entering "select" button in VPN client, the second page display is depending on Network Speed. Sometimes it is loaded within 4 sec, sometimes after 10 sec. Is there any code where i can get the VPN is fully loaded or not (like BUSY command for IE).

    set WshShell=Wscript.CreateObject("Wscript.Shell")
    WshShell.Run("""C:\\Program Files\Cisco\Anyconnect\vpnui.exe""")
    WScript.Sleep 500

    WshShell.SendKeys "{ENTER}"
    WScript.Sleep 500 
    WshShell.SendKeys "username"
    WshShell.SendKeys "rsa_no"
    WshShell.SendKeys "password"
    WScript.Sleep 500 
    WshShell.SendKeys "{ENTER}"


Solution 1:[1]

Whilst not vb script, I think this approach should still work.

I have the vpncli directory in my %path%

I have a batch file:

vpncli.exe connect xyz.123.com  -s < d:\vpncreds.txt

with the credentials in a separate file (d:\vpncreds.txt):

username
password
y

Note: you need an empty line at the end.

This works fine here, and wonder if you take the credentials out of your VB script and put them in a separate file, it might achieve what you need to.

Also, if your credentials change, you only need to change the one file, and not the potential handful of script files if you access the vpn in multiple scripts.

Solution 2:[2]

Try my code below. Note that you may have to adjust sleep times (in milliseconds). To see what's happening in the command prompt, change the 2 in the 9th line to a 1.

Dim host, username, password, pathToClient
host = "yourHostURL"
username = "yourUsername"
password = "yourPassword"
pathToClient = "C:\Program Files {(}x86{)}\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe"

Set ws = WScript.CreateObject("WScript.Shell")
ws.run("TASKKILL.exe /F /IM vpnui.exe"), 0, false
ws.run("cmd.exe"), 2, false
ws.AppActivate("Command Prompt")
WScript.Sleep 300
ws.SendKeys """" & pathToClient & """ connect " & host & "~"
WScript.Sleep 1000
ws.SendKeys(username & "~")
WScript.Sleep 50
ws.SendKeys(password & "~")
ws.run("TASKKILL.exe /F /IM cmd.exe"), 0, false

Solution 3:[3]

Here is an improved version that doesn't wait for sleep timeouts, i.e. connects as soon as the window becomes visible. This works with the latest version of AnyConnect (4.10 as of writing).

' Script to automatically connect to Cisco AnyConnect.
' This script assumes that you have already set up your connection.

Const Password = "[YOUR PASSWORD]" ' Enter your password here
Const ConnectionUrl = "[YOUR CONNECTION URL]" ' Enter the URL of your endpoint (without HTTP prefix)

' Copy password to clipboard in case something goes wrong (to connect manually)
CopyToClipboard(Password)

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run """%PROGRAMFILES(x86)%\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"""

ActivateWindow("Cisco AnyConnect Secure Mobility Client")
WshShell.SendKeys "{ENTER}"

ActivateWindow("Cisco AnyConnect | " + ConnectionUrl)
WshShell.SendKeys "{TAB}"
WshShell.SendKeys Password
WshShell.SendKeys "{ENTER}"


Function ActivateWindow(title)
  Const Step = 100
  Const Timeout = 10000
  Dim Result
  Dim Counter

  For Counter = 0 To Timeout Step Step
    Result = WshShell.AppActivate(title)
    If Result Then Exit For

    WScript.Sleep Step
  Next

  If Result = False Then
    MsgBox("Window '" + title + "' not found.")
    WScript.Quit
  End If
End Function


Function CopyToClipboard(Input)
  If IsNull(Input) Then
    Set Clipboard = CreateObject("HTMLFile").parentWindow.clipboardData.getData("Text")
    If IsNull(Clipboard) Then Clipboard = ""
  Else
    CreateObject("WScript.Shell").Run _
      "mshta.exe javascript:eval(""document.parentWindow.clipboardData.setData('text','" _
      & Replace(Replace(Replace(Input, "'", "\\u0027"), """","\\u0022"),Chr(13),"\\r\\n") & "');window.close()"")", _
      0,True
  End If
End Function

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tym
Solution 2 Joel Christophel
Solution 3 arni