by Peter Lavin
December 22, 2011
I’ve used OpenWrt (Kamikaze 7.09) installed on a Linksys WRT54GL (v 1.1) router for a few years. When I initially installed OpenWrt I made changes to the dnsmasq.conf file configuring bogus domains, setting up text records for machines on my LAN and other such things. OpenWrt works so well that after a while you tend to forget about it.
When I realized that I could programme the button on the face of my router I couldn’t resist taking another look at OpenWrt. What to do with the button though? There’s no point in making it a reboot button- you never have to reboot an OpenWrt router. Toggle the wi-fi? I don’t think so.
Instead, I decided to send the logs by email. One click on the face of my router and a few moments later my phone is telling me I’ve got ‘OpenWrt Logs’.
Note
This article is geared towards Kamikaze 7.09 on a Linksys WRT54GL router. However, the ssmtp package is available for all versions of Kamikaze and for Backfire so what’s described here may well be more widely applicable.
ssmtp is a minimalist sendmail clone ideal for use on embedded systems. You can find out more about it from the command line of your router by using ipkg info ssmtp. You should see something similar to the following:
Package: ssmtp
Version: 2.61-2
Depends: libopenssl
Provides:
Status: install user installed
Section: net
Architecture: mipsel
maintainer: OpenWrt Developers Team <openwrt-devel@openwrt.org>
MD5Sum: 8bd9deb9af9e1d5a0ab82512a87c8fef
Size: 12727
Filename: ./ssmtp_2.61-2_mipsel.ipk
Source: /data/release/packages/packages/net/ssmtp
Description: A minimal and secure mail sender
A secure, effective and simple way of getting mail off a system to your
mail hub. It contains no suid-binaries or other dangerous things - no
mail spool to poke around in, and no daemons running in the background.
mail is simply forwarded to the configured mailhost. Extremely easy
configuration.
The ssmtp package requires libopenssl. To find out more about this package use ipkg info libopenssl. Note that the file size is 472517. Check that there is space available by going to the command line and using the df command.
Issuing the command ipkg install ssmtp will install the ssmtp package and the libopenssl package.
After installation you can find the ssmtp configuration file, ssmtp.conf, in the /etc/ssmtp directory. Change or add the configuration options shown below:
mailhub=smtp.gmail.com:465
AuthUser= *name*@gmail.com
AuthPass= *secret*
FromLineOverride=YES
UseTLS=YES
In this case, the GMail mail server is being used. Add values appropriate to your circumstances. Setting FromLineOverride to YES is optional.
After changing the ssmtp configuration file, test that the ssmtp command functions properly. To do this, first add the following file to the /etc directory naming it email:
To: *myaddress*@gmail.com
Subject: OpenWrt Logs
From: *other_address*@gmail.com
Log Files
Add appropriate values for the sender and recipient. Leave an empty line after the From: line so that the body of the email is separated from the sender line.
Test sending an email by navigating to the /etc directory and issuing the command ssmtp -vvv `to@example.com` < email. Use the -vvv option for verbose output-you’ll see every phase of the SMTP transaction.
If you are not successful then an error message is displayed and a dead.letter is created in the home directory. With verbose output set, you’ll see the SMTP response code 250 which indicates success but check your email account to make sure that the message was received.
Now that ssmtp is installed and working, you can begin coding the router’s button.
Create a directory in /etc/hotplug.d called button and then create a file in /etc/hotplug.d/button called send_logs. Paste the following code into that file:
#!/bin/sh
logger $BUTTON
logger $ACTION
Creating this file lets you determine the button name and action. Make sure that this file is executable by issuing the command chmod +x /etc/hotplug.d/button/send_logs. Press the button on your router and then from the command line issue the logread command. The final log entries should display the button name and action.
Once you’ve discovered the button name, replace the send_logs file with the following changing ses if required.
#!/bin/sh
# Mail the logs when the "ses" button is pressed
if [ "$BUTTON" = "ses" ] && [ "$ACTION" = "pressed" ] ; then
cd /etc
cp email email.new
logread >> email.new
ssmtp -t < email.new
rm email.new
fi
When the button is pressed this script navigates to the /etc directory and copies the file containing the addressee, sender and subject line. The router logs are appended to this file which is then mailed via ssmtp to the recipient defined on the To: line in the file. The email.new file is then removed.
Now that you can send log files at the press of a button you should know what they’re telling you. dnsmasq uses the syslog facility and writes a circular log to memory. This log is read using the logread utility.
Find below sample log entries related to sending an email:
...
Dec 22 17:05:24 OpenWrt mail.info sSMTP[697]: Creating SSL connection to host
Dec 22 17:05:25 OpenWrt mail.info sSMTP[697]: SSL connection using RC4-SHA
Dec 22 17:05:29 OpenWrt mail.info sSMTP[697]: Sent mail for root@openwrt.lan
(221 2.0.0 closing connection h9sm18368679qac.13) uid=0 username=root outbytes=13989
Dec 22 17:17:39 OpenWrt syslog.info -- MARK --
The meaning of the fields is as follows:
Records with the message – MARK – are “heartbeats” sent every twenty minutes, indicating that logging is still active.
Peter Lavin is a technical writer who has been published in a number of print and online magazines. He is the author of Object Oriented PHP, published by No Starch Press and a contributor to PHP Hacks by O’Reilly Media.
Please do not reproduce this article in whole or part, in any form, without obtaining written permission.