Email from BASH script – OPEN HARDWARE / SOFTWARE / MIND
Menú Close

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

 

Related Posts

Deja una respuesta

Tu dirección de correo electrónico no será publicada.

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.