OpenWRT – Automatic email sending

I thought it is time to make it more smarter, so I improved it in some ways. It would be good if the router send me a message in some cases. Eg.: It has been rebooted.

wrt160nlopenwrtgmail

==Automount after boot==

Before downloading any packages and consuming all my precious disk space I have configured the exroot/overlay in a previous article. Only one thing that I have missed is that the USB drive will not automatically get mounted after a restart. I will add this missing piece now.

Based on Boot Process and Init Scripts I have created a script under the

 /etc/init.d/ 

and named it mountusb

touch mountusb
vi mountusb

The content of the file:

 
#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org
# Mount USB drive after booting and swapon

 START=100
 STOP=105

  start() {
          echo start
          # commands to launch application
          #TODO check if the device is exist
          swapon /dev/sda2
          mount /dev/sda1 /overlay
          }
  stop() {
          echo stop
          # commands to kill application
         }
  boot() {
          echo mountusb script
          swapon /dev/sda2
          mount /dev/sda1 /overlay
         }

If you have saved the file, you have to make it runnable you can do it by:

chmod +x /etc/init.d/mountusb

In order to enable it simply give that command:

/etc/init.d/mountusb enable

After I have rebooted the router, the overlay was already there without manually mounting, so it worked.

Some prerequisite is ready let’s go back to the main line. Also I have registered a new gmail account for my router.

==SSMTP==

I have researched a little bit, and I have chosen to the ssmtp to be my mail sender. There are other solution like mutt.

===SSMTP install===

It is very easy to install either you use the web interface(lucy) or the console as shown below:

opkg update
opkg install ssmtp

===SSMTP configuration===

It is necessary to config the SSMTP, to do that open and edit the config file:

vi /etc/ssmtp/ssmtp.conf

I did the following changes:
[mynewgmail] – change it to yours
[mynewgmailpassword] – change it to your email password

root=[mynewgmail]@gmail.com
mailhub=smtp.gmail.com:587
rewriteDomain=gmail.com
hostname=smtp.gmail.com:587
FromLineOverride=YES
UseTLS=YES
USESTARTTLS=YES
AuthUser=[mynewgmail]@gmail.com
AuthPass=[mynewgmailpassword]

Also you have to edit the /etc/ssmtp/revaliases . Mine looks similiar to this:

vi /etc/ssmtp/revaliases 

[username] – replace by user
[mynewgmail] – change it to yours

[username]:[mynewgmail]@gmail.com:smtp.gmail.com:587

===SSMTP test===

Now let’s try it out if it works! To do that it is very simple:
[user@gmail.com] – Change it to your email address.

ssmtp [user@gmail.com] 
Hello World!

After you are finished with the message press CTRL + d. Then you should it in the mail box where you have sent the mail.

== Send a mail if there was a reboot ==

Just for showing an example what you can bring it out if you did the previous steps. Let’s say that if the router for some reason(system crash, power shortage, etc. …) reboots, then it should send some mail.

I have already showed how to effect things after reboot in the first part. Let’s do the same and create an init script like this:

touch /etc/init.d/emailafterreboot
chmod +x /etc/init.d/emailafterreboot
vi /etc/init.d/emailafterreboot 

The content of the file:

[user@gmail.com] - replace it to valid mail where you want to send the message.
#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org

 START=99
 STOP=100

  start() {
            echo start of the email sending after reboot
            ssmtp [user@gmail.com] < /etc/ssmtp/mail.txt
          }

  stop() {
            echo stop
            # commands to kill application
         }
  boot() {
            echo start of the email sending after reboot
            ssmtp [user@gmail.com] < /etc/ssmtp/mail.txt
         }

When you are finished with the editing, then enable it by:

/etc/init.d/emailafterreboot enable

You can try it out if the scripts works with that command:

/etc/init.d/emailafterreboot boot

The try it if really works by rebooting. Write this to the console.

reboot
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s