Skip to content

Instantly share code, notes, and snippets.

@yseto
Created November 15, 2018 17:25
Show Gist options
  • Save yseto/2625e044cdd9d2b71df884620a05ad56 to your computer and use it in GitHub Desktop.
Save yseto/2625e044cdd9d2b71df884620a05ad56 to your computer and use it in GitHub Desktop.
#!/bin/bash
USER=
SERVERNAME=
SSHPORT=
HOST=$USER@$SERVERNAME
TUNNELPORT=
IDENTIFY=
# NEED "NO TYPE PASSWORD" SSH CONNECTION
LOGPATH=$(echo -n $0 | sha1sum -t | awk '{print $1}')
LOGDATE=$(date |tr '\n ' ' ')
LOGGING (){
echo "$LOGDATE: $@" | tee -a /var/tmp/sshconn-$LOGPATH
}
case "$1" in
start)
pgrep -f "ssh -f $HOST -N -L $TUNNELPORT -i $IDENTIFY -p $SSHPORT"
if [ $? -ne 0 ] ; then
ssh -f $HOST -N -L $TUNNELPORT -i $IDENTIFY -p $SSHPORT
if [ $? -eq 0 ] ; then
pgrep -f "ssh -f $HOST -N -L $TUNNELPORT -i $IDENTIFY -p $SSHPORT"
LOGGING "Connect Success"
else
LOGGING "Connect Fail"
exit 1
fi
fi
;;
stop)
pkill -f "ssh -f $HOST -N -L $TUNNELPORT -i $IDENTIFY -p $SSHPORT"
LOGGING "Connection Shutdown"
;;
status)
pgrep -f "ssh -f $HOST -N -L $TUNNELPORT -i $IDENTIFY -p $SSHPORT"
if [ $? -ne 0 ] ; then
echo "No Connect."
fi
;;
restart)
echo "Restart Connection"
$0 stop; $0 start
;;
*)
echo "Usage: start stop restart status"
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment