According to “official” system administrator rules and guidelines you shouldn’t be adding so-called vain scripts to the login prompt – only utilities that will add something useful to the system (for example, current system load, memory and disk usage, etc). However I have some systems that I frequently connect to and thought it would be neat to add a random quote script to my bash login. That being said, this should only be done on ‘non-production’ systems and adds a security vector so please be careful where you use this.

The goal of this is to add a little quote, at random, every time you log into your system. My thoughts were to do it not only as a little source of inspiration but also to add perspective to what I’m doing sitting in front of the computer all of the time.

Originally I was going to try to write the script solely in bash since it is so flexible (and just as a proof of concept) but dealing with RSS in bash isn’t exactly pretty and I just wanted to get this together as quick as possible. PHP makes parsing XML easy, there are a number of ways to accomplish it. I chose to use the ready-made script at rssphp.net to do this, if you are curious about how you can handle this yourself using SimpleXML check out this tutorial over at Pixel2Life. The end result of my solution is a bash script calling a php script to grab the quote.

The Code

First create a file named /etc/update-motd.d/10-quote. The name does not matter much – the number will decide what order the script is called in of all the scripts in /etc/update-motd.d. Do an ls on that directory to see what all is being called when you log in. Add the following lines to this file, assuming you are placing your scripts in /etc/scripts/:

#!/bin/sh
echo ""
/usr/bin/php /etc/scripts/getquote.php
echo ""

Download v1 of rssphp and extract it to the /etc/scripts/ directory. We will require that file in our php code.

Create the file /etc/scripts/getquote.php and add the following:

load('http://www.quotedb.com/quote/quote.php?action=random_quote_rss');
$rssitems = $rss->getItems();

if ($rssitems) {
// print_r($rssitems);
echo $rssitems[0]['description'].' :: '.$rssitems[0]['title']."\n";
}

?>

I am using the RSS source from QuoteDB as the source of my quotes. Of all the places I checked (and I checked a lot) they seemed to have the most appropriate ones for this use. Feel free to use any source you wish – as long as the XML fields title/description hold the quote you will be able to use it. The RSS url was not obvious from the site and I had to do some digging to find it, in the end I am using http://www.quotedb.com/quote/quote.php?action=random_quote_rss.

We also add the if statement to allow it to degrade nicely in case you have no network connectivity to the server. After a short period – a second or two – it will time out and let you log in.

The end result is a pretty quote in our motd:

Linux vps01.[redacted].com 2.6.18-2-pve #1 SMP Mon Feb 1 10:45:26 CET 2010 x86_64 GNU/Linux
Ubuntu 10.04.1 LTS

"The absence of alternatives clears the mind marvelously." :: Henry Kissinger

root@vps01:~#

It should be pretty strait forward; let me know if you run into any problems!

4 comments
  1. I did stumble across fortune, which was unfamiliar to me (despite its long-term existence!), but I wanted to grab it from an online source so that I didn’t have to manage a database of quotations. My thinking is that they would be more varied than a source that I self-select. Thanks for the tip!

  2. curl -s ‘http://www.quotedb.com/quote/quote.php?action=random_quote_rss’ | xml sel -t -m ‘//item’ -v ‘description’ -n

    This is xmlstarlet in action. I was just wondering which distro are you using? I don’t have /etc/update-motd.d, and you do have /etc/scripts or you made this dir? /etc is not really good place for this…

  3. Nice, thanks for the info about xmlstarlet, I hadn’t heard of it. I’ve tested on an Ubuntu 10.04 and that is the distro with /etc/update-motd.d dir. I’m not sure which other distros include that but it can be placed whereever.

    xmlstarlet was not installed for me, but was available via apt-get in the default sources. It did not provide ‘xml’ to me but you could easily link it to xmlstarlet.

    You are right /etc/ is not the best place for this; probably /usr/local/scripts/ or similar. I use /etc/ since I back all of those files up. :)

Comments are closed.

You May Also Like

PHP Script for controlling APC PDU via SNMP

This one is neat – you can control those APC PDU Power…

PowerDNS flexible, fast DNS Server

I’ve recently been testing/installing PowerDNS for a web hosting provider. Man am…