Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- usage()
- {
- cat << EOF
- usage: $0 options
- This sets up a persistent reverse tunnel (shell) to another machine
- OPTIONS:
- -h Show this message
- -r 'remote' machine (default localhost)
- -p Port on remote (this) machine to be forwarded (default 22/ssh)
- -l 'local' machine (the one you want to connect to here, from)
- -q Port on local (foreign) machine to be tunneled to the remote port (default 2222)
- -u Use an alternative user on 'local'
- -v Verbose
- EOF
- }
- while getopts “hr:p:l:q:u:v” OPTION
- do
- case $OPTION in
- h)
- usage
- exit 1
- ;;
- r)
- REMOTE=$OPTARG
- ;;
- p)
- REMOTE_PORT=$OPTARG
- ;;
- l)
- LOCAL=$OPTARG
- ;;
- q)
- LOCAL_PORT=$OPTARG
- ;;
- u)
- USER= $OPTARG
- ;;
- v)
- VERBOSE=" -v "
- ;;
- ?)
- usage
- exit
- ;;
- esac
- done
- #Set Defaults
- [[ -n $REMOTE ]] && REMOTE= $REMOTE || REMOTE="localhost"
- [[ -n $REMOTE_PORT ]] && REMOTE_PORT=$REMOTE_PORT || REMOTE_PORT="22"
- [[ -n $LOCAL_PORT ]] && LOCAL_PORT=$LOCAL_PORT || LOCAL_PORT="2222"
- [[ -n $USER ]] && USER=$USER || USER=`whoami`
- trap "logger Tunnel to $LOCAL died permenantly; exit" SIGINT SIGTERM EXIT
- while true;do
- ssh $VERBOSE -NR $LOCAL_PORT:localhost:$REMOTE_PORT $LOCAL
- logger "Tunnel to $LOCAL died (hopefully) temporarily, sleeping for 10 seconds, then trying again"
- sleep 10
- done;
- logger "Someone is trying to kill tunnel to $LOCAL!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement