6

I'm setting up a web server that won't have a static global IP. It will be at my parents house and I won't be able to SSH into it when the global IP changes. The global IP will change every time router is restarted which happens like once a month.

I need a script that sends me an email every time the global IP changes. So I can update the DNS so my website will work again.

I don't know anything about scripts in Ubuntu, but I know the basics in Java and PHP.

Computer: Ubuntu 13.10 server, iMac G4 Globe.

7

Have you considered using dynamic DNS update script/tool?

e.g. ddclient available as Debian/Ubuntu package.

It can update your DNS zone OR update your dynamic DNS record (so you can ssh in) and execute custom script to send you email.

  • that looks promising, will give it a try and see if it solves my problems. – Alvar Nov 10 '13 at 12:50
  • It should solve your problem as you stated it. There may be better fit solutions. – AnFi Nov 11 '13 at 12:29
4

Based on the code from this answer I have modified and added a few more options for debugging.

I had to set up the email server and use crontab -e to get this to email me my current ip address. You will also need to make the script file executable and make sure your user has read and write permission to the folder you put the scripts in.

#!/bin/bash
NOWIPADDR="/home/scripts/nowipaddr"
GETIPADDR="dig +short myip.opendns.com @resolver1.opendns.com"
LOG="/home/scripts/ip.log"
timestamp=$( date +%T )
curDate=$( date +"%m-%d-%y" )

if [ -f $NOWIPADDR ]; then
  if [[ `$GETIPADDR` = $(< $NOWIPADDR) ]]; then
    echo $curDate $timestamp " IP address check: " $(< $NOWIPADDR) >> $LOG
  else
    $GETIPADDR > $NOWIPADDR
    mail -s "Server IP" email@gmail.com < $NOWIPADDR
  fi
else
  $GETIPADDR > $NOWIPADDR
  mail -s "Server IP" email@gmail.com < $NOWIPADDR
fi
3

You can write a simple script to run from the cron and daily at particular time.

Take the existing ip in a file and then run your if loop to check the new ip with the existing ip and if both the ip's remain same, it can skip sending mail. Otherwise, if there is a change in IP it should send mail with the new ip.

you can run the script hourly, daily, weekly.

NOWIPADDR="nowipaddr"
GETIPADDR="ifconfig.me"


    if [ -f $NOWIPADDR ]
    then
       if [ `cat $NOWIPADDR` = `curl $GETIPADDR` ]
       then
           echo "no change in IP."
       else
           curl $GETIPADDR > $NOWIPADDR
           mail -s "IP is $GETIPADDR" yourmailadd@gmail.com
        fi
    else
        curl $GETIPADDR >> $NOWIPADDR
    fi
  • yes something like that is what I want to do. But I don't know how to write scripts can you? – Alvar Nov 10 '13 at 11:57
  • @OmPS Could you please add a script simple like this, replacing mail with sendemail? I think with sendemail I won't need an email server in the machine, correct? – vasilis74 Jan 27 '18 at 0:37
2

I am using this to check my ip address once a day.

wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' > ~/myip

If you have a linux computer with static ip address somewhere I would say just rcp the file ~/myip over there.

If that is not the case you can always use a handy command line tool called sendEmail (not to be confused with sendmail). This tool makes it very easy to send an email with a one line command. http://caspian.dotconf.net/menu/Software/SendEmail/

You could write something along these lines:

myip=`wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`<br>
sendEmail -f you@yourmail.com -m $myip 
0

A bit of an old thread but it comes up pretty high on the list when you Google search for how to monitor for external IP changes.

As an alternative option that might help you, I've written a python script designed to monitor for external IP address changes. It currently saves the "current" external IP address to a file and, when run, grabs a new IP address and then checks it against the old one. It is design to email you if a change is detected.

It's designed to run as a cronjob and does not run as a background daemon on its own.

Here's the link to the github repository: https://github.com/begleysm/ipwatch

Hopefully it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.