Skip to content

Instantly share code, notes, and snippets.

@WallaceTan
Last active August 29, 2015 14:27
Show Gist options
  • Save WallaceTan/8f9e4bd85dc56eb227bb to your computer and use it in GitHub Desktop.
Save WallaceTan/8f9e4bd85dc56eb227bb to your computer and use it in GitHub Desktop.
#!/bin/ash
export PATH=/usr/bin:/usr/sbin:/bin:/sbin
export LOCKFILE=/tmp/vpncheck.pid
export LOGFILE=/var/log/vpncheck.log
checktun() {
ifconfig | grep -q tun && return 1
return 0
}
checkopenvpn() {
pgrep openvpn && return 1
return 0
}
if [[ -f /tmp/vpncheck.pid ]]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $LOCKFILE is present. Exiting." | tee -a $LOGFILE
exit
fi
echo $$ > $LOCKFILE
while [[ 1 ]]; do
checktun
if [[ $? == 0 ]]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Tunnel is down." | tee -a $LOGFILE
checkopenvpn
if [[ $? == 0 ]]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] OpenVPN not running, attempting reconnection." | tee -a $LOGFILE
openvpn --cd /etc/openvpn --config /etc/openvpn/aws-ida-gds.ovpn &
else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] OpenVPN running." | tee -a $LOGFILE
fi
sleep 10
else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Tunnel is up." | tee -a $LOGFILE
break
fi
sleep 5
done
rm $LOCKFILE
# truncate log file to last 50 lines
tail -n 50 $LOGFILE > $LOGFILE.tmp && cat $LOGFILE.tmp > $LOGFILE && rm $LOGFILE.tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment