Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenConnect autoconnect/reconnect script?

I have this script:

#!bin/bash
NAME="user"
PIDFILE="openconnect.pid"
CERT="user.crt"
KEY="user.key"
PASS="pass"
HOST="https://example.com"
SCRIPT="/etc/vpnc/vpnc-script"

openconnect -b --script $SCRIPT --pid-file=$PIDFILE -c $CERT -k $KEY --key-password=$PASS --user=$NAME $HOST

It works, but sometimes if something goes wrong (restart of server, or some other issues), it disconnects from VPN. And I need to rerun script again. Is there some way I could modify it or add it in cron job or some other way?

Note. When I run this script I need to enter certificate password. So considering security, I'm wondering where I should keep that password for autoreconnect purposes?

like image 879
Andrius Avatar asked Jan 14 '15 10:01

Andrius


1 Answers

You can detect if openconnect is still running by checking its PID:

pidof openconnect

This return an exit value of 0 if openconnect still runs otherwise non zero.

You would have a script that looks like that [not tested but should give you a hint]:

#!/bin/bash

OPENCONNECT_PID=""
function checkOpenconnect(){
    ps -p "${OPENCONNECT_PID}"
    # print the status so we can check in the main loop
    echo $?
}

function startOpenConnect(){
    # start here open connect with your params and grab its pid
    openconnect [your params] & OPENCONNECT_PID=$!
}

startOpenConnect

while true
do
    # sleep a bit of time
    sleep 30
    OPENCONNECT_STATUS=$(checkOpenconnect)
    [ $OPENCONNECT_STATUS -ne 0 ] && startOpenConnect
done
like image 101
dawez Avatar answered Oct 31 '22 17:10

dawez