#!/bin/bash SCRIPT=$(basename $0) function usage () { echo "Usage: $SCRIPT [--x] [--verbose] [[--su |{user}@]{host}]" } function termwrapper () { PRG=$@ title="$title $titleopt" [ "$V" = 1 ] && echo $PRG # mc seem to have a bug separating upper- and lowercase with danish locale # case $LANG in # da_*) LANG=da;; # esac # case `readlink /etc/alternatives/x-terminal-emulator | xargs basename` in # rxvt*) # opts="+sb -sl 500 -n \"$title\" -title \"$title\"" # ;; # esac if [ -n "$PRG" ]; then # rxvt +sb -sl 500 -n "$title" -title "$title" -e $PRG & # exec rxvt +sb -sl 500 -n "$title" -title "$title" -e $PRG x-terminal-emulator +sb -sl 500 -n "$title" -title "$title" -e $PRG & # x-terminal-emulator $opts -e $PRG & else # rxvt +sb -sl 500 -n "$title" -title "$title" & # exec rxvt +sb -sl 500 -n "$title" -title "$title" x-terminal-emulator +sb -sl 500 -n "$title" -title "$title" & # x-terminal-emulator $opts & fi } # Initial setup SU='' X='' V='' SUHACK='' HOST='' USER='' ssh_opts="-t" args='' while [ $# -gt 0 ]; do case $1 in -su|su) SU=1;; -suu|suu) SU=1; SUU=1;; # Become root indirectly through a regular user (same as local user if -u not used) --user|-u) USER=$2; shift;; # Remote user allowed to switch to root --x|-x|x) X=1; ssh_opts="$ssh_opts -X";; --ssh2|-2) ssh_opts="$ssh_opts -2";; --verbose|-v) V=1; ssh_opts="$ssh_opts -v";; --port|-p) ssh_opts="$ssh_opts -p $2"; shift;; --host|-h) HOST=$1;; -) HOST="localhost";; -*) usage; exit 1;; root@*) HOST="$1"; SU=1; SUHACK=1;; *) args="$args$1 ";; esac shift done set -- $args if [ -z "$HOST" ]; then HOST=$1 shift fi if [ -z "$HOST" ]; then usage exit 1 fi prg_base=$1 shift prg_opts=$@ if [ "$X" = 1 -a "$SU" = 1 -a "$SUHACK" != 1 ]; then args="root@$args" SUHACK=1 fi titleopt="[$HOST]" if [ $HOST != "localhost" ]; then ssh_opts="-C $ssh_opts" fi # Pause for a moment if verbose if [ "$V" = 1 ]; then prg_opts="$prg_opts; sleep 4" fi ## Don't open 2 shells if no program is run - Disabled for now (something wrong with the test...) #if [ -z "$prg_base" -a -z "$prg_opts" -a $SUU != 1 ]; then # title="$SCRIPT" # prg_local='' # prg_remote='' #else if [ -z "$prg_base" ]; then title="$SCRIPT" prg_base='/bin/bash' else title="$prg_base" prg_opts_local="$prg_opts" prg_opts_remote="$prg_opts" fi # Special cases for specific programs case $prg_base in mc) prg_opts_remote="-s -c $prg_opts_remote" # Midnight Commander has an option for slow connections ;; esac prg_local="$prg_base $prg_opts_local" if [ "$SUU" = 1 ]; then prg_remote="echo -n 'Changing to root...: '; su $su_opts -c \"cd; $prg_base $prg_opts_remote\"" else # prg_remote="bash -i -c \"$prg_base $prg_opts_remote\"" # prg_remote="bash -i -c \"test -e /etc/debian_version || export TERM=xterm; $prg_base $prg_opts_remote\"" prg_remote="bash --login -i -c \"test -e /etc/debian_version || export TERM=xterm; $prg_base $prg_opts_remote\"" # prg_remote="bash -i -c \"test -e /etc/debian_version || export TERM=xterm; $prg_base $prg_opts_remote\"" fi #fi if [ "$SU" != 1 ]; then if [ $HOST = "localhost" ]; then termwrapper $prg_local else termwrapper ssh $ssh_opts $HOST $prg_remote fi else if [ "$SUHACK" = 1 ]; then # termwrapper ssh $ssh_opts $HOST su $su_opts -c \"$prg_remote\" termwrapper ssh $ssh_opts $HOST $prg_remote else titleopt="[$HOST root]" if [ "$SUU" = 1 ]; then if [ -z "$USER" ]; then termwrapper ssh $ssh_opts $HOST $prg_remote else termwrapper ssh $ssh_opts $USER@$HOST $prg_remote fi else termwrapper ssh $ssh_opts root@$HOST $prg_remote fi fi fi