Created
June 11, 2018 03:00
-
-
Save aursu/22e203b755afa3a447cc96dcbfa7e5fd to your computer and use it in GitHub Desktop.
Connect to Cisco AnyConnect VPN
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 | |
| loginscript=vpn-login | |
| [ -f $loginscript ] && . $loginscript | |
| VPNCOOKIE=$(curl -s -k "https://${VPNGATEWAY}/+webvpn+/index.html" \ | |
| -H 'Cookie: webvpnlogin=1' \ | |
| --data 'group_list=DefaultWEBVPNGroup' \ | |
| --data "username=${VPNLOGIN}" \ | |
| --data "password=${VPNSECRET}" \ | |
| -o/dev/null -D- | \ | |
| awk -F: '$1 ~ /Set-Cookie/ && $2 ~ /webvpn=/ {print $2}' | \ | |
| awk -F\; '{print $1}' | \ | |
| sed 's/^[[:space:]]*//g; s/webvpn=//') | |
| CERTHASH=$(echo | openssl s_client -connect $VPNGATEWAY:443 2>/dev/null | \ | |
| openssl x509 -pubkey -noout | openssl pkey -pubin -outform der | \ | |
| openssl sha256 | awk '{print $2}') | |
| mkdir -p /etc/vpnc/connect.d | |
| # do not set /etc/resolv.conf | |
| echo "export INTERNAL_IP4_DNS=" > /etc/vpnc/connect.d/01-resolv.conf | |
| # do not replace default gateway | |
| cat <<'EOF' > /etc/vpnc/connect.d/02-defroute | |
| set_default_route() { | |
| rm -f $DEFAULT_ROUTE_FILE | |
| } | |
| EOF | |
| if [ -z "$VPNCOOKIE" -o -z "$CERTHASH" ]; then | |
| exit 1 | |
| else | |
| openconnect --background \ | |
| --pid-file="/run/webvpn-${VPNGATEWAY}.pid" --servercert "sha256:${CERTHASH}" \ | |
| --no-dtls \ | |
| -C "$VPNCOOKIE" $VPNGATEWAY | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment