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