Automating GNU screen startup

Posté le mar. 04 avril 2017 dans blog

At work I use GNU screen with one window per server (ssh connection), and when I loose my screen, it takes minutes to rebuild the naming and the ssh connections ... So I searched and found a PHP version on Jon's blog but I don't like PHP and don't want a PHP cli on my machine (even to start screen !) So I rewrote it in bash (to have brace expansion !) Enjoy:

#!/bin/bash -f

SCREEN_NAME=julien
SERVERS=$(echo {julien,root}@dev root@{www,sql}{1,2} root@media{1,2,3,4,5,6}

screen -dmS "$SCREEN_NAME"
NUM=0
for SERVER in $SERVERS
do
    NUM=$((NUM + 1))
    screen -S "$SCREEN_NAME" -X screen
    if [ z"$SERVER" != z"" ]
    then
        screen -S "$SCREEN_NAME" -p $NUM -X title "$SERVER"
        screen -S "$SCREEN_NAME" -p $NUM -X stuff "ssh $SERVER $(printf \\r)"
    fi
done
printf "Done,  now you can join your screen with :\n"
printf "$ screen -dr -S $SCREEN_NAME\n"

Don't forgot to start your ssh-agent just before starting this script! PS: If you don't have an ssh-agent, you may want to remove the $(printf \\r) and press enter yourself.