Wednesday, September 29, 2010

Auto Emails from Linux

I recently needed to find out the IP address of a test machince on the network.
The machince was set up DCHP and every time it rebooted I needed to know what the IP was, so I wrote this little script.
What this script does is send me an email with the IP address and some other information I found useful.

A coulple of things you need, Mutt installed and fuctioning and access to the /etc/rc.local file.

Copy this script into a file  called
echo "Reboot from Test Machine" > /tmp/rrmessage.txt 
echo -e >> /tmp/rrmessage.txt 
hostname >> /tmp/rrmessage.txt 
echo -e >> /tmp/rrmessage.txt 
/sbin/ifconfig eth0 >> /tmp/rrmessage.txt 
uname -a >> /tmp/rrmessage.txt 
echo -e >> /tmp/rrmessage.txt 
cat /etc/issue >> /tmp/rrmessage.txt 
mutt -s "Reboot Report" < /tmp/rrmessage.txt

Then chmod +x
Now move the to where ever you keep your startup scripts.

Now gedit /etc/rc.local and add the /path/to/scripts/ to the end of the file.

Every time the system reboots you will get a nice little email like this.

Subject: Reboot Report
Reboot from Test Machine


eth0      Link encap:Ethernet  HWaddr 90:24:55:5A:2A:4D  
  inet addr:  Bcast:  Mask:
  RX packets:32 errors:0 dropped:0 overruns:0 frame:0
  TX packets:35 errors:0 dropped:0 overruns:0 carrier:0
  collisions:0 txqueuelen:1000 
  RX bytes:4311 (4.2 KiB)  TX bytes:3250 (3.1 KiB)
  Interrupt:169 Memory:fe8f0000-fe900000 

Linux TESTcentos64-bit 2.6.18-194.8.1.el5 #1 SMP Thu Jul 1 19:04:48 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux

CentOS release 5.5 (Final)

This will also let you know when the server has had an unplanned reboot.


No comments: