shosei

resume-screen

May 11th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.04 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # Resume/create screen sessions intelligently.  Never creates a second session.
  4. ## Usage: resume-screen [OPTIONS] [HOST...]
  5. ##  -h      This help information
  6. ##  -i      Installation help
  7. ##  --debug Debug this script
  8. ##  HOST    A host to ALWAYS use screen for, including from multiple sources.
  9. ##          HOST arguments are regular expressions.  You can also space-delimit.
  10. ##          The $screen_host variable can be used in addition to HOST arguments.
  11. ## $RESUME_HOSTS  acts just like HOST but hides the list from process lists (ps)
  12. ## resume-screen v0.6 Copyright < cyn.cc >, GPL 2+
  13. #
  14. #q## INSTALLATION HELP:
  15. ###   Add this to your ~/.screenrc file:
  16. ###     setenv inscreen 0
  17. ###   Call this from the very top of your rc file (~/.bashrc, etc).
  18. ###   The rest of this is only needed if you have multiple "screen" binaries.
  19. ###
  20. ###   Add this to the very bottom of your rc file (after the $PATH is resolved):
  21. ###     which -a screen |grep ^/ |head -n1 >~/.screen.`uname -n`
  22. ###   (Note that this might fail if the binary needs a different library path)
  23. ###   If you don't have GNU or Debian which, you'll need these lines instead:
  24. ###     OLDIFS="$IFS"  IFS=":"
  25. ###     for dir in $PATH; do
  26. ###       if [ -f "$dir/screen" ] && [ -x "$dir/screen" ]; then
  27. ###         echo "$dir/screen" > ~/.screen.`uname -n`
  28. ###         break
  29. ###       fi
  30. ###     done
  31. ###     IFS="$OLDIFS"
  32. ###
  33. ### For general help, try 'resume-screen -h'
  34. ##########################
  35. # Half of this code is for debug, but it remains because I suspect it would be
  36. # useful, e.g. the next time 'who' changes, etc.  Another note on this script:
  37. # Solaris uses jsh which cannot handle ${foo#bar} style substitution.
  38. ##########################
  39.  
  40. gethelp() { sed -e "/$1/!d" -e s/// `which $0 2>/dev/null || echo $0`; exit $2;}
  41.  
  42. while [ -n "$1" ]; do
  43.   case $1 in
  44.     --debug  ) DEBUGME=true; shift ;;
  45.     -i*|--i* ) gethelp '^### ' ;;
  46.     -h|--he* ) gethelp '^## ' ;;
  47.     -* ) echo "`basename $0`: invalid argument \`$1'" >&2; gethelp '^## ' 1 ;;
  48.    *  ) break ;;
  49.  esac
  50. done
  51.  
  52. if [ -z "$DEBUGME" ]
  53.  then debug() { true; }
  54.  else debug() { echo "`date +%M%S`: $*"; }
  55.       debug "  Debug enabled for `basename $0`."
  56. fi
  57.  
  58. # Saved location lets this run before $PATH is set. See INSTALLATION HELP above.
  59. HOST="`uname -n`"
  60. myscreen="`cat ~/.screen.$HOST 2>/dev/null || which screen 2>&1 |grep /screen$`"
  61. debug "  screen='$myscreen'"
  62.  
  63. # NOTE: 'inscreen' is a custom variable.  See INSTALLATION HELP above.
  64. if [ ! -x "$myscreen" ] || [ -z "$SSH_TTY" ] || [ -n "$inscreen" ]; then
  65.  debug "'$myscreen' isn't executable, not an SSH TTY, or already in screen"
  66.  exit 1
  67. fi
  68.  
  69. debug "STEP ONE:  try to re-attach a detached screen session"
  70. $myscreen -r >/dev/null 2>&1 && exit 0
  71. # EXIT after we successfully re-attached to a screen session
  72.  
  73.  
  74. debug "  No un-attached screen found."
  75.  
  76. # smoosh all hosts into one regular expression
  77. for arg in $RESUME_HOSTS "$@"
  78.  do screen_host="$screen_host${screen_host:+|}$arg"
  79. done
  80. debug "  HOST regexp='$screen_host'"
  81.  
  82. if [ -n "$RESUME_HOSTS$1" ] && echo "$HOST" |egrep "$screen_host" >/dev/null
  83. then # if host{
  84.  debug "STEP TWO:  Attach to an in-use screen or create new one"
  85.  if $myscreen -ls 2>/dev/null |grep '(Attached)' >/dev/null # if attached {
  86.  
  87.    then
  88.      debug "Attached screen found.  Trying to attach..."
  89.      mytty=`echo $SSH_TTY |sed -e 's:^/dev/::' -e 's:/:.:g'`
  90.      me=`id -un`
  91.      screen_client=`who |sed -e "/^$me.*$mytty[^0-9]/!d" -e 's/.*(//'`
  92.      debug "  tty='$mytty' client='$screen_client' user='$me'"
  93.      if [ `who |grep -c "$me.*$screen_client"` = 1 ]; then
  94.        debug "Success, you are not logged on from this SSH client IP."
  95.        echo "Connecting to already-attached session, press CTRL+C to cancel..."
  96.        sleep 3 && $myscreen -x
  97.      else
  98.        debug "Found more than one SSH connection from your IP.  No action."
  99.      fi
  100.  
  101.    else
  102.      debug "No screens (attached or not) found.  Creating new session..."
  103.      $myscreen
  104.  
  105.  fi # } end if attached
  106. fi # } end if host
Advertisement
Add Comment
Please, Sign In to add comment