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 |
My current employer does a 48hr disconnect on VPN. Give the script a ping target and your password as the arguments .. when the ping target stops responding .. the script assumes you've been disconnected and will reconnect with your password.
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.
' 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
Nice, thank you! arnileibovits
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My current employer does a 48hr disconnect on VPN. Give the script a ping target and your password as the arguments .. when the ping target stops responding .. the script assumes you've been disconnected and will reconnect with your password.