Monday, December 10, 2012

GNU Screen and many ssh windows

I use GNU Screen extensively as a terminal multiplexor.  From my Windows desktop, I use PuTTY to connect to a linux box and run screen in that terminal.  I then use that single terminal to open ssh sessions to various machines.  After a dozen windows, it can be difficult to pick the correct window out of the list even if you are naming your screen windows.  So I use the following script (screenssh) to execute all of my ssh sessions:


#!/bin/bash

if [ `ps waux |grep "ssh $1" | grep -v screenssh | grep -v grep |wc -l` -gt 0 ] ; then

    echo -n "There is an existing session to $1, do you want to attempt to select it? "

    read YN

    case $YN in

        [yY] | [yY][Ee][Ss] )

            echo "Selecting first $1"

            screen -X select $1

            exit

            ;;

        * )

            echo Starting new screen window.

    esac

fi

screen -X screen -t $1 ssh $1

Because I am a fiend with the tab button in terminals running Bash, I also want tab completion for 'screenssh' similar to 'ssh'.  So my .bashrc has the following in it:
if [ -f /etc/bash_completion ]; then
    source /etc/bash_completion
    source /usr/share/bash-completion/completions/ssh
    complete -F _ssh screenssh
fi

No comments:

Post a Comment