Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

color ssh script

By: a guest on Jul 28th, 2011  |  syntax: None  |  size: 1.93 KB  |  views: 711  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2. #
  3.  
  4. if [ "$2" ]; then
  5.     exec ssh $*
  6. fi
  7.  
  8. hostspec=$1
  9. host=$(echo $hostspec | sed -e 's/^.*@//' -e 's/\..*$//')
  10.  
  11. # The default opacity value if not specified (0-65535)
  12. #default_opacity=65535
  13.  
  14. # The default color which will be used when not connected to any host.
  15. default_color="2048, 2048, 2048"  # , $default_opacity"
  16.  
  17. case "$host" in
  18. ### Add matching host cases here:
  19.     dfwapp02) host_color="3328, 10080, 10080";;
  20.     optimus)  host_color="768, 768, 11808";;
  21.     ratchet)  host_color="815, 8175, 11717";;
  22.     iadapp01) host_color="4000, 9000, 4000";;
  23. ########################################################################
  24.  
  25.     *)  # Compute default color based on first three letters of hostname
  26.     declare -a cols
  27. #    cols=($(echo -ne ${host/[a-z]*@//} | tr 'A-Z' 'a-z' | tr -cd 'a-z' | \
  28. #            od -t d1 | head -n1 | cut -c10-22));
  29.     host_color=$(perl -e '
  30.           $DARKNESS = 4;
  31.           $host = $ARGV[0];
  32.           while ($host =~ /(.)/g) { $seed += ord($1) };
  33.           srand($seed);
  34.           for (1..3) {
  35.               $c = int(rand() * 65535);
  36.               if ($c > (65535/$DARKNESS)) {
  37.                   $c = $c / $DARKNESS
  38.               }
  39.               push @colors, $c;
  40.           };
  41.           print join (", ", @colors);
  42.       ' $host)
  43.     ;;
  44. esac
  45.  
  46. window_name="${host}_SSH_$$"
  47.  
  48. trap cleanup 1 2 3 6
  49.  
  50. function cleanup() {
  51.     set_color "$default_color"
  52.     echo -ne "\033]0;\007"
  53. }
  54.  
  55. function set_color() {
  56.     case "$TERM_PROGRAM" in
  57.     Apple_Terminal)
  58.         echo -ne "\033]0;${window_name}\007"
  59.         osascript -e 'tell application "Terminal" to tell (first window whose name contains "'${window_name}'") to set background color to {'"$1"'}'
  60.         ;;
  61.     iTerm.app)
  62.         osascript -e 'tell application "iTerm" to set background color of (current session of current terminal) to {'"$1"'}'
  63.         ;;
  64.     esac
  65. }
  66.  
  67. set_color "$host_color"
  68. LOCAL_TERMCOLOR="$host_color" ssh -o SendEnv=LOCAL_TERMCOLOR $hostspec
  69. cleanup
clone this paste RAW Paste Data