Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # This script was written to run under the busybox ash shell.
- #
- # Start a ssh reverse tunnel daemon back to the base server.
- SSH_USER=probe1
- SSH_PROXY_PORT=22201
- SSH_HOST=foo.bar
- SSH_KEYFILE=/root/.ssh/id_rsa
- RECOVER_WAIT=10
- SSH_SAI=15 # ServerAliveInterval
- SSH_SACM=4 # ServerAliveCountMax
- PIDFILE="/tmp/$(basename $0.pid)"
- MYNAME=$(basename $0)
- DEPENDENCIES="ssh netstat pgrep"
- #--
- echoerr() {
- # Print errors to stderr.
- echo "$@" 1>&2;
- }
- f_start() {
- # FIXME: Test for openssh clinent. Won't work with dbclient.
- #
- # Verify dependancies.
- for EACH in $DEPENDENCIES ; do
- DEP_TARGET=$(which $EACH 2> /dev/null)
- if [[ -z "$DEP_TARGET" ]] ; then
- DEP_FAIL=1
- echoerr ""
- echoerr "ERROR: $EACH not found."
- fi
- if [[ "$DEP_FAIL" == 1 ]] ; then
- echoerr ""
- echoerr "At least one required dependency is missing. Quitting."
- echoerr ""
- return 1
- fi
- done
- # Verify the keyfile's existence.
- if [[ ! -f "$SSH_KEYFILE" ]] ; then
- echoerr ""
- echoerr "ERROR: SSH_KEYFILE not found: $SSH_KEYFILE. Quitting."
- echoerr ""
- return 1
- fi
- # Bail out if the lockfile/PIDFILE already exists.
- if [[ -r "$PIDFILE" ]] ; then
- echoerr ""
- echoerr "ERROR: Already running at PID $(cat $PIDFILE)? Quitting."
- echoerr ""
- return 1
- fi
- echo ""
- echo -n "Starting daemon: "
- ( while true ; do
- # Wait until the network is up.
- if [[ $(netstat -rn | egrep "^0\.0\.0\.0" | wc -l) -lt 1 ]] ; then
- echoerr "Network not ready."
- sleep 3
- continue
- fi
- # Add timeout for ssh command?
- # How will we know that the tunnel is really up? Can we test that? How do we kill a bad tunnel?
- # dropbear sucks # dbclient -K 15 -I 0 -i $SSH_KEYFILE -N -R $SSH_PROXY_PORT:localhost:22 $SSH_USER@$SSH_HOST
- # FIME: Consider using autossh.
- echoerr "Starting tunnel."
- ssh -i $SSH_KEYFILE -o "ServerAliveInterval=$SSH_SAI" -o "ServerAliveCountMax=$SSH_SACM" -N -R $SSH_PROXY_PORT:localhost:22 $SSH_USER@$SSH_HOST
- X_SSHTUNNEL=$?
- echoerr "SSH tunnel quit with exit:$X_SSHTUNNEL"
- echoerr "Restarting in $RECOVER_WAIT seconds."
- sleep $RECOVER_WAIT
- done ) &> /dev/null &
- MYPID=$!
- echo "Done."
- echo "Running at PID $MYPID."
- echo "$MYPID" > $PIDFILE || (echoerr "ERROR: can't write to PIDFILE. Killing daemon." ; f_stop ; return 1)
- # Disown neither needed or available in busybox ash shell.
- # disown $MYPID
- echo ""
- }
- f_stop() {
- if [[ -r "$PIDFILE" ]] ; then
- echo ""
- echo -n "Stopping daemon: "
- MYPID=$(cat $PIDFILE)
- MYPID_CHILDREN=$(pgrep -P $MYPID)
- kill "$MYPID" || echoerr "Error trying to kill PID: $(cat $PIDFILE)"
- for EACH in $MYPID_CHILDREN ; do
- kill "$EACH"
- done
- rm -f "$PIDFILE"
- echo "Done."
- echo ""
- else
- echo ""
- echo "No PIDFILE found."
- echo ""
- return 2
- fi
- }
- #--
- case "$1" in
- start)
- f_start
- ;;
- stop)
- f_stop
- ;;
- *)
- echo ""
- echo "Usage: $MYNAME start|stop"
- echo ""
- exit 1
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement