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