server script that sends an email every time the global ip changes

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.

5 Answers

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.

2

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" < $NOWIPADDR fi
else $GETIPADDR > $NOWIPADDR mail -s "Server IP" < $NOWIPADDR
fi
0

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" fi else curl $GETIPADDR >> $NOWIPADDR fi
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.

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 -m $myip 

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:

Hopefully it helps!

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like