screen_ssh

I use GNU Screen a lot - as in basically all the time, and when I SSH somewhere I like to set the title of the screen I'm in, so I can easily keep track of loads of them.

To do this I've pulled together ideas and suggestions from a bunch of people to come up with a script which can be executed by SSH when it connects to a remote server.
You'll need to edit

~/.ssh/config

and set something like this:
Host *
LocalCommand /path/to/screen_ssh.sh $PPID %n
PermitLocalCommand yes


This will set the screen title to the name of the machine you SSH'd to and do its best to chop off extraneous Top-Level-Domain information (i.e. if you ssh to foo.company.com the title will be "foo").

You can download the script here: screen_ssh.sh

This is an example, if such a thing were needed:



If you want the blue line to appear at the bottom of your Screen sessions, add this to ~/.screenrc (all on one line):
hardstatus alwayslastline
"%{= bw} %{= bc}[%{-}%0c%{= bc}]%{-} [%-Lw%{= bW}%{+b}%50>%n%f* %t%{-b}%{= bw}%+Lw]%<"

Discussion

  1. Wonderful!
    But it does not restore the name when you log out.

    I've made another approach (it's just a sketch), it's a script that wraps ssh (you can use it as another command or just do an alias ssh=pathtothescript).
    It's almost the same but sets the terminal to the local hostname when ssh ends, you can even use it through sshs inside sshs (using the scriot on the remote machine too).


    SSH_OPTIONS="1246AaCfgKkMNnqsTtVvXxYyb:c:D:e:F:I:i:L:l:m:O:o:p:R:S:W:w:"

    # We only care if we are in a terminal
    tty -s || { ssh $@; exit; }

    # We also only care if we are in screen, which we infer by $TERM starting
    # with "screen"
    [ "${TERM:0:6}" != "screen" ] && { ssh $@; exit; }

    # Get rid of all the ssh options and go to the hostname
    while getopts "$SSH_OPTIONS" OPTION; do :; done
    args="$@"
    shift $((OPTIND-1)); OPTIND=1
    HOST=$1
    # get rid of the domain
    echo $HOST | sed -e 's/\.[^.]*$//' | awk '{ printf ("\033k%s\033\\", $NF) }'
    ssh $args
    # restore the name to the hostname of the local machine
    hostname | awk '{ printf ("\033k%s\033\\", $NF) }'

    By the way, I use screen a lot too, and I found that byobu helps a lot (also supports vertical splits).

    ReplyDelete