Skip to content

Instantly share code, notes, and snippets.

@aplocher
Last active September 6, 2023 07:18
Show Gist options
  • Save aplocher/f56b8cd494f987d25e94 to your computer and use it in GitHub Desktop.
Save aplocher/f56b8cd494f987d25e94 to your computer and use it in GitHub Desktop.
AutoHotKey script for Cisco AnyConnect. Takes hostname, username, and password as external args. Great for creating multiple shortcuts if you have more than one AnyConnect VPN.
; Create a shortcut to this file in Windows for each AnyConnect VPN configuration you need
; Each shortcut can have a different host, user, and pass defined (3 command line args)
; Usage: AutoHotKey.exe AnyConnect.ahk hostname username password
#SingleInstance force
if 0 < 3 ; The left side of a non-expression if-statement is always the name of a variable.
{
MsgBox This script requires at least 3 incoming parameters but it only received %0%.
ExitApp
}
vpn=%1%
username=%2%
password=%3%
EnvGet,localAppData,LOCALAPPDATA
pathToAnyConnect=C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe
pathToPreferencesXml=%localAppData%\Cisco\Cisco AnyConnect Secure Mobility Client\preferences.xml
Process,Close,vpnui.exe
Sleep,250
FileDelete,%pathToPreferencesXml%
FileAppend,
(
<?xml version="1.0" encoding="UTF-8"?>
<AnyConnectPreferences>
<DefaultUser>%username%</DefaultUser>
<DefaultSecondUser></DefaultSecondUser>
<ClientCertificateThumbprint></ClientCertificateThumbprint>
<ServerCertificateThumbprint></ServerCertificateThumbprint>
<DefaultHostName>%vpn%</DefaultHostName>
<DefaultHostAddress></DefaultHostAddress>
<DefaultGroup></DefaultGroup>
<ProxyHost></ProxyHost>
<ProxyPort></ProxyPort>
<SDITokenType>none</SDITokenType>
<ControllablePreferences></ControllablePreferences>
</AnyConnectPreferences>
), %pathToPreferencesXml%
Run,%pathToAnyConnect%,,,pid
WinWaitActive,ahk_pid %pid%
Sleep,250
ControlGetText,ButtonText,Button1
if ButtonText<>Connect
{
MsgBox,Connect button not found. Automation aborted.
Exit
}
ControlClick,Button1
WinWaitActive,Cisco AnyConnect | %vpn%,Password
Sleep,250
ControlSetText,Edit1,%username%
ControlSetText,Edit2,%password%
Send,{Enter}
; Check for a screen with an Accept button and a title of "Cisco AnyConnect" (but not "Cisco AnyConnect Secure (Mobility Client)...")
; If found, click Accept. Timeout after 8 seconds.
WinWaitActive,Cisco AnyConnect,Accept,8
if not ErrorLevel
{
Sleep,100
ControlClick,Button1
}
Exit
@OwenLiu04
Copy link

Hi, thanks for the script. But unfortunately it doesn't work for me. I use Cisco AnyConnect Secure Mobility Client.

@mark05e
Copy link

mark05e commented Feb 11, 2019

Hi @aplocher thank you this works great!

@lf19902001, create a batch file and call this script using below format:
AnyConnect.ahk <vpn-url> <username> <password>
eg: AnyConnect.ahk vpn.myhostname.com user01 pass01

Also verify the variable pathToAnyConnect -- this will be different for 32bit systems.

@FabrizioMaio
Copy link

Hi, i have create a batch file and call this script using below format,
but it don t work
tks

@dolzenko
Copy link

dolzenko commented Sep 6, 2023

Cheers, very useful. Worth mentioning 1) its for v1.x version of AutoHotKey 2) it can be easily adjusted to e.g. just entering just single password

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