Scripting

AppleScript to automatically connect to Cisco Anyconnect VPN!

We can connect to Cisco AnyConnect VPN without providing the password and Accept each time.

In my case, I’ve used Exchange. Microsoft Outlook (Exchange) uses the same password as I used for VPN. In Mac OS, we can retrieve this Exchange password from the keychain access app through /usr/bin/security.

But to access the keychain through security first we need to allow the app to access it. To do it, when we first run this app it will asks for the password to access the Exchange’s keychain. Once we entered the password we need to click “Always allow” instead of Allow (by clicking Always allow it will add entry in Access Control for security). Otherwise it will ask for the password again. We can verify if only these two applications are allowed to access the keychain by navigating to keychain access app and select Exchange -> GetInfo -> Access Control -> where you should see only two entries (Outlook and Security) as follows

on run {input, parameters}
	set vpn to do shell script "/opt/cisco/anyconnect/bin/vpn status | grep -q 'Connected' && echo 'connected' || echo 'disconnected'"
	if (vpn is "connected") then
		do shell script "/opt/cisco/anyconnect/bin/vpn disconnect"
		say "VPN is disconnected!"
		display dialog "VPN is disconnected!" with icon caution buttons {"OK"} default button "OK"
	else if (vpn is "disconnected") then
		set username to do shell script "whoami"
		set exchangepasskey to do shell script "/usr/bin/security find-generic-password -wl Exchange"
		set vpnnames to {"alias-1", "alias-2", "alias-4", "alias-4"}
		set vpnalias to {choose from list vpnnames}
		set output to do shell script "printf '" & username & "\\n" & exchangepasskey & "\\ny' | /opt/cisco/anyconnect/bin/vpn -s connect " & vpnalias
		if (output contains "state: Connected") then
			say "VPN is connected successfully!"
			display dialog "VPN is connected successfully!" with icon 1 buttons {"OK"} default button "OK"
		else
			do shell script "ps -ef | grep 'cisco' | awk '{print $2};' | xargs kill -9 > /dev/null 2>&1"
			say "Error connecting VPN, try again! I have killed the running instances"
			display dialog "Error connecting VPN, try again!" with icon stop buttons {"OK"} default button "OK"
		end if
	end if
	return input
end run

We also killed the running instance of VPN, in case of any error happened so that when we run the app next time, it will not throw any error.

If you need it as an executable app, just contact me.

Just extract the zip and copy the VPN.app to Applications folder. Now you got the app installed.

3 comments on “AppleScript to automatically connect to Cisco Anyconnect VPN!

  1. Pretty nice post. I just stumbled upon your weblog and wanted to say that
    I have really enjoyed surfing around your blog posts.
    In any case I will be subscribing to your feed and I hope you write again soon!

  2. Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is valuable and all. But just imagine if you added some great visuals or
    videos to give your posts more, “pop”! Your content
    is excellent but with pics and videos, this
    site could certainly be one of the most beneficial in its niche.
    Excellent blog!

Leave a Reply to cbd Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.