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