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 rebootscript.sh
#!/bin/bash
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" YourEmail@YourServer.com < /tmp/rrmessage.txt


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

Now gedit /etc/rc.local and add the /path/to/scripts/rebootscript.sh 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

TESTcentos64-bit

eth0      Link encap:Ethernet  HWaddr 90:24:55:5A:2A:4D  
  inet addr:192.168.1.101  Bcast:192.168.1.255  Mask:255.255.255.0
  UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
  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.

Enjoy,

No comments: