Last active
February 28, 2022 13:02
-
-
Save gtalton/d82d4f96a2a4f99cbfa3 to your computer and use it in GitHub Desktop.
VBScript to automate logging into CiscoAnyConnect VPN automatically.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
'On Error Resume Next | |
Dim i, WshShell, argv, pingTarget, password | |
Set WshShell = WScript.CreateObject("WScript.Shell") | |
Set argv = WScript.Arguments | |
pingTarget = argv(0) | |
password = argv(1) | |
'Keep looping back through the script so that it stays running | |
i = 0 | |
Do While i = 0 | |
If ServerPing(pingTarget) Then | |
'Ping was successful | |
Else | |
'Ping was not successful | |
Call ReConnectVPN(WshShell, password) | |
End If | |
WScript.Sleep 10000 | |
Loop | |
Function ReConnectVPN(WshShell, strPassword) | |
WshShell.Run """%PROGRAMFILES(x86)%\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe""" | |
WScript.Sleep 1000 | |
WshShell.AppActivate "Cisco AnyConnect Secure Mobility Client" | |
WshShell.SendKeys "{TAB}" | |
WshShell.SendKeys "{TAB}" | |
WshShell.SendKeys "{ENTER}" | |
WScript.Sleep 4000 | |
WshShell.SendKeys strPassword | |
WshShell.SendKeys "{TAB}" | |
WshShell.SendKeys "{ENTER}" | |
WScript.Sleep 4000 | |
WshShell.SendKeys "{ENTER}" | |
End Function | |
'******************************************************************************** | |
'ServerPing Function | |
'Ping the server and if available return true, otherwise false | |
'******************************************************************************** | |
Function ServerPing(strServerName) | |
Dim PINGFlag | |
Set WSHShell = CreateObject("WScript.Shell") | |
PINGFlag = Not CBool(WSHShell.Run("ping -n 5 " & strServerName, 0, True)) | |
If PINGFlag = True Then | |
'Ping was successful | |
ServerPing = True | |
Else | |
'Ping not successful | |
ServerPing = False | |
End If | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is an improved version that doesn't wait for sleep timeouts, i.e. connects as soon as the window becomes visible. This script doesn't have any pinging, though.