summaryrefslogtreecommitdiff
path: root/xsh
blob: d4e82afb6e6b15239108afa4640e33f3d10e427e (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. if [ "$V" = 1 ]; then
  9. echo $PRG
  10. sleep 4
  11. fi
  12. if [ -n "$PRG" ]; then
  13. # x-terminal-emulator +sb -sl 500 -e $PRG &
  14. x-terminal-emulator $opts -e $PRG &
  15. # exec x-terminal-emulator $opts -e $PRG &
  16. else
  17. # exec x-terminal-emulator +sb -sl 500 &
  18. x-terminal-emulator $opts &
  19. # exec x-terminal-emulator $opts &
  20. fi
  21. }
  22. # Initial setup
  23. SU=''
  24. X=''
  25. V=''
  26. SUHACK=''
  27. HOST=''
  28. ssh_opts="-t"
  29. args=''
  30. while [ $# -gt 0 ]; do
  31. case $1 in
  32. -su|su) SU=1;;
  33. # Become root indirectly through a regular user (same as local user if -u not used)
  34. -suu|suu) SU=1; SUU=1;;
  35. --x|-x|x) X=1; ssh_opts="$ssh_opts -X";;
  36. --ssh2|-2) ssh_opts="$ssh_opts -2";;
  37. --verbose|-v) V=1; ssh_opts="$ssh_opts -v";;
  38. --port|-p) ssh_opts="$ssh_opts -p $2"; shift;;
  39. --host|-h) HOST=$1;;
  40. -) HOST="localhost";;
  41. --) shift; args="$args$@ "; break;;
  42. -*) usage; exit 1;;
  43. root@*) HOST="$1"; SU=1; SUHACK=1;;
  44. *) args="$args$1 ";;
  45. esac
  46. shift
  47. done
  48. set -- $args
  49. # Make sure we have a hostname
  50. if [ -z "$HOST" ]; then
  51. if [ $# -gt 0 ]; then
  52. HOST=$1
  53. shift
  54. else
  55. HOST="localhost"
  56. fi
  57. fi
  58. # Split program name from its options
  59. if [ $# -gt 0 ]; then
  60. prg_base=$1
  61. shift
  62. prg_opts=$@
  63. fi
  64. if [ "$X" = 1 -a "$SU" = 1 -a "$SUHACK" != 1 ]; then
  65. args="root@$args"
  66. SUHACK=1
  67. fi
  68. if [ $HOST != "localhost" ]; then
  69. ssh_opts="-C $ssh_opts"
  70. fi
  71. case `readlink /etc/alternatives/x-terminal-emulator | xargs basename` in
  72. # rxvt*) opts="+sb -sl 500";;
  73. rxvt*) opts="+sb -sl 500 -ls";;
  74. urxvt*) opts="+sb -sl 500 -ls";;
  75. *xterm) opts="+sb -sl 500 +wc";;
  76. gnome-terminal) opts="";;
  77. gnome-terminal.wrapper) opts="";;
  78. konsole*) opts="--notoolbar";;
  79. *) opts="";;
  80. esac
  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. prg_base='/bin/bash'
  87. fi
  88. prg_local="$prg_base $prg_opts"
  89. prg_remote="bash --login -i -c \"$prg_base $prg_opts\""
  90. prg_remote_suu="echo -n 'Changing to root...: '; su $su_opts -c \"cd; $prg_base $prg_opts\""
  91. if [ "$SU" != 1 ]; then
  92. if [ $HOST = "localhost" ]; then
  93. termwrapper $prg_local
  94. else
  95. termwrapper ssh $ssh_opts $HOST $prg_remote
  96. fi
  97. else
  98. if [ "$SUHACK" = 1 ]; then
  99. # termwrapper ssh $ssh_opts $HOST su $su_opts -c \"$prg_remote\"
  100. termwrapper ssh $ssh_opts $HOST $prg_remote
  101. else
  102. if [ "$SUU" = 1 ]; then
  103. termwrapper ssh $ssh_opts $HOST $prg_remote_suu
  104. else
  105. termwrapper ssh $ssh_opts root@$HOST $prg_remote
  106. fi
  107. fi
  108. fi