Skip to content

Instantly share code, notes, and snippets.

@gtalton
Last active February 28, 2022 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gtalton/d82d4f96a2a4f99cbfa3 to your computer and use it in GitHub Desktop.
Save gtalton/d82d4f96a2a4f99cbfa3 to your computer and use it in GitHub Desktop.
VBScript to automate logging into CiscoAnyConnect VPN automatically.
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
@gtalton
Copy link
Author

gtalton commented Feb 28, 2022

Nice, thank you! arnileibovits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment