Skip to content

Instantly share code, notes, and snippets.

@mdonkers
Created January 6, 2017 07:34
Show Gist options
  • Save mdonkers/681c87e079893fa29646d4881d431a0d to your computer and use it in GitHub Desktop.
Save mdonkers/681c87e079893fa29646d4881d431a0d to your computer and use it in GitHub Desktop.
Run as root user. Needs openconnect installed. Connect to Juniper / Pulse VPN automatically, parsing password from a GPG encrypted file so it doen't have to be entered manually. Note this will have security implications!
#!/bin/bash
# encrypt (leave one empty line after password)
#gpg -e -a -r <user-id> your_password_file
set -e
# Must be a valid filename
NAME=my-vpn
PIDFILE=/var/run/$NAME.pid
#This is the command to be run, give the full pathname
DAEMON=/usr/sbin/openconnect
DAEMON_OPTS="--passwd-on-stdin -u <account> --background --pid-file ${PIDFILE} --juniper <VPN-url>"
PASSWD_FILE=<location_your_password_file>
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
case "$1" in
start)
echo -n "Starting daemon: "$NAME
sudo -u <username> gpg -d $PASSWD_FILE | eval "${DAEMON} ${DAEMON_OPTS}"
echo "."
;;
stop)
echo -n "Stopping daemon: "$NAME
pkill -SIGINT --pidfile $PIDFILE
rm $PIDFILE
echo "."
;;
restart)
echo -n "Restarting daemon: "$NAME
pkill -SIGINT --pidfile $PIDFILE
sudo -u <username> gpg -d $PASSWD_FILE | eval "${DAEMON} ${DAEMON_OPTS}"
echo "."
;;
*)
echo "Usage: "$1" {start|stop|restart}"
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment