painbrain
Posts: 6
Joined: Fri Jun 29, 2012 11:21 pm

[SCRIPT] change ip and temperature alarm

Sat Nov 03, 2012 12:30 pm

hi everyone,

i want ti share with you my script. it checks every 10 minutes if the public IP changes and if the temperature is over a certain value. if it founds something it send you an email.

start with install crontab

Code: Select all

sudo apt-get install crontab
maybe present for default

Code: Select all

sudo apt-get install sendemail
sending email by terminal

create a file

Code: Select all

nano inforaspy
and paste this

Code: Select all

#!/bin/bash

############ Parameters ############
user="pi"
tempmax="55"
mittente=""
dest=""
smtp=""
username=""
pass=""
############ End Parameters ############

ip_old="`tail /home/$user/.inforaspy`"
ip_now="`wget -qO- http://checkip.dyndns.org/ | sed "s/.* \([[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\).*/\1/g"`"
temp="`cat /sys/class/thermal/thermal_zone0/temp | cut -c1-2`"
echo $ip_now > /home/$user/.inforaspy
if [ "$ip_old" != "$ip_now" ]; then
 titoloip="Public IP changed!"
 ipresult="New IP is $ip_now"
 sendEmail -f $mittente -t $dest -u $titoloip -s $smtp -xu $username -xp $pass -m $ipresult
fi
if [[ "$temp" > "$tempmax" ]]; then
 titolotemp="Raspberry alarm!"
 tempresult="Alarm! Raspberry temperature is $temp."
 sendEmail -f $mittente -t $dest -u $titolotemp -s $smtp -xu $username -xp $pass -m $tempresult
fi
exit
here is parameters you have to set:
user="pi" --> your raspbian user, default is "pi"
tempmax="55" --> your temperature value for raspberry safety
mittente="" --> email sender
dest="" --> email destination
smtp="" --> your email sender SMTP server
username="" --> username mail sender
pass="" --> password mail sender

give the execute right

Code: Select all

chmod +x inforaspy
and copy in a system folder

Code: Select all

sudo mv inforaspy /usr/bin/
now program crontab to execute every 10 minute

Code: Select all

sudo crontab -e
it opens a text editore, paste this at the end

Code: Select all

*/10 * * * * inforaspy
then presso control+O to save and control+X to exit the editore
number 10 you see is for 10 minutes. you can change.


that's it!

i hope it will be usefull for someone,
bye!

hitman80
Posts: 18
Joined: Sun Nov 04, 2012 5:55 pm

Re: [SCRIPT] change ip and temperature alarm

Sun Nov 04, 2012 6:29 pm

thanks. it works very well.

Only one correction .

Are you sure that

Code: Select all

if [[ "$temp" > "$tempmax" ]]; then
is right ?

I writed this :

Code: Select all

if [ "$temp" -ge "$tempmax" ]; then
and it's ok...

Do you have a suggest to use a GMAIL account for send email with SENDEMAIL ?

painbrain
Posts: 6
Joined: Fri Jun 29, 2012 11:21 pm

Re: [SCRIPT] change ip and temperature alarm

Mon Nov 05, 2012 9:34 pm

Thanks for correction.
Tomorrow i will work about gmail.
Stay tuned :)

painbrain
Posts: 6
Joined: Fri Jun 29, 2012 11:21 pm

Re: [SCRIPT] change ip and temperature alarm

Tue Nov 06, 2012 2:03 pm

sorry but can i modify my 3d?
i want to change code with hitman80's correction but i don't see "modify" button! :shock:

TheDanger
Posts: 1
Joined: Wed Nov 07, 2012 9:24 am

Re: [SCRIPT] change ip and temperature alarm

Wed Nov 07, 2012 9:25 am

Sweet!

Worked a treat. Thank you!

GJE
Posts: 4
Joined: Sat Jun 09, 2012 1:18 pm

Re: [SCRIPT] change ip and temperature alarm

Sun Nov 11, 2012 4:53 pm

Love the script and now I finally got sendemail to work with Gmail. Took a lot of Googling and I'm not sure it is the *right* answer but I modified line 1907 in /usr/bin/sendemail (as root).

The diff output between the old and new is this:

1907c1907
< if (! IO::Socket::SSL->start_SSL($SERVER, SSL_version => 'SSLv3')) {
---
> if (! IO::Socket::SSL->start_SSL($SERVER, SSL_version => 'SSLv3 TLSv1')) {

Basically remove the TLSv1 bit and now it works.
If somebody would like to post the correct way of doing it please do.

hitman80
Posts: 18
Joined: Sun Nov 04, 2012 5:55 pm

Re: [SCRIPT] change ip and temperature alarm

Mon Nov 12, 2012 11:21 am

GJE wrote:Love the script and now I finally got sendemail to work with Gmail. Took a lot of Googling and I'm not sure it is the *right* answer but I modified line 1907 in /usr/bin/sendemail (as root).

The diff output between the old and new is this:

1907c1907
< if (! IO::Socket::SSL->start_SSL($SERVER, SSL_version => 'SSLv3')) {
---
> if (! IO::Socket::SSL->start_SSL($SERVER, SSL_version => 'SSLv3 TLSv1')) {

Basically remove the TLSv1 bit and now it works.
If somebody would like to post the correct way of doing it please do.
Thanks ... it works.... without TLSv1 ... it runs.... :D :)

I set sender and receiver with the same account GMAIL ... and it's ok

Code: Select all

############ Parameters ############
user="pi"
tempmax="55"
mittente="[email protected]"
dest="[email protected]"
smtp="smtp.gmail.com:587"
username="XXXXXXX"
pass="YYYYYYYY"

hitman80
Posts: 18
Joined: Sun Nov 04, 2012 5:55 pm

Re: [SCRIPT] change ip and temperature alarm

Sun Nov 18, 2012 7:30 pm

Needs also these packages for a gmail account :

Code: Select all

sudo apt-get install libio-socket-ssl-perl libnet-ssleay-perl perl

DrakkenLemmont
Posts: 2
Joined: Fri Jan 24, 2014 5:48 pm

Re: [SCRIPT] change ip and temperature alarm

Fri Jan 24, 2014 5:53 pm

I'm having some trouble with this, new to Raspberry Pi and Raspbian linux. I run the script and get this:

./inforaspy: line 23: $'\302\240titolotemp=Raspberry alarm!': command not found
./inforaspy: line 24: $'\302\240tempresult=Alarm! Raspberry temperature is 49.': command not found
./inforaspy: line 25: $'\302\240sendemail': command not found

I copied the script text, and just put my info in, picked up the perl installs, tried all the above so far, but unfortunately am stuck with not getting that email notification.

User avatar
dentex
Posts: 180
Joined: Sun May 20, 2012 7:58 am
Location: Italy
Contact: Website

Re: [SCRIPT] change ip and temperature alarm

Wed Oct 08, 2014 1:38 pm

hitman80 wrote:
GJE wrote:Love the script and now I finally got sendemail to work with Gmail. Took a lot of Googling and I'm not sure it is the *right* answer but I modified line 1907 in /usr/bin/sendemail (as root).

The diff output between the old and new is this:

1907c1907
< if (! IO::Socket::SSL->start_SSL($SERVER, SSL_version => 'SSLv3')) {
---
> if (! IO::Socket::SSL->start_SSL($SERVER, SSL_version => 'SSLv3 TLSv1')) {

Basically remove the TLSv1 bit and now it works.
If somebody would like to post the correct way of doing it please do.
Thanks ... it works.... without TLSv1 ... it runs.... :D :)

I set sender and receiver with the same account GMAIL ... and it's ok
In addition to this, I had to enable, from my google account preferences, access from "less secure apps"; in fact, on the 1st try, I received a google system email saying they stopped a message due to security restriction. They also provided the link to unblock this behaviour.
One of my projects with the Raspberry Pi:
--------------------------------------------------------
(Raspberry) Pi Aquarium Controller: https://github.com/dentex/piac

Return to “Networking and servers”