Email from BASH script

I did looking for a solution to inform to me when a distributed router is reseted/rebooted, and add to it some information, like public IP, GEO position and hostname of the router.

And I found this solution, a script into the boot order that send to me a email with this information.

root@rasp3:/home/pi# cat sendMail.sh
#!/bin/bash
myPublicIP=$(wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
echo $myPublicIP
myGPSposition=$(geoiplookup $myPublicIP)
echo $myGPSposition
quienSoy=$(whoami)
echo $quienSoy
localHost=$(hostname)
echo $localHost
dt=`date '+%d/%m/%Y_%H:%M:%S'`
echo $dt
run=$(mail -s "Server $localHost rebooted" iphone@walii.es <<< "El router $localHost se ha conectado desde:\n IP: $myPublicIP \n Posicion GPS: $myGPSposition \n Fecha $dt")

 

and the next code into the init folder.

/etc/init.d/sendMailRun

case "$1" in
start)
echo -n "SEND MAIL"
/home/pi/SCRIPTS/sendMail.sh & >/dev/null
echo "."
;;
stop)
echo -n "SEND MAIL NO NEEDED"
echo "."
;;
restart)
echo -n "SEND MAIL AGAIN"
/home/pi/SCRIPTS/sendMail.sh & >/dev/null
echo "."
;;
*)
echo "Usage: /etc/init.d/radius {start|stop|re-send}"
exit 1
esac

exit 0

 

Leave a Reply

Facebook Like Add
Google PLUS ONE
De donde me visitan
Publicidad
Calendario de Posts
May 2018
M T W T F S S
« Nov    
 123456
78910111213
14151617181920
21222324252627
28293031