Monday 31 March 2014

Ubuntu Server - Automatic E-mail of Status upon Boot

As the multiboxer project has grown, I now need to support multiple bug reporters (I have three helpers now, if you've not donated yet and joined in you're missing out!)....

So with the new bug reports I'm looking at establishing a server at home, with a private IP and allowing the helpers to post their tickets there.  The trouble is, the loft man lab, where the server is stationed, is serviced by wireless only.  The signal strength is good, and I'm not going to throw a cat-5-e cable up there because I've spent too much time fixing walls and removing old dead telephone cables as it is.

The server is a Ubuntu 12.04.4 LTS box, and when it boots I want it to e-mail the testers, and me, its IP Address.  This way I don't have to faff about with dyndns (as much as I love them, their usage terms are a pain in the arse for free users now-a-days) or any other dynamic domain name service.

Now, the IP mailing just needs to send the interfaces information out, through a few tricks the IP of eth0 is the router outbound IP, and I've carefully opened port 80 for them to chat to it over the web...

So, to set this all up... We need a script to wait an amount of time, then get the adapter information, and then e-mail it...

sudo nano /usr/sbin/bootmail.sh

The script then looks like this:

#!/bin/bash
echo Waiting...
sleep 30
echo Getting Interface Info
ifconfig > /tmp/ifinfo.txt
file=/tmp/ifinfo.txt
echo Sending mail...
function mailalert(){
  echo Calling Mail...
  sendmail -F "noreply@Git-Server-VM" -it << END_MESSAGE
To: user1@gmail.com,user2@gmail.com,xelous@hotmail.co.uk
Subject: Multiboxer IP Update

$(cat $file)
END_MESSAGE
}
mailalert
echo Complete

You can look at this yourself in detail, I'm noting it for reference myself...

You save this file and then have to allow it to execute:

sudo chmod +x /usr/sbin/bootmail.sh

And finally we need to add it to the boot script, so its run as the server comes up...

sudo nano /etc/rc.local

And before "exit" at the bottom add:

/usr/sbin/bootmail.sh

Reboot your server and you'll now get an e-mail of the IP interfaces sent out...

I may later improve the script to add the bug reporters to a known user group, use echo & cat to output the members of that group into a file, and use that list as the "To" field.  But for now, with the happy few of us, this will do.

Now, I would have previously (as you can tell by my older blog entries) used Subversion.  However, I'm managing this project with Git, so I may also now set up the Git on this server, and put a centralised copy of the code onto it, securing it with SSH... Pretty sure there was a good tutorial on setting this up on a server in a recent Linux Format...

No comments:

Post a Comment