summaryrefslogtreecommitdiff
path: root/xsh
blob: caf5b8b6eda5788334a3fcca4c99652ef8e9e267 (plain)
  1. #!/bin/bash
  2. SCRIPT=$(basename $0)
  3. function usage () {
  4. echo "Usage: $SCRIPT [--x] [--verbose] [[--su |{user}@]{host}]"
  5. }
  6. function termwrapper () {
  7. PRG=$@
  8. titletext="$title $titleopt"
  9. [ "$V" = 1 ] && echo $PRG
  10. case `readlink /etc/alternatives/x-terminal-emulator | xargs basename` in
  11. # rxvt*) opts="+sb -sl 500 -n \"$titletext\""; titleoptname="-title";;
  12. rxvt*) opts="+sb -sl 500"; titleoptname="-title";;
  13. *xterm) opts="+sb -sl 500 +wc"; titleoptname="-title";;
  14. gnome-terminal) opts=""; titleoptname="--title";;
  15. gnome-terminal.wrapper) opts=""; titleoptname="-title";;
  16. konsole*) opts="--notoolbar"; titleoptname="-T";;
  17. *) opts=""; titleoptname="-T";;
  18. esac
  19. if [ -n "$PRG" ]; then
  20. # x-terminal-emulator +sb -sl 500 -n "$title" -title "$title" --title "$title" -e $PRG &
  21. x-terminal-emulator $opts $titleoptname "$titletext" -e $PRG &
  22. # exec x-terminal-emulator $opts $titleoptname "$title" -e $PRG &
  23. else
  24. # exec x-terminal-emulator +sb -sl 500 -n "$title" -title "$title" --title "$title" &
  25. x-terminal-emulator $opts $titleoptname "$titletext" &
  26. # exec x-terminal-emulator $opts $titleoptname "$title" &
  27. fi
  28. }
  29. # Initial setup
  30. SU=''
  31. X=''
  32. V=''
  33. SUHACK=''
  34. HOST=''
  35. USER=''
  36. ssh_opts="-t"
  37. args=''
  38. while [ $# -gt 0 ]; do
  39. case $1 in
  40. -su|su) SU=1;;
  41. # Become root indirectly through a regular user (same as local user if -u not used)
  42. -suu|suu) SU=1; SUU=1;;
  43. # Remote user allowed to switch to root
  44. --user|-u) USER=$2; shift;;
  45. --x|-x|x) X=1; ssh_opts="$ssh_opts -X";;
  46. --ssh2|-2) ssh_opts="$ssh_opts -2";;
  47. --verbose|-v) V=1; ssh_opts="$ssh_opts -v";;
  48. --port|-p) ssh_opts="$ssh_opts -p $2"; shift;;
  49. --host|-h) HOST=$1;;
  50. -) HOST="localhost";;
  51. -*) usage; exit 1;;
  52. root@*) HOST="$1"; SU=1; SUHACK=1;;
  53. *) args="$args$1 ";;
  54. esac
  55. shift
  56. done
  57. set -- $args
  58. # Make sure we have a hostname
  59. if [ -z "$HOST" ]; then
  60. if [ $# -gt 0 ]; then
  61. HOST=$1
  62. shift
  63. else
  64. HOST="localhost"
  65. fi
  66. fi
  67. # Split program name from its options
  68. if [ $# -gt 0 ]; then
  69. prg_base=$1
  70. shift
  71. prg_opts=$@
  72. fi
  73. if [ "$X" = 1 -a "$SU" = 1 -a "$SUHACK" != 1 ]; then
  74. args="root@$args"
  75. SUHACK=1
  76. fi
  77. titleopt="[$HOST]"
  78. if [ $HOST != "localhost" ]; then
  79. ssh_opts="-C $ssh_opts -o ProtocolKeepAlives=300"
  80. fi
  81. # Pause for a moment if verbose
  82. if [ "$V" = 1 ]; then
  83. prg_opts="$prg_opts; sleep 4"
  84. fi
  85. if [ -z "$prg_base" ]; then
  86. title="$SCRIPT"
  87. prg_base='/bin/bash'
  88. else
  89. title="$prg_base"
  90. prg_opts_local="$prg_opts"
  91. prg_opts_remote="$prg_opts"
  92. fi
  93. # Special cases for specific programs
  94. case $prg_base in
  95. # Force Midnight Commander colors and support slow connections
  96. mc) prg_opts_local="-c $prg_opts_local"; prg_opts_remote="-s -c $prg_opts_remote";;
  97. esac
  98. prg_local="$prg_base $prg_opts_local"
  99. prg_remote="bash --login -i -c \"test -e /etc/debian_version || export TERM=xterm; $prg_base $prg_opts_remote\""
  100. prg_remote_suu="echo -n 'Changing to root...: '; su $su_opts -c \"cd; $prg_base $prg_opts_remote\""
  101. if [ "$SU" != 1 ]; then
  102. if [ $HOST = "localhost" ]; then
  103. termwrapper $prg_local
  104. else
  105. titleopt="[$HOST]"
  106. termwrapper ssh $ssh_opts $HOST $prg_remote
  107. fi
  108. else
  109. if [ "$SUHACK" = 1 ]; then
  110. titleopt="[$HOST]"
  111. # termwrapper ssh $ssh_opts $HOST su $su_opts -c \"$prg_remote\"
  112. termwrapper ssh $ssh_opts $HOST $prg_remote
  113. else
  114. titleopt="[$HOST root]"
  115. if [ "$SUU" = 1 ]; then
  116. if [ -z "$USER" ]; then
  117. termwrapper ssh $ssh_opts $HOST $prg_remote_suu
  118. else
  119. termwrapper ssh $ssh_opts $USER@$HOST $prg_remote_suu
  120. fi
  121. else
  122. if [ $HOST = "localhost" ]; then
  123. termwrapper ssh $ssh_opts root@$HOST bash --login -i -c \"$prg_local\"
  124. else
  125. termwrapper ssh $ssh_opts root@$HOST $prg_remote
  126. fi
  127. fi
  128. fi
  129. fi