summaryrefslogtreecommitdiff
path: root/xsh
blob: 6f522f462c35408e66f26bafe42ec4a1e1d87c86 (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. USER=''
  29. ssh_opts="-t"
  30. args=''
  31. while [ $# -gt 0 ]; do
  32. case $1 in
  33. -su|su) SU=1;;
  34. # Become root indirectly through a regular user (same as local user if -u not used)
  35. -suu|suu) SU=1; SUU=1;;
  36. # Remote user allowed to switch to root
  37. --user|-u) USER=$2; shift;;
  38. --x|-x|x) X=1; ssh_opts="$ssh_opts -X";;
  39. --ssh2|-2) ssh_opts="$ssh_opts -2";;
  40. --verbose|-v) V=1; ssh_opts="$ssh_opts -v";;
  41. --port|-p) ssh_opts="$ssh_opts -p $2"; shift;;
  42. --host|-h) HOST=$1;;
  43. -) HOST="localhost";;
  44. --) shift; args="$args$@ "; break;;
  45. -*) usage; exit 1;;
  46. root@*) HOST="$1"; SU=1; SUHACK=1;;
  47. *) args="$args$1 ";;
  48. esac
  49. shift
  50. done
  51. set -- $args
  52. # Make sure we have a hostname
  53. if [ -z "$HOST" ]; then
  54. if [ $# -gt 0 ]; then
  55. HOST=$1
  56. shift
  57. else
  58. HOST="localhost"
  59. fi
  60. fi
  61. # Split program name from its options
  62. if [ $# -gt 0 ]; then
  63. prg_base=$1
  64. shift
  65. prg_opts=$@
  66. fi
  67. if [ "$X" = 1 -a "$SU" = 1 -a "$SUHACK" != 1 ]; then
  68. args="root@$args"
  69. SUHACK=1
  70. fi
  71. if [ $HOST != "localhost" ]; then
  72. ssh_opts="-C $ssh_opts"
  73. fi
  74. case `readlink /etc/alternatives/x-terminal-emulator | xargs basename` in
  75. # rxvt*) opts="+sb -sl 500";;
  76. rxvt*) opts="+sb -sl 500 -ls";;
  77. urxvt*) opts="+sb -sl 500 -ls";;
  78. *xterm) opts="+sb -sl 500 +wc";;
  79. gnome-terminal) opts="";;
  80. gnome-terminal.wrapper) opts="";;
  81. konsole*) opts="--notoolbar";;
  82. *) opts="";;
  83. esac
  84. # Pause for a moment if verbose
  85. if [ "$V" = 1 ]; then
  86. prg_opts="$prg_opts; sleep 4"
  87. fi
  88. if [ -z "$prg_base" ]; then
  89. prg_base='/bin/bash'
  90. else
  91. prg_opts_local="$prg_opts"
  92. prg_opts_remote="$prg_opts"
  93. fi
  94. prg_local="$prg_base $prg_opts_local"
  95. prg_remote="bash --login -i -c \"$prg_base $prg_opts_remote\""
  96. prg_remote_suu="echo -n 'Changing to root...: '; su $su_opts -c \"cd; $prg_base $prg_opts_remote\""
  97. if [ "$SU" != 1 ]; then
  98. if [ $HOST = "localhost" ]; then
  99. termwrapper $prg_local
  100. else
  101. termwrapper ssh $ssh_opts $HOST $prg_remote
  102. fi
  103. else
  104. if [ "$SUHACK" = 1 ]; then
  105. # termwrapper ssh $ssh_opts $HOST su $su_opts -c \"$prg_remote\"
  106. termwrapper ssh $ssh_opts $HOST $prg_remote
  107. else
  108. if [ "$SUU" = 1 ]; then
  109. if [ -z "$USER" ]; then
  110. termwrapper ssh $ssh_opts $HOST $prg_remote_suu
  111. else
  112. termwrapper ssh $ssh_opts $USER@$HOST $prg_remote_suu
  113. fi
  114. else
  115. if [ $HOST = "localhost" ]; then
  116. termwrapper ssh $ssh_opts root@$HOST bash --login -i -c \"$prg_local\"
  117. else
  118. termwrapper ssh $ssh_opts root@$HOST $prg_remote
  119. fi
  120. fi
  121. fi
  122. fi