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