Skip to content

Instantly share code, notes, and snippets.

@aplocher aplocher/AnyConnect.ahk

Last active Apr 20, 2020
Embed
What would you like to do?
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
@lf19902001

This comment has been minimized.

Copy link

lf19902001 commented Aug 9, 2018

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

@mark05e

This comment has been minimized.

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

This comment has been minimized.

Copy link

FabrizioMaio commented Mar 15, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.