Created
May 28, 2019 21:58
-
-
Save charles-rumley/c8032dc8003590b63917c96121a853ea to your computer and use it in GitHub Desktop.
Use stored 1Password credentials to authenticate a Cisco AnyConnect VPN client
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/env bash | |
# Prerequisites | |
# | |
# Download the 1Password CLI tool | |
# | |
# brew cask install 1password-cli | |
# | |
# Before using this script, you'll need to authenticate the 1Password | |
# CLI for the first time. Use the following command, replacing | |
# the domain and email address with your details in the below command. | |
# | |
# op signin example.1password.com [email protected] | |
# | |
# See: https://support.1password.com/command-line-getting-started/ | |
# | |
# Commands | |
# | |
# vpn.sh connect <optional endpoint> | |
# | |
# vpn.sh state | |
# | |
# vpn.sh disconnect | |
# endpoint for the Cisco AnyConnect VPN if not specified | |
DEFAULT_ENDPOINT="example.endpoint.com" | |
# your 1Password domain | |
OP_DOMAIN="example" | |
# name or UUID of the entry holding your VPN credentials | |
OP_ENTRY_IDENTIFIER="example name" | |
if [[ "$1" = "connect" ]] | |
then | |
# authenticate with 1Password (must be done every 30 minutes) | |
eval $(op signin $OP_DOMAIN) | |
# grab credentials | |
USERNAME=$(op get item $OP_ENTRY_IDENTIFIER | jq --raw-output '.details.fields[] | select(.designation=="username").value') | |
PASSWORD=$(op get item $OP_ENTRY_IDENTIFIER | jq --raw-output '.details.fields[] | select(.designation=="password").value') | |
# escaping the special chars in the password to allow sed-ing it | |
ESC_PWD=$(echo $PASSWORD | sed -e 's/[]\/$*.^[]/\\&/g') | |
VPN_ENDPOINT=${2:-$DEFAULT_ENDPOINT} | |
echo "Connecting to $VPN_ENDPOINT as $USERNAME..." | |
printf "${USERNAME}\n${PASSWORD}\ny" | /opt/cisco/anyconnect/bin/vpn -s connect $VPN_ENDPOINT | sed "s/$ESC_PWD/*****/" | |
else | |
# pass the command through to the Cisco VPN client | |
/opt/cisco/anyconnect/bin/vpn $1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment