summaryrefslogtreecommitdiff
path: root/xsh
blob: 13916400a84b3e739f3ec8a933990a37fa522299 (plain)
  1. #!/bin/bash
  2. SCRIPT=$(basename $0)
  3. usage() {
  4. echo "Usage: $SCRIPT [--x] [--verbose] [su|sudo] [[{user}]@{host}] [[--] command...]"
  5. }
  6. termwrapper() {
  7. PRG=$@
  8. if [ "$V" = 1 ]; then
  9. echo $PRG
  10. sleep 4
  11. fi
  12. if [ -n "$PRG" ]; then
  13. x-terminal-emulator -e $PRG &
  14. else
  15. x-terminal-emulator &
  16. fi
  17. }
  18. # Initial setup
  19. SU=''
  20. SUDO=''
  21. X=''
  22. V=''
  23. HOST=''
  24. ssh_opts="-t"
  25. while [ $# -gt 0 ]; do
  26. case $1 in
  27. su) SU=1; SUDO=;;
  28. sudo) SUDO=1; SU=;;
  29. --x|-x|x) X=1; ssh_opts="$ssh_opts -X";;
  30. --verbose|-v) V=1; ssh_opts="$ssh_opts -v";;
  31. --port|-p) ssh_opts="$ssh_opts -p $2"; shift;;
  32. --) shift; break;;
  33. -*) usage; exit 1;;
  34. @*) HOST=${1#@};;
  35. *@*) HOST=$1;;
  36. *) break;;
  37. esac
  38. shift
  39. done
  40. # Split program name from its options
  41. if [ $# -gt 0 ]; then
  42. prg_base=$1
  43. shift
  44. prg_opts=$@
  45. fi
  46. # Pause for a moment if verbose
  47. if [ "$V" = 1 ]; then
  48. prg_opts="$prg_opts; sleep 4"
  49. fi
  50. if [ -z "$prg_base" ]; then
  51. prg_base='/bin/bash'
  52. fi
  53. prg="$prg_base $prg_opts"
  54. prg_su="echo -n 'Changing to root...: '; su -c \"cd; $prg\""
  55. prg_sudo="sudo -i -- $prg"
  56. case "$HOST" in
  57. '')
  58. if [ -n "$SU" ]; then
  59. termwrapper $prg_su
  60. elif [ -n "$SUDO" ]; then
  61. termwrapper $prg_sudo
  62. else
  63. termwrapper $prg
  64. fi
  65. ;;
  66. *)
  67. if [ -n "$SU" ]; then
  68. termwrapper ssh $ssh_opts $HOST $prg_su
  69. elif [ -n "$SUDO" ]; then
  70. termwrapper ssh $ssh_opts $HOST $prg_sudo
  71. else
  72. termwrapper ssh $ssh_opts $HOST $prg
  73. fi
  74. ;;
  75. esac