Skip to content

Instantly share code, notes, and snippets.

@aursu
Created June 11, 2018 03:00
Show Gist options
  • Save aursu/22e203b755afa3a447cc96dcbfa7e5fd to your computer and use it in GitHub Desktop.
Save aursu/22e203b755afa3a447cc96dcbfa7e5fd to your computer and use it in GitHub Desktop.
Connect to Cisco AnyConnect VPN
#!/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