summaryrefslogtreecommitdiff
path: root/xsh
blob: 32a906f6272cb9f7673ab9a02efb9d23489ab012 (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";;
  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. prg_remote="bash --login -i -c \"$prg_base $prg_opts_remote\""
  105. prg_remote_suu="echo -n 'Changing to root...: '; su $su_opts -c \"cd; $prg_base $prg_opts_remote\""
  106. if [ "$SU" != 1 ]; then
  107. if [ $HOST = "localhost" ]; then
  108. termwrapper $prg_local
  109. else
  110. titleopt="[$HOST]"
  111. termwrapper ssh $ssh_opts $HOST $prg_remote
  112. fi
  113. else
  114. if [ "$SUHACK" = 1 ]; then
  115. titleopt="[$HOST]"
  116. # termwrapper ssh $ssh_opts $HOST su $su_opts -c \"$prg_remote\"
  117. termwrapper ssh $ssh_opts $HOST $prg_remote
  118. else
  119. titleopt="[$HOST root]"
  120. if [ "$SUU" = 1 ]; then
  121. if [ -z "$USER" ]; then
  122. termwrapper ssh $ssh_opts $HOST $prg_remote_suu
  123. else
  124. termwrapper ssh $ssh_opts $USER@$HOST $prg_remote_suu
  125. fi
  126. else
  127. if [ $HOST = "localhost" ]; then
  128. termwrapper ssh $ssh_opts root@$HOST bash --login -i -c \"$prg_local\"
  129. else
  130. termwrapper ssh $ssh_opts root@$HOST $prg_remote
  131. fi
  132. fi
  133. fi
  134. fi