neurotrigger

Auto-reattaching terminal multiplexor with tabs

by mmnicolas on Oct.21, 2009, under gnu/linux

Typically, from a single ssh connection comes a single shell, let’s improve this situation.

We will use a terminal multiplexor GNU Screen, which provide multiple terminals. We will also implement a neat tab bar to allow naming/showing tabs and automatic session re-attachment capability.

Follow these steps to significantly enhance your distant shell experience, you will need to install screen and ssh, mutt and irssi are optional.

The first step involves using environment variables in order to detect if the login is locally originating or not. If the user is logging from a distant machine, we will reattach an existing detached screen session or create a new one.

When logging in remotely, the most evident distinction i’ve found between a local and remote shell is the environment variable SSH_CONNECTION=, with this var we have a way of detecting remote logins easily and launch something else accordingly.


Insert in your ~/.bashrc:

if env | grep -q SSH_CONNECTION; then
. ~/.screenlogin.sh
fi

The previous lines will launch .screenlogin.sh upon a remote connection, a second script which will contain:

DETACHED=`screen -list | grep Detached | wc -l`
if [ “$DETACHED” = 1 ]; then
screen -DR
fi

Make the script executable:

chmod +x .screenlogin.sh

Now exit and login back again.

Share this post:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • LinkedIn
  • PDF
  • StumbleUpon
  • Twitter
:, , , ,

2 Comments for this entry

  • mm_nicolas

    Instead of:

    if env | grep -q SSH_CONNECTION; then
    . ~/.screenlogin.sh
    fi

    This should also do the trick in a cleaner way:

    if [ -n $SSH_CONNECTION ]; then
    . ~/.screenlogin.sh
    fi

  • mmnicolas

    The self-reattachment is now a feature of earlier version of the screen package found in ubuntu and debian, with any newer version my trick is useless :’(

    But if you use the latest LTS (8.04) or Lenny, this trick is pretty handy.

Leave a Reply

Looking for something?

Use the form below to search the site:

Visit my idols!

Those who gave us tools to play with...

    Archives

    All entries, chronologically...