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 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 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 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 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." |
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
vpn
bash script that switches toKB VPN
location and connects to KB using Cisco AnyConnectwifi
bash script that switches toAutomatic
locations and kills Cisco AnyConnect_vpn_start
helper Apple script (called fromvpn
) that starts Cisco AnyConnect and does the clicking_vpn_stop
helper Apple script (called fromwifi
) that kills Cisco AnyConnectPrepare
HOW-TO
Network profiles
I have two network profiles (a.k.a Locations):
KB VPN
andAutomatic
.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
Exchange
key. 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/sudoers
file.Use
sudo visudo -f /etc/sudoers
command to add following lines toetc/sudoers
. Replacekrtek
with your login.Usage
Move the files to
~/bin
directory and make them executable usingchmod
. Then runvpn
command to start VPN andwifi
to disconnect from VPN back to normal wifi.