Keeping your SSH sessions alive with NOOP
March 12th, 2008
In the past, my SSH sessions died due to inactivity. In order to solve this, I used to:
while true; do uptime; sleep 5;done
Obviously, this eventually clears your terminal history. BASH to rescue! My noop script solves this problem. (Please see comments, there maybe a better solution, thanks David!) noop, standing for no operation, is a processor instruction and is common in protocols. You may find it interesting, that exploit code is filled with NOP’s. The operation increases your chances of exploiting buffer overflows
The source:
$ cat /usr/bin/noop
#!/bin/bash
backspace() {
echo -e "\b\c"
}
cleanup() {
backspace
exit
}
trap "cleanup" 2
while :
do
num=${RANDOM:0:1}
printf $num
sleep ".$num"
backspace
done
For the hell of it, I made a video of noop in action.
If your wondering how the script works, here is a quick explanation. The script defines two functions. backspace and cleanup. Backspace prints the special characters \b and \c. Backslash b is a backspace, and backslash c, stops echo from printing a trailing newline:
backspace() {
echo -e "\b\c"
}
The cleanup function prints a backspace and then exits. The cleanup function is run by trap when it receives a SIGINT (2):
cleanup() {
backspace
exit
}
trap "cleanup" 2
The main body of the script, is an infinite loop which generates, a random number using the special variable $RANDOM. This random is assigned to the variable num, utilizing only the first digit. After printing that number, the script sleeps num tenths of seconds, and the backspace function is called:
while :
do
num=${RANDOM:0:1}
printf $num
sleep ".$num"
backspace
done


March 12th, 2008 at 8:20 pm
A better solution is to correctly configure your “TCPKeepAlive” and “ServerAliveInterval” options for your particular SSH connection, see:
http://drupal.star.bnl.gov/STAR/comp/sofi/facility-access/ssh-stable-con
March 12th, 2008 at 11:41 pm
David,
That is a great resource. Thanks for sharing!
I am going to give those ssh and putty options a go.
Brock
March 13th, 2008 at 5:09 am
What about just “watch -n99 uptime”? That seems to work well for me…
March 13th, 2008 at 8:08 am
@pb,
Thats a huge improvement over the while true uptime loop. However, my noop script will not fill your terminal with crap. When you CTRL-C the script, all output disappears.
Brock
March 14th, 2008 at 5:48 pm
i just use top to keep my sessions alive, plus i can keep an eye on what’s going on at a glance as well.
March 17th, 2008 at 7:43 am
Although David’s suggestion is the best practice…
I would probably spawn something like this in the background from my .bashrc file:
#!/bin/sh
X=0
while :
do
echo -n “33]0;CONNECTED FOR $X SECONDS07″
sleep 1
done
That should only update your window title and not print anything to your screen. The actual echo string would depend on your shell and terminal. See http://www.faqs.org/docs/Linux-mini/Xterm-Title.html for further details.
March 17th, 2008 at 7:45 am
Craptastic.. your blog accepted my slashes so \\” became “. Probably because you use PHP. Hopefully you can edit the post for me.
March 17th, 2008 at 11:16 am
Hmm - I always used:
ping -i 300 -s 1 yahoo.com
As you mention though, that’s rather ugly when you’re working in the same shell. I only us it when the sole purpose of my ssh connection is to set up a tunnel for other ports.
March 17th, 2008 at 10:44 pm
Cool tip man, i’ll link to your article later today.