• Home
  • Help
  • Search
  • Login
  • Register
Pages: [1]
Author Topic: Script for Emailing change in WAN IP Address using Cron Job and sSMTP...  (Read 8997 times)
badcam
Jr. Member
**

Karma: 1
Posts: 78


View Profile
« on: December 11, 2009, 07:40:07 PM »

Here is a little script that queries http://checkip.dyndns.org/ and compares it to my Routers WAN IP address. If there's no change, the script exits. If the IP address doesn't match the result returned from DynDns, an email is sent to a Gmail address using sSMTP mail server.

I used this tutorial http://www.nixtutor.com/linux/send-mail-with-gmail-and-ssmtp/ to set up sSMTP. I highly recommend this site.

This is my ssmtp.conf file. It's slightly different to the tutorial. Suggestions for improvement would be gratefully accepted:

Code:
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=mail

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=sheeva

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

root=myemailaddresshere@gmail.com
mailhub=smtp.gmail.com:587
rewriteDomain=gmail.com
AuthUser=myemailaddresshere # (without@gmail.com)
AuthPass=*************

Three files will be created in the root of my user directory (in this case badcam):

iplog.txt          -   This file is blank initially. The script will use this for storing the current WAN IP address which is compared against the DynDns.org websites CheckIP.

whereami.txt    -  This file is used to store the data which will be sent as an email

whereami.sh     -  This is my Bash script, which is as follows:


Code:
#!/bin/bash
# A script for sending myself an email containing my WAN IP Address
get_ip=`wget -q -O - http://checkip.dyndns.org/ | grep -Eo '[0-9\.]+'`
if [ "$get_ip" = "$(cat ./iplog.txt)" ]; then
echo "No need, I'm out of here..."
exit;
else
echo "The IP has changed! Sending an email..."
echo "SUBJECT: My External WAN IP Address has changed" > ./whereami.txt
echo "FROM: Badcam's Island in the Net" >> ./whereami.txt
date >> ./whereami.txt
echo " " >> ./whereami.txt
echo "$get_ip" >> ./whereami.txt
echo "$get_ip" > ./iplog.txt
/usr/sbin/ssmtp -oi myemailaddress@gmail.com < ./whereami.txt
fi

Then I used this tutorial  http://www.nixtutor.com/linux/sending-email-alerts-through-cron/   to set up a Cron Job to automatically run every 15 minutes.

My Crontab -e file looks like this, where the */15 means run every fifteen minutes:

Code:
# m h  dom mon dow   command
MAILTO="myemailaddresshere@gmail.com
*/5 * * * * /home/badcam/whereami.sh
~

If anyone can find something I can improve here, or make clearer, please let me know.
Logged

cbxbiker61
Global Moderator
Sr. Member
*****

Karma: 38
Posts: 497


View Profile
« Reply #1 on: December 11, 2009, 09:31:49 PM »

I'm curious why you would automatically "check" for a change in IP address without automatically "changing" the ip address association at dnydns.org.

Polling dnydns.org every 15 minutes seems like overkill.

I've got a script on a router that checks it's current ip address once an hour (it saves it's previous ip address in /tmp).  If the address changed in that hour it runs a section of code that update freedns.  It doesn't poll freedns at all.  Freedns only get's hit when there is an actual update to handle.
Logged

badcam
Jr. Member
**

Karma: 1
Posts: 78


View Profile
« Reply #2 on: December 11, 2009, 10:12:32 PM »

I'm curious why you would automatically "check" for a change in IP address without automatically "changing" the ip address association at dnydns.org
I'm not quite sure what you mean here.  I assume what your saying here is related to your script you mention next. Could you please clarify?

Quote
Polling dnydns.org every 15 minutes seems like overkill.

15 minutes is set because I can't wait 1 hour before I get the first email.  It's easy to change. For the situation I'm using this particular plug in, 15 minutes is a good time to wait.

Quote
I've got a script on a router that checks it's current ip address once an hour (it saves it's previous ip address in /tmp).  If the address changed in that hour it runs a section of code that update freedns.  It doesn't poll freedns at all.  Freedns only get's hit when there is an actual update to handle.

Well, that sounds mighty handy. Would you mind posting that here? I'd be happy to try it out.

Please bear in mind. I know nothing about these sorts of things. I'm just a babe in the woods when it comes to this sort of stuff.  Tongue
Logged

cbxbiker61
Global Moderator
Sr. Member
*****

Karma: 38
Posts: 497


View Profile
« Reply #3 on: December 11, 2009, 10:32:19 PM »

The following is a script located as /etc/cron.hourly/CheckIpAddress.sh.  You could put it anywhere as long as your cron runs it periodically.  In my case I'm monitoring the external ip address which comes up as eth0.1 on the router.  You'd almost for sure need to change that to a different eth adapter.  UpdateIpAddress.sh is a script downloaded from freedns that magically updates freedns with the ip address.  Dyndns would require something similar but different.

Code:
#! /bin/sh

eip=/tmp/ExternalIp

ifconfig eth0.1 | grep 'inet addr:' | sed -e 's#.*inet addr:\([^ ]*\).*#\1#' > ${eip}.new

if [ ! -f ${eip}.prev ]; then
        /etc/init.d/UpdateIpAddress.sh
else
        if ! diff ${eip}.prev ${eip}.new 2>&1 >/dev/null; then
                /etc/init.d/UpdateIpAddress.sh
        fi
fi

mv ${eip}.new ${eip}.prev
Logged

badcam
Jr. Member
**

Karma: 1
Posts: 78


View Profile
« Reply #4 on: December 11, 2009, 10:41:37 PM »

Great. Thanks.

I'll check out freedns while I'm about it.  Cheesy
Logged

badcam
Jr. Member
**

Karma: 1
Posts: 78


View Profile
« Reply #5 on: December 11, 2009, 10:53:33 PM »

cbxbiker61:

Sorry, but which particular freedns site is it that you use? There seem to be quite a few.
Logged

cbxbiker61
Global Moderator
Sr. Member
*****

Karma: 38
Posts: 497


View Profile
« Reply #6 on: December 11, 2009, 11:06:16 PM »

http://freedns.afraid.org/

There update script (which they supply as a .bat file) just needs to be changed into a script file.  All it does is a wget with a magic string to update the ip address.  Mine looks something like this.

wget -q \
http://freedns.afraid.org/dynamic/update.php?ThisIsAMagicKeyForYourUpdate
Logged

badcam
Jr. Member
**

Karma: 1
Posts: 78


View Profile
« Reply #7 on: December 12, 2009, 01:58:19 AM »

I'm set up my freedns details and am waiting for the domain name to be authorised. So, all good so far.

How do I set up this magic string please? I can't find anything on their site that mentions this.
Logged

cbxbiker61
Global Moderator
Sr. Member
*****

Karma: 38
Posts: 497


View Profile
« Reply #8 on: December 12, 2009, 02:08:30 AM »

When you "manage" your domain name it will have an option such as download automated script next to the domain name.
Logged

birdman
Sr. Member
****

Karma: 4
Posts: 443


View Profile WWW
« Reply #9 on: December 12, 2009, 05:04:11 AM »

Here is a little script that queries http://checkip.dyndns.org/ and compares it to my Routers WAN IP address. If there's no change, the script exits. If the IP address doesn't match the result returned from DynDns, an email is sent to a Gmail address using sSMTP mail server.
Why do you need to know?  Why not just use the name? The routers I have had handle updating the dynamic DNS server themselves.  Doesn't yours?
Logged

j0n3s
Newbie
*

Karma: 2
Posts: 13


View Profile
« Reply #10 on: December 12, 2009, 06:50:01 AM »

consider using either inadyn or ddclient package - you have option to choose DNS provider, refresh time, etc.
Logged

badcam
Jr. Member
**

Karma: 1
Posts: 78


View Profile
« Reply #11 on: December 12, 2009, 12:19:14 PM »

Here is a little script that queries http://checkip.dyndns.org/ and compares it to my Routers WAN IP address. If there's no change, the script exits. If the IP address doesn't match the result returned from DynDns, an email is sent to a Gmail address using sSMTP mail server.
Why do you need to know?  Why not just use the name? The routers I have had handle updating the dynamic DNS server themselves.  Doesn't yours?

The Sheeva will be sent to someone else. They have a cable modem and no idea how to access it and I don't either. We're in different countries. They're quite PC illiterate, so the easiest solution for us is to send it and get them to just plug it in and connect it to their network.

cbxbiker61's solution is a much more elegant method than mine, so I'll adopt that. My way meant that I would have to check my gmail. I never knew until cbxbiker61 suggested it, that you could auto update a dns site.
Logged

badcam
Jr. Member
**

Karma: 1
Posts: 78


View Profile
« Reply #12 on: December 13, 2009, 03:03:46 AM »

cbxbiker61:

Hi.

I have set up the two scripts, I have my magic key. The only thing I've changed in the CheckIpaddress.sh is the Eth0.1 to Eth0.

When I run the scripts and check the /tmp/ExternalIp.prev file, what I see is my Internal IP and not the External WAN IP address.

Now, I can just throw in my
Code:
get_ip=`wget -q -O - http://checkip.dyndns.org/ | grep -Eo '[0-9\.]+'`
command, but perhaps there's something I've missed.

Do you have any suggestions as to why I'm not getting the WAN IP?

Thanks.
Logged

Pages: [1]
Print
Jump to: