The officially official Devuan Forum!

You are not logged in.

#1 2021-04-05 03:55:25

ralph.ronnquist
Administrator
From: Battery Point, Tasmania, AUS
Registered: 2016-11-30
Posts: 1,119  

ssh-tabbed xfce4-terminal

If you use xfc4-terminal for running different ssh in its tabs, then this little scriptlet might suit you.

I have it set up to run in a launcher on the panel, for starting an xfce-terminal that begins by asking for the ssh command line arguments before connecting like so; an empty argument line  starts a local shell tab. Then the same happens on every C-S-T tab in that terminal. Quite handy.

File /home/ralph/bin/remote.xfce4

#!/bin/bash
#
# Start an xfce4-terminal that runs ssh in each new tab, prompting for
# ssh command arguments, or drops to an interactive shell for empty
# arguments. Typically invoked by a launcher or a command line as
#     /whatever/the/path/remote.xfce4
# or with initial ssh arguments
#    /whatever/the/path/remote.xfce4 <arguments...>
#
# The script defines and uses the environment variable TABBED
# which gets cleared for a local shell.

if [ -z "$TABBED" ] ; then
    exec env TABBED="-" xfce4-terminal -e "$0 $1" || exit 1
fi
TABBED="$*"
if [ -z "$TABBED" ] ; then
    HISTFILE="$HOME/.$(basename $0)"
    history -r
    # The following line defaults to reusing the last ssh argument line
    TABBED="$(history -p "!!" )"
    read -e -r -i "$TABBED" -p "ssh: " TABBED
    if [ -z "$TABBED" ] ; then
	printf "\033]0;%s\007" "$(hostname)"
	[ -z "$SHELL" ] && SHELL=/bin/bash
	exec $SHELL -i || exit 1
    fi
    # The following 3 lines remove any prior duplicate ssh argument lines
    for N in $(grep -nFx "$TABBED" $HISTFILE | sed 's/:.*//' | sort -rn) ; do
	history -d $N
    done
    history -s "$TABBED"
    history -w
fi
printf "\033]0;%s\007" "${TABBED%% *}"
exec ssh $TABBED

Note that the history fiddling in the script, which makes the script look complicated, is not really required for the "tabbed ssh" functionality. It could be edited out, but it's there in order to make the ssh arguments input interaction "friendlier" by allowing for up/down arrow stepping through the history of such inputs.

Enjoy

EDIT: Note that the xfce4-terminal preferences also needs to be set up to run the script as custom command for tabs in order for the script (rather than shell) to be run for tabs.

EDIT 2: changed the code for bug fixing and to incorporate @Head_on_a_stick's touch up points (below). This might be a final verrsion smile

Offline

#2 2021-04-05 09:46:22

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 3,125  
Website

Re: ssh-tabbed xfce4-terminal

I ran the script through https://www.shellcheck.net/ and it had some observations:

Line 14:
    exec env TABBED="$*" xfce4-terminal -e $0 || exit 1
                                           ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
    exec env TABBED="$*" xfce4-terminal -e "$0" || exit 1
 
Line 18:
    HISTFILE=$HOME/.$(basename $0)
                               ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
    HISTFILE=$HOME/.$(basename "$0")
 
Line 22:
    read -e -i "$TABBED" -p "ssh: " TABBED
    ^-- SC2162: read without -r will mangle backslashes.
 
Line 24:
        printf "\033]0;$(hostname)\007"
               ^-- SC2059: Don't use variables in the printf format string. Use printf '..%s..' "$foo".
 
Line 29:
    for N in $(grep -n "^$TABBED\$" $HISTFILE | sort -rn) ; do
             ^-- SC2013: To read lines rather than words, pipe/redirect to a 'while read' loop.
                                    ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
    for N in $(grep -n "^$TABBED\$" "$HISTFILE" | sort -rn) ; do
 
Line 30:
        history -d ${N%%:*}
                   ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
        history -d "${N%%:*}"
 
Line 35:
printf "\033]0;${TABBED%% *}\007"
       ^-- SC2059: Don't use variables in the printf format string. Use printf '..%s..' "$foo".
 
Line 36:
exec ssh $TABBED
         ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
exec ssh "$TABBED"

I would also observe that for a bash script [[ should probably be preferred over [: http://mywiki.wooledge.org/BashGuide/Pr … Bash_Tests

If [[ is used then you don't have to quote the variable because that keyword doesn't perform word splitting or glob expansion.


Brianna Ghey — Rest In Power

Offline

#3 2021-04-05 10:55:25

ralph.ronnquist
Administrator
From: Battery Point, Tasmania, AUS
Registered: 2016-11-30
Posts: 1,119  

Re: ssh-tabbed xfce4-terminal

Thanks. That's quite useful. Much of it valid! The script obviously is a "works-for-me" kind of script smile

The line 36 note is misconceived though as that line actually needs the globbing and word splitting; the other ones are good if not terribly important.

And line 29 should rather include a sed filtering to discard all but the lilne numbers; I'm not sure a while loop will work since bash runs those in sub processes(?); does a history command in the sub change the history list of the parent?

But in any case I'll leave those improvements as exercise.

EDIT: also, the grep at line 29 should rather be grep -nFx "$TABBED" "$HISTFILE" to match the input verbatim (i.e., to avoid that it gets interpreted as a pattern).

Offline

Board footer