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

