Advertisement
bolster

ssh-persist

Dec 8th, 2011
981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.67 KB | None | 0 0
  1. #!/bin/bash
  2. usage()
  3. {
  4. cat << EOF
  5. usage: $0 options
  6.  
  7. This sets up a persistent reverse tunnel (shell) to another machine
  8.  
  9. OPTIONS:
  10.    -h      Show this message
  11.    -r      'remote' machine (default localhost)
  12.    -p      Port on remote (this) machine to be forwarded (default 22/ssh)
  13.    -l      'local' machine (the one you want to connect to here, from)
  14.    -q      Port on local (foreign) machine to be tunneled to the remote port (default 2222)
  15.    -u      Use an alternative user on 'local'
  16.    -v      Verbose
  17. EOF
  18. }
  19.  
  20. while getopts “hr:p:l:q:u:v” OPTION
  21. do
  22.      case $OPTION in
  23.          h)
  24.              usage
  25.              exit 1
  26.              ;;
  27.          r)
  28.              REMOTE=$OPTARG
  29.              ;;
  30.          p)
  31.              REMOTE_PORT=$OPTARG
  32.              ;;
  33.          l)
  34.              LOCAL=$OPTARG
  35.              ;;
  36.          q)
  37.              LOCAL_PORT=$OPTARG
  38.              ;;
  39.          u)
  40.              USER= $OPTARG
  41.              ;;
  42.          v)
  43.              VERBOSE=" -v "
  44.              ;;
  45.          ?)
  46.              usage
  47.              exit
  48.              ;;
  49.      esac
  50. done
  51.  
  52. #Set Defaults
  53. [[ -n $REMOTE ]] && REMOTE= $REMOTE || REMOTE="localhost"
  54. [[ -n $REMOTE_PORT ]] && REMOTE_PORT=$REMOTE_PORT || REMOTE_PORT="22"
  55. [[ -n $LOCAL_PORT ]] && LOCAL_PORT=$LOCAL_PORT || LOCAL_PORT="2222"
  56. [[ -n $USER ]] && USER=$USER || USER=`whoami`
  57.  
  58. trap "logger Tunnel to $LOCAL died permenantly; exit" SIGINT SIGTERM EXIT
  59.  
  60. while true;do
  61.     ssh $VERBOSE -NR $LOCAL_PORT:localhost:$REMOTE_PORT $LOCAL
  62.     logger "Tunnel to $LOCAL died (hopefully) temporarily, sleeping for 10 seconds, then trying again"
  63.     sleep 10
  64. done;
  65.  
  66. logger "Someone is trying to kill tunnel to $LOCAL!"
  67.  
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement