LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-05-2017, 05:28 PM   #1
Nitzz
LQ Newbie
 
Registered: Sep 2017
Location: Barcelona
Posts: 3

Rep: Reputation: Disabled
Question 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

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
 
Old 09-05-2017, 06:01 PM   #2
ferrari
Senior Member
 
Registered: Sep 2003
Location: Auckland, NZ
Distribution: openSUSE Leap
Posts: 4,763

Rep: Reputation: 895Reputation: 895Reputation: 895Reputation: 895Reputation: 895Reputation: 895Reputation: 895
A complete solution is documented here...
http://www.howtovmlinux.com/articles...asberrypi.html
 
1 members found this post helpful.
Old 09-05-2017, 06:50 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 20,423

Rep: Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556
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
 
1 members found this post helpful.
Old 09-06-2017, 03:00 PM   #4
andros705
LQ Newbie
 
Registered: Feb 2016
Posts: 11

Rep: Reputation: Disabled
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

Last edited by andros705; 09-06-2017 at 03:03 PM. Reason: Fix content
 
1 members found this post helpful.
Old 09-15-2017, 03:18 AM   #5
Nitzz
LQ Newbie
 
Registered: Sep 2017
Location: Barcelona
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
 
Old 09-30-2017, 03:20 PM   #6
Nitzz
LQ Newbie
 
Registered: Sep 2017
Location: Barcelona
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thumbs up

I am very sorry for your response .
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sending email still send to email to control account although it deleted from useres ahmedsa Linux - Server 3 04-10-2015 04:22 AM
[SOLVED] Change email address on my public key? jdkaye Linux - Software 5 10-06-2013 12:04 AM
Command based email client to send email through secure smtp havolinec Linux - Newbie 2 07-27-2010 07:40 AM
Perl email::send.. how to send the email? hawk__0 Programming 6 12-24-2009 01:53 PM
Looking for Public IP address monitor that can send email alerts 5circles Linux - Software 2 07-11-2009 08:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration