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