2

我是使用 VB 脚本的新手。我正在使用以下代码连接我的 VPN。但问题是在VPN客户端输入“选择”按钮后,第二页显示取决于网络速度。有时在 4 秒内加载,有时在 10 秒后加载。是否有任何代码可以让 VPN 完全加载(例如 IE 的 BUSY 命令)。

    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}"
4

2 回答 2

-1

在下面试试我的代码。请注意,您可能需要调整睡眠时间(以毫秒为单位)。要查看命令提示符中发生的情况,2请将第 9 行中的 更改为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
于 2015-08-04T20:18:00.337 回答
-1

这是一个不等待睡眠超时的改进版本,即在窗口可见时立即连接。这适用于最新版本的 AnyConnect(截至撰写时为 4.10)。

' 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
于 2022-02-28T10:14:42.040 回答