Last active
August 29, 2015 14:01
-
-
Save Raven24/1b3e087a2b70031c65d9 to your computer and use it in GitHub Desktop.
monitoring script checking for ssh tunnel, rebuilding it if necessary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
########################################################################### | |
# CONFIGURATION | |
## | |
TAP_DEV="tap0" # TAP device configured to connect to the remote server | |
ETH_DEV="eth0" # ETH device for DHCP | |
SRV_IP="XXX.XXX.XXX.XXX" # public remote server IP | |
SRV_PT="22" # public remote server SSH port | |
INTRN_IP="10.0.1.1" # internal remote server IP | |
TEST_URL="http://www.google.com" # any remote URL for testing connectivity | |
########################################################################### | |
IFUP=`which ifup` | |
IFDOWN=`which ifdown` | |
SLEEP=`which sleep` | |
NSTAT=`which netstat` | |
GREP=`which grep` | |
DATE=`which date` | |
PING=`which ping` | |
DHCLIENT=`which dhclient` | |
CURL=`which curl` | |
PS=`which ps` | |
TUN_COMMAND="$DHCLIENT $ETH_DEV; $SLEEP 3; $IFDOWN $TAP_DEV ; $SLEEP 3 ; $IFUP $TAP_DEV ; $SLEEP 3" | |
NETSTAT_COMMAND="$NSTAT -nt | $GREP '$SRV_IP:$SRV_PT' | $GREP 'ESTABLISHED'" | |
PS_COMMAND="$PS -aux | $GREP '$SRV_PT'" | |
PING_COMMAND="$PING -c 3 -w 10 $INTRN_IP" | |
CURL_COMMAND="$CURL '$TEST_URL'" | |
MONITOR_COMMAND="$NETSTAT_COMMAND && $PS_COMMAND && $PING_COMMAND && $CURL_COMMAND" | |
DATE_LOG_COMMAND="$DATE --rfc-3339=seconds" | |
while true ; do | |
printf "%s -- " "`$DATE_LOG_COMMAND`" | |
eval "$MONITOR_COMMAND" > /dev/null 2>&1 | |
if [ $? -ne 0 ] ; then | |
printf "re-building tunnel" | |
eval "$TUN_COMMAND" > /dev/null 2>&1 | |
else | |
printf "tunnel is up" | |
fi | |
I=0 | |
STEP=5 | |
while [ $I -le 60 ] ; do | |
$SLEEP $STEP | |
printf "." | |
I=`expr $I + $STEP` | |
done | |
printf "\n" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment