summaryrefslogtreecommitdiff
path: root/xsh
blob: 9c7d701f9f6cbacf6bccae899ef5665c9aad023e (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 ] && 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" -a "$SU" -a ! "$SUHACK" ]; 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. [ $V ] && prg_opts="$prg_opts; sleep 4"
  78. ## Don't open 2 shells if no program is run - Disabled for now (something wrong with the test...)
  79. #if [ -z $prg_base -a -z $prg_opts -a -z $SUU ]; then
  80. # title="$SCRIPT"
  81. # prg_local=''
  82. # prg_remote=''
  83. #else
  84. if [ -z $prg_base ]; then
  85. title="$SCRIPT"
  86. prg_base='/bin/bash'
  87. else
  88. title="$prg_base"
  89. prg_opts_local="$prg_opts"
  90. prg_opts_remote="$prg_opts"
  91. fi
  92. # Special cases for specific programs
  93. case $prg_base in
  94. mc) prg_opts_remote="-s -c $prg_opts_remote" # Midnight Commander has an option for slow connections
  95. ;;
  96. esac
  97. prg_local="$prg_base $prg_opts_local"
  98. if [ $SUU ]; then
  99. prg_remote="echo -n 'Changing to root...: '; su $su_opts -c \"cd; $prg_base $prg_opts_remote\""
  100. else
  101. # prg_remote="bash -i -c \"$prg_base $prg_opts_remote\""
  102. # prg_remote="bash -i -c \"test -e /etc/debian_version || export TERM=xterm; $prg_base $prg_opts_remote\""
  103. prg_remote="bash --login -i -c \"test -e /etc/debian_version || export TERM=xterm; $prg_base $prg_opts_remote\""
  104. # prg_remote="bash -i -c \"test -e /etc/debian_version || export TERM=xterm; $prg_base $prg_opts_remote\""
  105. fi
  106. #fi
  107. if [ ! $SU ]; then
  108. if [ $HOST = "localhost" ]; then
  109. termwrapper $prg_local
  110. else
  111. termwrapper ssh $ssh_opts $HOST $prg_remote
  112. fi
  113. else
  114. if [ "$SUHACK" ]; then
  115. # termwrapper ssh $ssh_opts $HOST su $su_opts -c \"$prg_remote\"
  116. termwrapper ssh $ssh_opts $HOST $prg_remote
  117. else
  118. titleopt="[$HOST root]"
  119. if [ $SUU ]; then
  120. if [ "x$USER" = "x" ]; then
  121. termwrapper ssh $ssh_opts $HOST $prg_remote
  122. else
  123. termwrapper ssh $ssh_opts $USER@$HOST $prg_remote
  124. fi
  125. else
  126. termwrapper ssh $ssh_opts root@$HOST $prg_remote
  127. fi
  128. fi
  129. fi