Last active
July 3, 2020 07:33
-
-
Save krtek/10a7bc5d24b70d69d668b86b90a57fb8 to your computer and use it in GitHub Desktop.
MacOS X Cisco AnyConnect Automation
This file contains hidden or 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
| #!/usr/bin/osascript | |
| tell application "Cisco AnyConnect Secure Mobility Client" to activate | |
| delay 1 | |
| tell application "System Events" to tell process "Cisco AnyConnect Secure Mobility Client" | |
| key code 36 | |
| end tell | |
| tell application "System Events" | |
| repeat until (exists window "Cisco AnyConnect | VPN KB NONSTANDARD" of application process "Cisco AnyConnect Secure Mobility Client") | |
| delay 1 | |
| end repeat | |
| end tell | |
| set pwd to do shell script "security find-generic-password -w -s Exchange" | |
| tell application "System Events" to tell process "Cisco AnyConnect Secure Mobility Client" | |
| keystroke pwd | |
| key code 36 | |
| end tell |
This file contains hidden or 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
| #!/usr/bin/osascript | |
| tell application "Cisco AnyConnect Secure Mobility Client" to quit |
This file contains hidden or 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
| #!/bin/bash | |
| echo "Switching to KB VPN location." | |
| sudo networksetup -switchtolocation "KB VPN" | |
| echo "Turning off the Ethernet interface." | |
| sudo networksetup -setv6off "Apple USB Ethernet Adapter" | |
| sudo networksetup -setv4off "Apple USB Ethernet Adapter" | |
| sudo dscacheutil -flushcache | |
| echo "Connecting to KB Wifi." | |
| sudo networksetup -setairportpower "Wi-Fi" on | |
| echo "Restoring KB ~/.npmrc" | |
| cp ~/.npmrc_kb ~/.npmrc | |
| echo "Restoring KB Maven settings" | |
| cp ~/.m2/settings_kb.xml ~/.m2/settings.xml | |
| echo "Waiting for WiFi" | |
| sleep 10 | |
| echo "Starting VPN" | |
| _vpn_start | |
| echo "Done." |
This file contains hidden or 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
| #!/bin/bash | |
| echo "Switching to Automatic location." | |
| sudo networksetup -switchtolocation "Automatic" | |
| echo "Turning off the Ethernet interface." | |
| sudo networksetup -setv6off "Apple USB Ethernet Adapter" | |
| sudo networksetup -setv4off "Apple USB Ethernet Adapter" | |
| sudo dscacheutil -flushcache | |
| echo "Connecting to Wifi" | |
| sudo networksetup -setairportpower "Wi-Fi" on | |
| echo "Deleting KB ~/.npmrc" | |
| mv ~/.npmrc ~/.npmrc_kb | |
| echo "Deleting KB Maven settings" | |
| mv ~/.m2/settings.xml ~/.m2/settings_kb.xml | |
| echo "Stopping VPN" | |
| _vpn_stop | |
| echo "Done." |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Manage Cisco AnyConnect from command line
What is it about?
Starts and stops Cisco AnyConnect VPN service from command line.
Files
vpnbash script that switches toKB VPNlocation and connects to KB using Cisco AnyConnectwifibash script that switches toAutomaticlocations and kills Cisco AnyConnect_vpn_starthelper Apple script (called fromvpn) that starts Cisco AnyConnect and does the clicking_vpn_stophelper Apple script (called fromwifi) that kills Cisco AnyConnectPrepare
HOW-TO
Network profiles
I have two network profiles (a.k.a Locations):
KB VPNandAutomatic.Yours might be called differently.
VPN password
My VPN password is the same as my e-mail password (yours probably too). And it's stored in Keychain Access under

Exchangekey. Yours is probably different..npmrc and .m2 setting
I have specific npm and maven settings for KB. If you don't know what I'm talking about then you can ommit lines with those settings.
Visudo
To avoid asking for password you must put the scripts to
etc/sudoersfile.Use
sudo visudo -f /etc/sudoerscommand to add following lines toetc/sudoers. Replacekrtekwith your login.Usage
Move the files to
~/bindirectory and make them executable usingchmod. Then runvpncommand to start VPN andwifito disconnect from VPN back to normal wifi.