summaryrefslogtreecommitdiff
path: root/xsh
blob: 65429effdaf39ce927abe01385af01950d126fc5 (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. # get hostname unless already given or not there
  59. if [ -z "$HOST" ]; then
  60. if [ $# -gt 1 ]; then
  61. HOST=$1
  62. shift
  63. else
  64. HOST="localhost"
  65. fi
  66. fi
  67. if [ -z "$HOST" ]; then
  68. usage
  69. exit 1
  70. fi
  71. # Split program name from its options
  72. if [ $# -gt 0 ]; then
  73. prg_base=$1
  74. shift
  75. prg_opts=$@
  76. fi
  77. if [ "$X" = 1 -a "$SU" = 1 -a "$SUHACK" != 1 ]; then
  78. args="root@$args"
  79. SUHACK=1
  80. fi
  81. titleopt="[$HOST]"
  82. if [ $HOST != "localhost" ]; then
  83. ssh_opts="-C $ssh_opts"
  84. fi
  85. # Pause for a moment if verbose
  86. if [ "$V" = 1 ]; then
  87. prg_opts="$prg_opts; sleep 4"
  88. fi
  89. ## Don't open 2 shells if no program is run - Disabled for now (something wrong with the test...)
  90. #if [ -z "$prg_base" -a -z "$prg_opts" -a "$SUU" != 1 ]; then
  91. # title="$SCRIPT"
  92. # prg_local=''
  93. # prg_remote=''
  94. #else
  95. if [ -z "$prg_base" ]; then
  96. title="$SCRIPT"
  97. prg_base='/bin/bash'
  98. else
  99. title="$prg_base"
  100. prg_opts_local="$prg_opts"
  101. prg_opts_remote="$prg_opts"
  102. fi
  103. # Special cases for specific programs
  104. case $prg_base in
  105. mc) prg_opts_remote="-s -c $prg_opts_remote" # Midnight Commander has an option for slow connections
  106. ;;
  107. esac
  108. prg_local="$prg_base $prg_opts_local"
  109. if [ "$SUU" = 1 ]; then
  110. prg_remote="echo -n 'Changing to root...: '; su $su_opts -c \"cd; $prg_base $prg_opts_remote\""
  111. else
  112. # prg_remote="bash -i -c \"$prg_base $prg_opts_remote\""
  113. # prg_remote="bash -i -c \"test -e /etc/debian_version || export TERM=xterm; $prg_base $prg_opts_remote\""
  114. prg_remote="bash --login -i -c \"test -e /etc/debian_version || export TERM=xterm; $prg_base $prg_opts_remote\""
  115. # prg_remote="bash -i -c \"test -e /etc/debian_version || export TERM=xterm; $prg_base $prg_opts_remote\""
  116. fi
  117. #fi
  118. if [ "$SU" != 1 ]; then
  119. if [ $HOST = "localhost" ]; then
  120. termwrapper $prg_local
  121. else
  122. termwrapper ssh $ssh_opts $HOST $prg_remote
  123. fi
  124. else
  125. if [ "$SUHACK" = 1 ]; then
  126. # termwrapper ssh $ssh_opts $HOST su $su_opts -c \"$prg_remote\"
  127. termwrapper ssh $ssh_opts $HOST $prg_remote
  128. else
  129. titleopt="[$HOST root]"
  130. if [ "$SUU" = 1 ]; then
  131. if [ -z "$USER" ]; then
  132. termwrapper ssh $ssh_opts $HOST $prg_remote
  133. else
  134. termwrapper ssh $ssh_opts $USER@$HOST $prg_remote
  135. fi
  136. else
  137. termwrapper ssh $ssh_opts root@$HOST $prg_remote
  138. fi
  139. fi
  140. fi