GNU Screen is a great utility that allows you to have multiple workspaces but perhaps more importantly allows you to start a program executing, detach from the session and then re-attach to that session later to see any updates on the output. Very useful if you have to log into your router remotely say from a laptop to start a program then log into it again later from a different location to see the results of the program.
I wanted to setup GNU Screen on my router with a number of workspaces configured automatically at SSH login, the problem with that is if you establish a second SSH session the whole environment is duplicated again, and again et al.....
What I wanted therefore was a single setup with say 4 workspaces that the SSH session would, if detected, log into that existing environment rather then create a new one.
GNU Screen will allow this by using it's multiuser functionality.
To do this you need to edit 2 files and create a batch script which I've done for you below.
Firstly ensure that GNU Screen is installed on your router.
opkg install screen
Now the first thing is to edit your SSH file which is
vi /etc/profile
then add the following two lines to the bottom of the file
# Make GNU Screen the default setup for SSH logins
/usr/sbin/autosshscreen
Now save the file and exit.
Now lets add some stuff to our GNU Screen config file....the following is just an example, you should play with you own setup.
vi /etc/screenrc
startup_message off
multiuser on
acladd root
screen -t ash 0
screen -t htop 1 htop
# You can add other sessions here as you require, please read GNU Screen documents for examples.
select ash
# This determines the default session you appear in when connecting.
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %D %d %M %{W}%C %a %{g}]'
# The hardstatus string above generates a permanent line at the bottom of the display to show data like router name, Screen sessions and current date and time.
# This string is very flexible and you should explore customising it for your own needs.
Now save the file and exit.
Lastly we have to create the autosshscreen script referenced earlier.
vi /usr/sbin/autosshscreen
#!/bin/ash
SCREENRUNNING=`pgrep screen`
if [ -z "$SCREENRUNNING" ]; then
echo "Screen not running so let's start the Master session"
sleep 5
screen -S Master
else
echo "Screen is already running let's connect to existing session"
sleep 5
screen -x root/Master
fi
Now save this file and ensure you make its permissions executable.
Now on your PC or laptop open a new terminal session and ssh to the router.
ssh 192.168.1.1 (or whatever your routers IP address is)
If it's all worked properly you should see a message telling you that the router is starting the Master session followed by a display with the hardstatus information line at the bottom.
Now on your PC open another terminal session and ssh to the router again......
ssh 192.168.1.1 (or whatever is your routers IP address)
This time you should see a message telling you that Screen is already running before seeing the hardstatus line.
If you keep both terminal sessions visible on your display and type into one, you should see the type appearing immediately on the other as well.
That's it.......now instead of having maybe four or five ssh sessions to your router to allow you to do different things you should only need one or two.
If you want to get really funky GNU Screen even allows you to display more then one session at the same time using split commands but that's outside the scope of this HOWTO.
Hope someone finds this useful..
(Last edited by pinnoccio on 23 Aug 2012, 03:07)