#!/usr/bin/env bash VPN_Network="VPN-SO36-wipe" PID_FILE="/var/run/${VPN_Network}" # ---------- # Base Function(s) # ---------- clean_up() { # - Terminate connection # - if ps -p "$_pid" > /dev/null 2>&1; then sudo kill $_pid > /dev/null 2>&1 fi # - Remove PID file # - sudo rm -f "$PID_FILE" if $terminal ; then echo -en "\033[1G" if [[ "$1" -gt 0 ]]; then blank_line echo -e " \033[41m Connection to \033[1m${VPN_Network}\033[m\033[41m was terminated! \033[m" else echononl "Terminate VPN connection to \033[32m\033[1m${VPN_Network}\033[m" echo_done fi blank_line fi exit $1 } echononl(){ if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n " $*\\c" 1>&2 else echo -e -n " $*" 1>&2 fi rm /tmp/shprompt$$ fi } echo_done() { if $terminal ; then echo -e "\033[75G[ \033[32mdone\033[m ]" fi } echo_failed(){ if $terminal ; then echo -e "\033[75G[ \033[1;31mfailed\033[m ]" fi } echo_wait(){ if $terminal ; then echo -en "\033[75G[ \033[5m\033[1m..\033[m ]" fi } blank_line() { if $terminal ; then echo "" fi } # ---------- # - Jobhandling # ---------- # - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM # - trap clean_up SIGHUP SIGINT SIGTERM # ---------- # - Some checks .. # ---------- # - Running in a terminal? # - if [[ -t 1 ]] ; then terminal=true else terminal=false fi # ---------- # - Main part of script # ---------- blank_line echononl "Establish VPN connection to \033[32m\033[1m${VPN_Network}\033[m" echo_wait sudo /usr/sbin/openvpn \ --config /etc/openvpn/client-confs/VPN-SO36-wipe/client.conf \ --writepid /var/run/openvpn-wipe-so36.pid \ --log /var/log/openvpn/wipe-so36.log \ --status /var/log/openvpn/status-wipe-so36.log > /dev/null 2>&1 & _pid="$!" declare -i time=0 if ps -p "$_pid" > /dev/null 2>&1; then while ! ip a | grep -q -E "tun[0-9]{1,2}" 2> /dev/null && [[ $time -lt 60 ]] ; do sleep 1 ((time++)) done if [[ $time -ge 60 ]]; then echo_failed clean_up 1 else echo_done fi else echo_failed clean_up 1 fi #while ps -p "$_pid" > /dev/null 2>&1 ; do while ip a | grep -q -E "tun[0-9]{1,2}" 2> /dev/null ; do sleep 3 done clean_up 0