-   Linux - Newbie (
-   -   Send email when my dinamic public IP change (

Nitzz 09-05-2017 05:28 PM

Send email when my dinamic public IP change
Hi to all I'm a newbie in Linux (Raspbian) with my raspberry pi 2.

I've been looking for a long time and I have not found it anywhere.Somebody can help me please?

I have a DYNAMIC public IP and I want connect to my raspberry pi remotely.The big problem is that sometimes my DYNAMIC IP changes :confused:

Someone can tell me the command line to put in the crontab without or other web pages that tells you the new IP changed?

I know that the package MSMTP preconfigured can send this change but I didn't know which command line I have to put in crontab.

I need a wonderful help please? thanks in advance

ferrari 09-05-2017 06:01 PM

A complete solution is documented here...

michaelk 09-05-2017 06:50 PM

Welcome to LinuxQuestions.

There are several free dynamic DNS sites like where you can sign up and get a URL. A client runs in the background that automatically updates your IP address. With noip the only caveat for a free account is that you have to confirm your still using the URL every 30 days.

One way to find your internet address without using a web page is the dig command and the opendns servers.

dig +short

andros705 09-06-2017 03:00 PM

And this is mine solution :P
This will work if you have systemd (Raspbian Jessie do).

Create folder: /opt/ipchange
Create file: /opt/ipchange/
Put this into this file and edit the FIELDS according to your needs.

IP=$(dig +short

# You can edit TITLE and MESSAGE and, you have to edit EMAIL
TITLE="IP Change"
MESSAGE="The new ip is: $IP"

if [[ $IP != $(cat current.ip) ]]; then
  echo $IP > current.ip
  echo "$MESSAGE" | mail -s "$TITLE" "$EMAIL"

Then create a new file: /etc/systemd/system/ipchange.timer
With the following content:

Description=Check the public ip every 15 minutes and send the email if it change.



Then create a new file: /etc/systemd/system/ipchange.service
With the following content:

Description=Check the public ip and send the email if it change.

ExecStart=/bin/bash /opt/ipchange/

Then enable and start the timer:

systemctl enable ipchange.timer
systemctl start ipchange.timer

Nitzz 09-15-2017 03:18 AM

Thank you for your anserws Ferrari - Michaelk - Andros705.I was "fighting" with the solution of Ferrari but I have no exit.First I'll try with MSMTP and then with SSMTP like the example.

I'll try until it comes out, I promise.

Sorry for taking so long to answer and BE THE LINUX BE WITH YOU.

You are the best. Thanks ;)

Nitzz 09-30-2017 03:20 PM

I am very sorry for your response :jawa: .
I have also found a solution in which the terminal sends me an e.mail and thanks to this (show the original) I can know my IP with only one .py file, granting permissions and putting it in to the crontab
I create the file in /home/pi/ --------> Code of the file:

import smtplib, getpass, os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

print("**** Send email with Gmail ****")
user = ('')
password = ('the password of')

From = ('')
password = ('password of your')
To = ('')
Subject = ('Subject you want)
message = ('message you want')

gmail = smtplib.SMTP('', 587)


gmail.login(user, password)


header = MIMEMultipart()
header ['Subject'] = Subject
header ['From'] = From
header['To'] = To

mensaje = MIMEText(mensaje, 'html') #Content-type:text/html

gmail.sendmail(From, to, header.as_string())


I hope it is useful to someone, it has served me.

I wanted to share it with you for helping me.

Thanks again for your answers, I'll try too. SHARING IS CARING. :hattip:

All times are GMT -5. The time now is 10:00 PM.