why unix | RBL service | netrs | please | ripcalc | linescroll
hosted services

hosted services

If you like GNU Screen, why not take a look at ratpoison.

overview

Screen is a great program for keeping console applications running and storing their output.

Screen is an essential part of any admins toolkit.

One of the big advantages of screen over a normal terminal is the bindkey function. This allows you to assign a series of keystrokes to a smaller sequence. Thus typing !at could tell screen to insert sudo apache2ctl configtest && sudo apache2ctl graceful for example. This together with the scroll back function and logging creates a very versatile and solid application.

In the below .screenrc file the strokes !ht cause apache to run a clean and !sb is an internal screen command sequence to reset the scrollback lines.

In order to put a carriage return into a stuff command from within vim press ctrl-v followed by ctrl-m.

bindkey -t !ht stuff "sudo apache2ctl configtest && sudo apache2ctl graceful^M"

Some of the most common screen commands I use,

command purpose
c create new window
H turn on/ogg logging
n next window
p previous window
A change window name
' jump to window number
" list windows
k kill current window

automation

It took a while but I thought there would be a way to send some data to a pts to prevent it from timing a session out, although this path proved too complex through /dev/pts and unfruitful for me. GNU Screen does have a mechanism to do this however, though -X. For example, from cron, I've configured this, to send the .info command to a screen session:

1       1       *       *       *       /usr/bin/screen -rd talker -X stuff '^U.info^M'

This, tells the session named 'talker' to stuff the text '.info' into the session. Replace ^U and ^M with ctrl-v followed by u/m (carriage return character), this simulates the user clearing the line and typing .info followed by the return key.

Automation can be further enhanced though the -p argument.

Screen makes it very handy if you want to automate both the left and right hand site of an application (simulate the user and server).

scroll back

Scroll back doesn't include all the movement functions in vim, but there is some overlap with the key strokes

keystroke action
h Move the cursor left by one character
j Move the cursor down by one line
k Move the cursor up by one line
l Move the cursor right by one character
0/^ Move to the beginning of the current line
$ Move to the end of the current line
g Move to the begging of the buffer
G Moves to the specified line (defaults to the end of the buffer)
C-u Scrolls a half page up
C-b Scrolls a full page up
C-d Scrolls a half page down
C-f Scrolls the full page down

marking

| keystroke | purpose | | v | Sets line numbers | | a/A | Press this after selecting text and the following space will append marked text to the buffer rather than overwriting the contents, uppercase to toggle | | > | This writes the buffer to the screen-exchange file where the contents can be read later. | | x | reposition the start of the marker |

log files

One of the really, really useful things about GNU Screen is that you can configure it to record everything that happens in a given window to a log file containing the date, time, window number and session name at creation.

This is highly useful if you're intending to trace over your steps at a later date. Simply add the following to your .screenrc to bring this life changing feature to life.

deflog on
logfile logs/%Y%m%d-screenlog-%0c-%s-%t.%n
log on

The values above translate as follows:

symbol definition
%Y Year, in YYYY format
%m Month, in MM format
%d Day, in DD format
%c Time, HH:MM in 24h format
%s Seconds
%t Window title
.
%n Window number

I find this generates a very unique file name which is easy to locate later. Although scrollback buffers is very good to locate something immediately, should the machine running your screen sessions die, you'll have the copy on disk to refer to later.

sessions

Suppose you want a set of sessions created to various hosts, one way to do that might be something along the lines of:

$ screen -d -m -S mega
$ for i in host1 host2 host3; do screen -rd mega -X screen -t $i ssh $i; done;

This will create three sessions to host[1..3] with the title of host[1..3]. This is perhaps one of the most useful features that I've found with screen (besides all the other useful things of course). There is a maximum of 40 windows allowed with the default build of screen. If you want to change this, reconfigure your screen build with -DMAXWIN=255, or edit the config and set the MAXWIN there.

When using the screen command it might be worth noting the following very useful parameters

parameter description
-T Sets the TERM environment, such as vt100, xterm, xterm-color and xterm-256color. This is highly useful if the default TERM does not support the bells and whistles of xterm
-M Monitors the window for activity
-t Sets the title of the window

pre-selection

In the previous example we have a number of screen windows with title host1. Should you wish to select for example host1 and list a file, then write something to the terminal you might do something like this:

$ screen -rd mega -p host1 -X stuff 'ls -al ~/.screenrc^Mecho joy^M'

I hope this gives you an idea of how even more useful screen can be than it first appears! I often use pre-selection when the window list cannot be searched through, for example rather than scan through 40 window titles with my eye balls I can drop out of the screen session and use screen -r -p /name/ to jump to the window title /name/.

must be connected

On my account at work I don't have the luxury of hibernation, so instead I like to have evilwm spawn some terminals and for each of them to attach to a screen session on the file server, just as I left it. This in many ways is a bit better than hibernation as the connections inside that screen session does not die. The first time I tried this I had an error:

$ ssh user@fileserver screen -r main
Must be connected to a terminal.

After spending a long time reading the screen man page I thought of something, the -t ssh parameter.

Once you tell ssh to use the screen program as your tty things work.

$ ssh user@fileserver -t screen -r main

The ssh man page describes this as:

 -t      Force pseudo-tty allocation.  This can be used to execute arbi‐
         trary screen-based programs on a remote machine, which can be
         very useful, e.g. when implementing menu services.  Multiple -t
         options force tty allocation, even if ssh has no local tty.

nethack

If you have ever enjoyed the game nethack then you'll enjoy putting the following in your .screenrc:

nethack on

Messages which get printed from screen should now have a pleasing nostalgic twist to them.

screen and email

This is something that covers both screen, vim and mutt. I've not found the best way to use vim for email whilst have it take care of email headers and quotation reliably. The reason for this is that vim is a line editor and not a screen editor. The best solution I've found for vim to autoformat emails is have a macro reformat the section of the email that you've written once you have finished composing the message.

To do this I have the following key binding:

bindkey -t !refm    stuff   "^[mz:set tw=72^M:1^M/\^\(On \|-- \)^M{vgg}gq:set tw=8192^M`z"

In the above entry you will need to replace all occurrences of ^[ with ^V^[ in order to store the escape sequences.

This macro presses escape for you, sets the textwidth to 72 col, goes to the first line of the email, searches for the bottom of your edit, goes to the top of the paragraph, blocks, goes to the top of the message, goes to the first blank line, and reformats the visual section.

This only works for top-posting, it can be adapted for bottom-posting too.

copy-mode

Time and time again whist using screen I wonder to myself how on Earth non-screeners get along in their day-to-day admin. One feature that I use a lot is the screen copy and buffer modes.

To enter screen copy-mode, use ^a [ or ^a ^[ (^[ = escape). Once in copy mode you can move around using vi modement or arrow keys. To copy a screen area use space to drop the marker and space to end the marker. x moves the marker start point, c and C set the start/finish columns.

Once you have the screen data in the copy buffer, use ^a > to write that buffer to your screen exchange file, or ^a < to read the contents of the exchange file into your copy buffer.

This is such a useful feature of screen that I struggle to think how people can exist without it.

split screens

To have two virtual windows in one screen terminal you can type ^a S, this will split the terminal vertically. I like to use vi bindings, so I put this in my .screenrc:

bind h focus left
bind j focus down
bind k focus up
bind l focus right
bind t focus top
bind b focus bottom

^a j will move down, ^a k will move up.

digraphs

At first I didn't know what this section in the manual was. This gem of a feature allows the user to ender Unicode characters from a standard keyboard using two key strokes.

To access this feature, enter the following:

^a ^v

You'll be prompted with the following: Enter digraph:

At this point you'll be able to enter the two character code. There are several understood digraph combinations in GNU Screen's vocabulary (incomplete):

character code character
!I ¡
¢
ct ¢
$$ £
Pd £
ox ¤
Cu ¤
Cu ¤
Eu ¤
Y- ¥
Ye ¥
| ¦
BB ¦
pa §
SE §
"" ¨
': ¨
cO ©
Co ©
a- ª
<< «
-, ¬
NO ¬
-- ­
rO ®
Rg ®
-= ¯
'm ¯
~o °
DG °
+- ±
22 ²
2S ²
33 ³
3S ³