LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Send email when my dinamic public IP change (https://www.linuxquestions.org/questions/linux-newbie-8/send-email-when-my-dinamic-public-ip-change-4175613297/)

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 ifconfig.me 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...
http://www.howtovmlinux.com/articles...asberrypi.html

michaelk 09-05-2017 06:50 PM

Welcome to LinuxQuestions.

There are several free dynamic DNS sites like noip.com 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 myip.opendns.com @resolver1.opendns.com

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/check.sh
Put this into this file and edit the FIELDS according to your needs.
Code:

IP=$(dig +short myip.opendns.com @resolver1.opendns.com)

# You can edit TITLE and MESSAGE and, you have to edit EMAIL
EMAIL="example@mail.com"
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"
fi

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

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

[Timer]
OnCalendar=*:0/15
Persistent=true

[Install]
WantedBy=timers.target

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

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

[Service]
WorkingDirectory=/opt/ipchange/
ExecStart=/bin/bash /opt/ipchange/check.sh

Then enable and start the timer:
Code:

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 email.py 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 = ('email@gmail.com')
password = ('the password of email@gmail.com')

From = ('email@gmail.com')
password = ('password of your email@gmail.com')
To = ('anotheremail@gmail.com')
Subject = ('Subject you want)
message = ('message you want')

gmail = smtplib.SMTP('smtp.gmail.com', 587)

gmail.starttls()

gmail.login(user, password)

gmail.set_debuglevel(1)

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

mensaje = MIMEText(mensaje, 'html') #Content-type:text/html
header.attach(Subject)

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

gmail.quit()




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.