Skip to content

Instantly share code, notes, and snippets.

@rtgibbons
Last active February 3, 2022 23:03

Revisions

  1. rtgibbons revised this gist Mar 9, 2016. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions oc
    Original file line number Diff line number Diff line change
    @@ -33,11 +33,15 @@ function start() {
    # For now if not on OSX ask for password on command prompt
    if [[ $(uname) == "Darwin" ]]; then
    VPN_PASS=$(osascript -e 'display dialog "RSA Password" default answer "" with title "OpenConnect VPN" with hidden answer' | awk -F'[:,]' '{print $4}')

    openconnect -b --user=${USER} ${HOST} --pid-file=${PIDFILE} --syslog --passwd-on-stdin &> /dev/null <<< ${VPN_PASS}
    else
    openconnect -b --user=${USER} ${HOST} --pid-file=${PIDFILE} --syslog
    stty -echo
    printf "key and RSA password:"
    read VPN_PASS
    stty echo
    printf "\n"
    fi

    openconnect -b --user=${USER} ${HOST} --pid-file=${PIDFILE} --syslog --passwd-on-stdin <<< ${VPN_PASS}
    [ $? -ne 0 ] && echo "Openconnect failed to start!" && \
    rm -f ${PIDFILE} && exit 1
    }
  2. rtgibbons revised this gist Mar 7, 2016. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions oc
    Original file line number Diff line number Diff line change
    @@ -30,9 +30,14 @@ function start() {
    [ ${UID} -ne 0 ] && echo "You must be root to run this script." && exit 1

    # Connect
    VPN_PASS=$(osascript -e 'display dialog "RSA Password" default answer "" with title "OpenConnect VPN" with hidden answer' | awk -F'[:,]' '{print $4}')
    # For now if not on OSX ask for password on command prompt
    if [[ $(uname) == "Darwin" ]]; then
    VPN_PASS=$(osascript -e 'display dialog "RSA Password" default answer "" with title "OpenConnect VPN" with hidden answer' | awk -F'[:,]' '{print $4}')

    openconnect -b --user=${USER} ${HOST} --pid-file=${PIDFILE} --syslog --passwd-on-stdin &> /dev/null <<< ${VPN_PASS}
    openconnect -b --user=${USER} ${HOST} --pid-file=${PIDFILE} --syslog --passwd-on-stdin &> /dev/null <<< ${VPN_PASS}
    else
    openconnect -b --user=${USER} ${HOST} --pid-file=${PIDFILE} --syslog
    fi
    [ $? -ne 0 ] && echo "Openconnect failed to start!" && \
    rm -f ${PIDFILE} && exit 1
    }
  3. rtgibbons revised this gist Mar 7, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions oc
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,8 @@
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

    # VPN Variables
    HOST="https://<VPN URL>"
    USER="<USERNAME>"
    HOST="https://##VPNURL##"
    USER="##USERNAME##"
    #PASS="PASSWORD"
    #CERT="/my/cert.pem"
    #KEY="/my/key.pem"
  4. rtgibbons revised this gist Mar 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion oc
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

    # VPN Variables
    HOST="<VPN URL>"
    HOST="https://<VPN URL>"
    USER="<USERNAME>"
    #PASS="PASSWORD"
    #CERT="/my/cert.pem"
  5. rtgibbons renamed this gist Mar 7, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. rtgibbons created this gist Mar 7, 2016.
    80 changes: 80 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    #! /bin/bash
    ### BEGIN INIT INFO
    # Provides: openconnect
    # Required-Start: $local_fs $remote_fs $network
    # Required-Stop: $local_fs $remote_fs $network
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Basic script to connect to a SSL VPN using Openconnect
    ### END INIT INFO

    # Define PATH
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

    # VPN Variables
    HOST="<VPN URL>"
    USER="<USERNAME>"
    #PASS="PASSWORD"
    #CERT="/my/cert.pem"
    #KEY="/my/key.pem"

    # Set pidfile
    PIDFILE="/var/run/openconnect.pid"

    function start() {
    # Check if process is running. Exit in this case.
    [ -f ${PIDFILE} ] && ps -p $(< ${PIDFILE}) &> /dev/null && \
    echo "Openconnect is already running." && exit 0

    # Must be root
    [ ${UID} -ne 0 ] && echo "You must be root to run this script." && exit 1

    # Connect
    VPN_PASS=$(osascript -e 'display dialog "RSA Password" default answer "" with title "OpenConnect VPN" with hidden answer' | awk -F'[:,]' '{print $4}')

    openconnect -b --user=${USER} ${HOST} --pid-file=${PIDFILE} --syslog --passwd-on-stdin &> /dev/null <<< ${VPN_PASS}
    [ $? -ne 0 ] && echo "Openconnect failed to start!" && \
    rm -f ${PIDFILE} && exit 1
    }

    function stop() {
    if [ -f ${PIDFILE} ] && ps -p $(< ${PIDFILE}) &> /dev/null; then
    # Pid exists, kill process and remove pidfile
    [ ${UID} -ne 0 ] && echo "You must be root to run this script." && exit 1
    kill $(< ${PIDFILE}) && rm -f ${PIDFILE}
    else
    echo "Openconnect is not running!"
    fi
    }

    function status() {
    if [ -f ${PIDFILE} ] && ps -p $(< ${PIDFILE}) &> /dev/null; then
    echo "Openconnect is running."
    runningtime=$(ps -p $(< ${PIDFILE}) -o etime=)
    echo " IP: $(ifconfig | awk '/-->/{print $2}')"
    echo " $(ifconfig | awk -F': ' '/^utun/{print $1}'): ${runningtime}"

    else
    [ -f ${PIDFILE} ] && rm -f ${PIDFILE}
    echo "Openconnect is stopped."
    exit 3
    fi
    }

    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    status)
    status
    ;;
    restart)
    stop && start
    ;;
    *)
    echo "Usage: ${0##*/} (start|stop|status|restart)" && exit 0
    ;;
    esac