It is often said that computers will make life a lot easier. In my opinion, that era still lies in the far future. Until then I will post here solutions, which emerge from the daily struggle with the silicon evil. Maybe they are even useful to others.

X11 Remote Control

Monday, 28 January 2008 15:36

x2x is a brilliant little remote control for X11. For example when you are working at different computers simultaneously it can make life much easier. Mouse and keyboard commands are sent to other xservers as well as it copies the clipboard. The standard X11 protocol is used, so the usual tricks and ssh tunnels work fine.

I have a shortcut on my laptop to “invite” the keyboard and mouse of my desktop at work. To avoid exposing the xserver, a bit of ssh tunneling is done:

ssh -f -n -X
  'export DISPLAY=:0.0;ssh -n -f -X cap@`echo $SSH_CLIENT | cut -d" " -f1` x2x -west -to :1.0';
echo "connecting X server ..." |
  osd_cat -A right -s 2 -f -*-helvetica-bold-r-&#-20-&#-&#-&#-*

After that, the mouse pointer can leave the desktop screen on the left (west) and will enter the laptop screen on the right. Whichever screen the mouse is residing in will receive the keyboard events from the desktop keyboard. osd_cat is merely used to print a notification of what is happening on the display.


Crop PDFs

Monday, 28 January 2008 14:22

I came across a lot of ebooks in PDF format which are in principle suitable for display on the CyBook (e.g., Planet PDF), but feature margins that are way too large. Also, headers and footers, and advertisement are not really required. To crop a PDF file and maintain the structure like bookmarks and links can be done by replacing the MediaBox or CropBox and repairing the XRef table with pdftk (pdfcrop2):

perl -pe "s/(Crop|Media)Box\s*\[(.+?)\]/\$1Box\[$2\]/g;" $1 | pdftk - output $3

The script takes three arguments:

  1. Input file
  2. New crop box, i.e. "left bottom right top" in postscript pixels starting from the top left, e.g. "0 0 612 792". To obtain this one could use xv or any other image manipulation program that can open PDF pages without looking at their CropBox after.
  3. Output file

Automatic ssh tunnel

Sunday, 27 January 2008 14:32

The drawback of high security guidelines and policies against spam is that access to an SMTP server may only be granted from inside a company's or university's intranet. If you want to use this server from the outside, the easiest solution is to set up an ssh tunnel to a computer available inside the company's intranet:

ssh -L localport:mailserver:25

This tunnel has to be set up always before sending emails. For automation the xinetd (or inetd) can be used. Xinetd will be configured to listen at a local port (which will be our local SMTP port) and relay the traffic to/from stdin/stdout of an ssh tunnel. ssh, in turn, relays the traffic to a netcat (or nc) instance, running on the trusted computer, finally forwarding everything to the mailserver. For security on our side, xinetd will only listen for local connections, but the system can be used in principle as a replacement for a VPN where only few ports are of interest.

All that is needed is a proper configuration of xinetd. Here an example for an automatic tunnel, listening on localhost:25 (/etc/xinetd.d/mailtunnel):

service smtp
  port        = 25
  socket_type = stream
  protocol    = tcp
  only_from   = localhost
  interface   =
  wait        = no
  user        = local_user_with_appropriate_ssh_key
  group       = the_users_group
  flags       = REUSE
  server      = /usr/bin/ssh
  server_args = -x -a -q -T netcat mailserver 25

Move windows between XServers

Saturday, 26 January 2008 22:31

Who has ever been working on more than one Linux computer simultaneously knows the problem: I need this window on the other computer's screen.

The solution: xmove. Usage is quite simple: start another XServer on port :1 by issueing the command

xmove -port 1

All windows that shall be movable to other displays should use this server. By default xmove displays them on the active XServer. To move them to another display type

xmovectrl :1 -moveall :10

where :10 denotes the 10th XServer. Because the standard X protocol is used, this also works over ssh or together with Xnest.


CyBook Gen3 PDF thumbnail images

Saturday, 26 January 2008 22:16

One drawback of the CyBook's PDF support is the missing metadata. For example, it simply does not display preview images of PDF files in the browser.

However, when looking around a bit on the SD card, some files with the extension .t2b seem to be the right candidates. It turns out that they are 4-bit greyscale images of the size 144x96.

Here is a listing of my pdftot2b:

  1. #!/usr/bin/perl
  3. use GD;
  5. my $palette = $0;
  6. $palette =~ s/[^\/]+$/palette.gif/;
  8. while ( my $file = shift @ARGV )
  9. {
  10. open IN, "pdftops -f 1 -l 1 -paper match $file - |
  11. convert -resize 96x144 -type TrueColor PS:- JPG:- |
  12. djpeg -pnm -map $palette -dither ordered |
  13. pnmtopng 2>/dev/null |";
  14. my $in = join( '', <IN> );
  15. close IN;
  17. $image = GD::Image->new( $in );
  19. $k = 3;
  20. @px = ( );
  22. $left = int( ( 96 - $image->width ) / 2 );
  23. $right = 96 - $image->width - $left;
  24. $top = int( ( 144 - $image->height ) / 2 );
  25. $bottom = 144 - $image->height - $top;
  27. $file =~ s/\.pdf$/_6090.t2b/;
  28. open OUT, "> $file";
  30. for ( $i = - $top; $i < 144 - $top; $i ++ )
  31. {
  32. for ( $j = - $left; $j < 96 - $left; $j ++ )
  33. {
  34. $px[$k --] = ( ( $i >= 0 and $i < $image->height and $j >= 0 and $j < $image->width )
  35. ? $image->getPixel( $j, $i )
  36. : 3 );
  37. if ( $k < 0 )
  38. {
  39. # this line can be here because 4 divides 96
  40. print OUT chr( $px[0] + 4 * $px[1] + 16 * $px[2] + 64 * $px[3] );
  41. $k = 3;
  42. }
  43. }
  44. }
  46. close OUT;
  47. }
Listing: pdftot2b

The purpose of the file palette.ppm is to give the dithering algorithm the codes of the four colours we want to use. I started with four equally spaced grey codes (0x00, 0x55, 0xaa, 0xff). It should lie in the same directory as the script file.

The conversion performed in the lines 10–13 could of course be done by convert alone. However, the result of the dithering by means of djpeg was slightly more satisfactory.

update (2008-01-29): Of course, the whole script can be used to generate thumbnails from any format that is supported by ImageMagick by adapting lines 10–13 and 26 appropriately. Here is a modified version of the script: t2b.


Toshiba Portégé M400 hard disk replacement

Saturday, 26 January 2008 22:08

Constant freezing of the system led to mainboard and hard disk replacement. The backup was a simple disk images by means of dd. After replacement the backup wouldn't work. It turned out that the BIOS stores some information about the RAID mode in the MBR. So the path to restored data is the following:

  1. Install the new hard drive and make all necessary changes in the BIOS (JBOD/RAID0).
  2. Restoring everything but the first sector.
  3. Generate a new partition table from scratch rather than replacing the whole MBR.

Page 2 of 2