A gentle introduction to GNU screen

You probably heard of GNU screen. It’s handy, ubiquitous, and dead simple. Here’s how to use it!

Open a terminal and type:

screen

You’re welcomed by an introduction message, press enter, and… You’re in a shell. Uh?

Screen is simple

screen is a terminal manager, so it’s logical that the first thing you see when you start it is a terminal.

This terminal is as vanilla as the terminal we started from. Just try it:

See? No black magic here, simply a terminal.

Screen is simple

The only difference is that Ctrl+a is now a special key combination that you can use to invoke screen‘s commands.

So let’s take a break and quit screen. Type Ctrl+a to let screen know you want its attention, then d, as in detach. There! You’re back in your first terminal.

Let’s go back in screen and learn some more! Just type:

screen -r

The -r stands for reattach: screen will re-open the last session, the one we detached from. You can see the results of the commands we entered earlier are still here.

We just saw a great feature of screen: the ability to log out and log back in without losing anything. Do you have something long to on a server? SSH into the server, launch screen, launch the task, detach from screen, log out from SSH, go back home, enjoy a good dinner and a well-deserved night of sleep, come back to work, SSH into the server, launch screen -r, and it’s just as if you never left.

Screen is simple

You can already use screen just like that, but let’s just see another nifty feature: multiple terminals!

In screen, type Ctrl-a, then ‘c’, short for create. You’re in a shell. Uh?

Screen is simple

You just created another terminal. screen can manage plenty of simultaneous terminals, not just one. To see a list of them, type Ctrl+a, then the quote symbol “, and you will see your two terminals. Just use the arrows to select which one you want to open.

There you go, you know screen! See, I told you it was simple.

Misc useful commands

Do you want to change the name of a terminal in screen‘s list? In that terminal, Ctrl+a and A.
Do you want to go directly to a specific terminal? Ctrl+a and its number.
Do you want to go to the previous/next terminal? Ctrl+a and p or n.
Do you want to switch to the previous terminal quickly? Ctrl+a and Ctrl+a.
Do you want to remap Ctrl+a to another key, say Ctrl+b? Just put escape ^b in your .screenrc.

Remaildr – the tech bits

Here are a few small things you might want to know about http://remaildr.com. Or maybe not, but then again, nobody forces you to read, stranger!

“Hardware”

Remaildr is hosted on an Amazon EC2 micro instance, benefiting of the free tier offer. Apart from the static IP that will probably end up costing me something, remaildr should be about free.

— Edit: as of may, remaildr is now hosted on a VPS at OVH. The EBS volume of my EC2 instance blew up on me, and with the free tier coming to end, EC2 would be too costly.

Network

The remaildr.com domain is registered at OVH, because of the low price and the flexibility they allow on DNS. I added an A record for mail.remaildr.com pointing to 50.16.218.96 —the AWS elastic IP—, then modified the MX record for remaildr.com to point to mail.remaildr.com. That way, every email sent to any_address@remaildr.com will be sent to the right mail server. Having an A record also allows reverse DNS on the mail server, often used to flag spam.

Other DNS modifications included the SPF record, which allows the mail server to actually send emails in behalf of remaildr.com, and a TXT record for DKIM — cryptographically signing outgoing emails.

OVH provides a free 1MB web storage for each domain name subscription, which is more than enough to host the remaildr.com website, weighing about 30KB.

The mail server

The email server at OVH is a run-of-the-mill Debian Squeeze. It runs a Postfix server, configured to forward a few specific email addresses (for example abuse, postmaster and info) to my account, and let everything else go to a catch-all account called remind.

A set of two Daemonized Ruby scripts will then do all the work:

  • receivr.rb will fetch the emails in POP, compute the send date, then put the remaildr to send back into a PostgreSQL database as a Base64-encoded marshalled ruby object (akin to how DelayedJobs works as far as I understand)
  • sendr.rb will read the database and send all the emails who need to be sent

Of course, the code is on GitHub.

That’s about it! Feel free to ask any questions, and I’ll answer as well as I can. :)

Photocard – a Java postcard generator

As a school project, we recently finished Photocard, a Java application for Linux that allows you to design postcards (‘we’ as in a couple of other students & me).

Basically, Photocard listens to /media/ for an USB key, lets you chose a blueprint for your card (that might contain text and pictures), then drag and drop pictures into that blueprint, retouch them, and print your card (actually save it to /tmp/).

This is not a commercial-grade product, of course, and there are a lot of details I would have changed and improved, given some more time. For example, rotating pictures is choppy, luminosity and contrast aren’t well defined, the notification system is botched, the XML parser seems way too complicated for what it does, the user interface could be improved… And we used the MVC pattern as good as we can, but I don’t think we respect it fully.

However, it works! I’m pretty proud we managed to get it done on time. And some bits of it are really cool and pushed me to think about OO concepts in depth (anonymous classes & reflection in particular).

The project itself might be useful to someone out there, so we released it on Sourceforge under the GPL. All the comments (and the application itself) are in French, though.

Have fun!

http://sourceforge.net/projects/photocardcpe/

Tunneling iTunes through SSH

If you want to listen to your music library on the go, you can tunnel iTunes through SSH in a few easy steps.

First of all, you have to enable sharing in iTunes: iTunes > Preferences > Sharing > Share my library on my local network.

This will announce your library on your local network using multicast DNS, provided by Apple’s implementation of zeroconf: Bonjour. The library can then be accessed on port 3689 of the computer mDNS announced.

Hence, to access your local library on a distant network, we will implement two things: ssh tunneling of port 3689 for the data to be transmitted, and mDNS announcement of the library on the new network so iTunes is aware of where to get it.

We will assume:
– local = where me & my laptop are
– remote = where iTunes is (=server).

Tunneling port 3689

We will tunnel port 3689 of the remote computer to a port of your choice on your local computer:

ssh -N florent@myServer -L 3690:localhost:3689 -f

-L is the standard syntax for SSH tunneling. man ssh tells us the three arguments stands for port:host:hostport; as in “send data received at my own port to the hostport of said host“. Here for example, it means: tunnel all data received on my port 3690 to this SSH connection’s localhost (myServer) on its port 3689.

Feel free to change 3690 to the port of your choice, replace florent@myServer by your own login/host combination, and add a -p if you use a non-standard port for SSH (which is a Good Thing(tm)!).

The -f argument means the SSH tunnel will stay in the background. Finally, -N tells SSH not to execute a command on the remote side (i.e. launch a terminal on the server), meaning we will only use this connection to forward data.

Announcing the library

We will use this command to declare the new library:

dns-sd -P iTunesServer _daap._tcp local 3690 localhost.local 127.0.0.1 &

This command sets up a DNS proxy (-P) called iTunesServer, retransmitting DAAP announcements, on the local domain, on port 3690 of host localhost.local (at address 127.0.0.1).

You should get the following output from dns-sd:

 Registering Service iTunesServer._daap._tcp.local host localhost.local port 3690
 0:24:30.140  Got a reply for service iTunesServer._daap._tcp.local.: Name now registered and active
 0:24:30.140  Got a reply for record localhost.local: Name now registered and active

Note that this will only advertise the library on your own computer. If you want other computers on your network to see the library you’re tunneling to, use the following command, where ComputerName is the Bonjour name of your computer (set in System Preferences > Sharing > Computer Name):

dns-sd -P iTunesServer _daap._tcp . 3690 ComputerName.local ComputerName.local &

Annd your library will show up in iTunes!

iTunes shows the remote library
Victory!

Playback can be a bit choppy if your server’s upload or laptop’s download is low, but it usually should be okay – MP3 and AAC yields great results around 192 to 320 kbps, equivalent to an average 25 to 40 kB/s transfer rate.

Note that this method also allows you to use a password (set up in iTunes) to protect your shared libraries.