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

10 Responses to “Keeping your SSH sessions alive with NOOP”

  1. David Says:

    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

  2. admin Says:

    David,

    That is a great resource. Thanks for sharing!

    I am going to give those ssh and putty options a go.

    Brock

  3. pb Says:

    What about just “watch -n99 uptime”? That seems to work well for me…

  4. admin Says:

    @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

  5. D Says:

    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.

  6. MERLiiN Says:

    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.

  7. MERLiiN Says:

    Craptastic.. your blog accepted my slashes so \\” became “. Probably because you use PHP. Hopefully you can edit the post for me.

  8. Jacob Says:

    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.

  9. danny Says:

    Cool tip man, i’ll link to your article later today.

  10. jeff Says:

    I’m not familiar with how this is supposed to work.
    Do you run this script in the backround, and continue using the ssh window (I’m using secureCRT)?
    If so, why do you have to print characters to the screen – can’t you redirect it /dev/null so it doesn’t show up?

    Also is the SIGINT caught when you close the window, or when the server tries to close the connection?

    Lastly, why do yoave to sleep for a random time?

    Thanks,
    Jeff

Leave a Reply

If Wordpress eats your comment (shell output, loops, ex..) brock (at) gmail dot com.