================
Public IP Change
================
Windows Version: Here (Or use CYGWIN with this linux script – not tested)
For: Any Linux (Tested on Debian 6)
 
=== Some random info ===
 
Lots of people alot of money to get static IP address. There are alot of benefits to having the same Static IP. If you have your IP tied to a Domain, and it changes, even if you update it, it could take up to 24 (or 48 hours) to update through out the DNS servers. This could be very hazardous to business etc. The best solution is definatly having a none changing ip. Some of us arent that lucky though. So the solution is to have your system update you when the public IP changes.
 
I have two methods that keep updating me. The second way is optional. 
 
The first way checks if the IP changes and emails me (I have it email to my phone via sms text message and my email)
 
The second optional way is that I just have my system update me of my IP 3 times everyday via email (again to my phone via sms text message and my email)
 
I use the sms gateway email of my phone service provider to send an email, it then forwards a text message to my phone. I will provide a list of the phone service providers different sms & mms gateways later on.
 
=== Tools I use ===
 
For the operations of the code I just use BASH script. For scheduling I use cron jobs. For my system to email me, I use postfix.
To get current IP:
# METHOD 1 - using wget
wget www.icanhazip.com
cat index.html
# delete the extra file that was made
rm index.html

# METHOD 2 - using curl
curl www.icanhazip.com

# METHOD 2a -  in a script we would set a variable to this
IP=`curl www.icanhazip.com 2> /dev/null`
echo $IP

# METHOD 2b - in a script we could also save to a file
curl www.icanhazip.com 2> /dev/null > /var/log/ip_data.txt
cat /var/log/ip_data.txt

 

 
=== INSTRUCTIONS ====
 
=== Step 1 : Postfix – System Emails ===
 
apt-get install postfix
 
When asked for what kind of email server select “Internet Site”
Leave everything at default
 
Test the email like so
First type “# mail destination@email.com”
Then in the subject put “TEST” and hit enter
Then type the email body “This is a test message” and hit enter and type a dot/period “.”. Te period is a EOT character. EOT meaning End of Transmitions notifies the system that the email is complete.
 
# mail user1@gmail.com
subject: TEST <enter>
This is a test message <enter>
. <enter>

Another way to email

# METHOD1 - none interactive use of mail command (good for scripting) - includes subject line and body text. 2 lines of code and saves body of email to file.
echo "This is the body of the email" > body.txt
mail -s "Todays News" user1@gmail.com < body.txt

# METHOD2 - none interactive use of mail command (good for scripting) - includes subject line and body text. One line, body is not saved to file (saving disk space)
echo "This is the body of the email" | mail -s "Todays News" user1@gmail.com
 
=== Step 2 : Make the apps location ===
 
First log in as root
And go to roots home directory 
cd ~
NOTE: in my examples this is /root/
 
Make the ipchange folder where the apps will go and inside it the log folder where the logs will go 
mkdir -p ipchange/logs
NOTE: in my examples this is /root/ipchange and /root/ipchange/logs
cd ipchange
=== Step 3a : Write the 2 scripts ===
I use vim (when inside it hit “i” to start typing, and then “esc” to stop typing, once out of the typing mode, aka insert mode, type “:wq!” to save and exit”). Then I verify the scripts with cat command.
 
If you dont have vim type apt-get install vim 
 
Make the file and give it the proper execution attributes. 
# create the scripts that will run and email when ip changed
touch ipchange.sh
chmod +x ipchange.sh

# create the scripts that will email current wan ip (even when its not changed)
touch iplog.sh
chmod +x iplog.sh
 
Now that the scripts are made and can be execute, its time to write the instructions into them.
=== Step 3b : Write the ipchange.sh script ===
 
This script will send the new ip by email if it has changed. It gets the IP and saves it to a file, next time it runs it checks to see if that file has the same or different value. If the value is different, then it must be a new IP so it emails it. 
vim ipchange.sh
Type “i” to enter insert mode
Type the following (in between the tidle break lines, obviously dont include the tidle break lines):
Note: change the EMAIL1, EMAIL2, and EMAIL3 to 3 different emails. I have EMAIL3 send it to my phone where 5551234321 is my phone number and tmomail.net is the text gateway of my phoneprovider (I have a list of the phone gateways at the bottom)
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
#!/bin/bash
# What this script does: this one mails you the ip if it changes
# change above to !/bin/sh if you dont have /bin/bash and have sh instead

#################################
# **** set these variables **** #
#################################

# Which emails will receive the update/alert. Can have one or many. If many space delimited.
EMAIL_DEST="user1@email.com user2@email.com 5551234321@tmomail.net"

# SUBJECT_COMMENT can be set to empty or to any text, it will appear in the subject line so its nice to put the hostname, or perhaps some keyword/phrase that will let you know what server the email came from
# Pick any of these SUBJECT_COMMENTs or create your own. Or leave the current one which is an empty SUBJECT_COMMENT
SUBJECT_COMMENT=""
### SUBJECT_COMMENT="My Awesome Server"
### SUBJECT_COMMENT="`hostname` - My Awesome Server"

# OLD_IP_FILE pick a file which will store the IP in a safe place. Dont put this in /tmp as that location is cleared upon reboot. It best to keep this in /root, /etc, or /var/log (permanent storage)
OLD_IP_FILE="/var/log/old_ip.txt"

#######################
# **** main code **** #
#######################

# get current ip
IP=`curl icanhazip.com 2> /dev/null`
# get last ip that we saved on the last run of this (see very bottom of script)
OLD_IP=`cat "${OLD_IP_FILE}"`

# check if old ip and new ip are the same. if they are the same dont do anything. if they arent email every email in email_dest
if [ "${IP}" = "${OLD_IP}" ]; then
    MESSAGE="[`date`] ipchange script - IP is the same: ${IP} so not emailing"
    echo "${MESSAGE}"
    logger "${MESSAGE}"
else
    # ANOTHER WAY TO EMAIL: (echo "From: home@homesystem.com"; echo "To: myemail@domain.com"; echo "Subject: IP Address Update"; echo; echo "Computer's IP Changed. The new IP is:"; cat index.html) | sendmail -f home@homesystem.com myemail@domain.com
    MESSAGE="[`date`] ipchange script - The new IP is: ${IP}"
    echo "${MESSAGE}"
    logger "${MESSAGE}"
    for CURRENT_EMAIL in ${EMAIL_DEST}; do
       echo "Emailing ${CURRENT_EMAIL}";
       echo "${MESSAGE}" | mail -s "IP CHANGED: ${SUBJECT_COMMENT}" "${CURRENT_EMAIL}";
    done
fi
# save current ip as the old ip, as the next time this runs it will be the old ip
echo "${IP}" > "${OLD_IP_FILE}"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Type “Escape” to quit insert mode
Then type “:wq!” to force save anf quit.
 
=== Step 3c : Write the iplog.sh script ===
 
This script emails the current IP to the email.
vim iplog.sh
Type “i” to enter insert mode
Type the following (in between the tidle break lines, obviously dont include the tidle break lines):
 
Note: change the EMAIL1, EMAIL2, and EMAIL3 to 3 different emails. I have EMAIL3 send it to my phone where 5551234321 is my phone number and tmomail.net is the text gateway of my phoneprovider (I have a list of the phone gateways at the bottom)
Note: to find out the email (sms or mms gateway) for your phone/telecom provider check out my list here: http://www.infotinks.com/list-of-sms-mms-gateways-for-phone-companys/
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
#!/bin/bash
# This script will get your WAN / Public IP and email it to you (as definited in EMAIL_DEST)
# change above to !/bin/sh if you dont have !/bin/bash
# change the EMAIL_DEST and FOLDER variables to meet your needs
# this script should be run by the root user
# Set EMAIL_DEST to any email which should receive the alert. any number of emails that will get the alert (space character seperator/delimiter). If you just want one email, then only put one there, like this EMAIL_DEST="one@email.com"
EMAIL_DEST="user1@email.com user2@email.com 8015551234@tmomail.net" 
# The folder /root/ipchange should exist (but if it doesnt it will get created). Copys of the body which is sent will go there
FOLDER=/root/ipchange
# SUBJECT_COMMENT can be set to empty or to any text, it will appear in the subject line so its nice to put the hostname, or perhaps some keyword/phrase that will let you know what server the email came from
# Pick any of these SUBJECT_COMMENTs or create your own. Or leave the current one which is an empty SUBJECT_COMMENT
SUBJECT_COMMENT=""
### SUBJECT_COMMENT="My Awesome Server"
### SUBJECT_COMMENT="`hostname` - My Awesome Server"
# get the WAN IP and save to the variable IP
IP=`curl icanhazip.com 2> /dev/null`
# The FILE variable will be the filename of the file which will hold the body of the email
FILE="`date +D%D-T%T | tr / - | tr : -`"
mkdir -p ${FOLDER}/logs 2> /dev/null
# EMAIL_BODY is the full filename (with the path). The file refered by EMAIL_BODY contains the body text of the email which will be sent to each of the EMAIL_DESTINATIONS
EMAIL_BODY=${FOLDER}/logs/${FILE}
echo "Saving copy of email body to: ${EMAIL_BODY}"
echo ${FILE} ": THE IP IS:" ${IP} | tee ${EMAIL_BODY}
# send the same email to each of the emails listed in EMAIL_DEST
# EMAIL_DEST needs to have either 1 email, or more than one (as long as they are space delimited)
for CURRENT_EMAIL in ${EMAIL_DEST}; do
echo "Emailing ${CURRENT_EMAIL}";
mail -s "IP LOG: ${SUBJECT_COMMENT} ${FILE}" "${CURRENT_EMAIL}" < ${EMAIL_BODY};
done
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Type “Escape” to quit insert mode
Then type “:wq!” to force save anf quit.
 
=== Step 4 : Setup the Cronjob ===
 
In linux cron jobs run scheduled tasks.
 
Every user has cron jobs. Google “cron examples” to learn more. For now we will make a cronjob that runs as root, these run in the background.
 
To see all the current root cron tasks type # crontab -l. Dont be surprised if you have none. There are actually more cronjobs that just that. The other ones usually go to /etc/cron.d
 
To edit crontabs run the command below, it might ask what editor to use, Just use vim or vi, or nano/pico. If you use nano/pico (Save is: Control-O then enter enter, and Quiting is Control-X) 
crontab -e
 
Add the following lines into it(at the bottom) and save and exit:
 
NOTE how you can use # hash symbols in crontabs to comment stuff out
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
# this logs the ip and emails it (twice a day)
# the @midnight is a shortcut for midnight, so that the iplog sends you your IP at midnight of every day
# the 0 12,18 * * * sends updates at noon and 6pm

@midnight /root/ipchange/iplog.sh
0 12,18 * * * /root/ipchange/iplog.sh
 
# this looks for email changes every 5 minutes
# If it were all start * * * * * it would check every minute but */5 makes it skip 5 minutes
 
*/5 * * * * /root/ipchange/ipchange.sh
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
=== THE END ====
 
Final folder structure in my examples is like so
 
/root/ipchange/ipchange.sh
/root/ipchange/iplog.sh
/root/ipchange/logs/<logs will go here>
 
-OPTIONAL STEP-
 
Now all this should just run
 
But just incase reload the cron job (optional, cron automatically check its crontabs so this step is unnecessary) 
# /etc/init.d/cron reload
=== List of other sms gateways ===
 Check out the list of other sms/mms gateways here

2 thoughts on “Script Email/Text Current Public IP & email/SMS or MMS text when Public IP Changes

    1. Im not sure what a “Yellow Dog” is. However if its a phone, then yes as long as its supports incoming sms text (you will just need to find out the email address associated with SMS texting your phone **see below**). If its a linux distro, then yes you can use this method as long as it has internet access, the mail command, and curl command.

      ** For example texting a Tmobile provided cell phone (any model of phone that support SMS text) which has the phone number 8015551234 then the email address is 8015551234@tmomail.net.

Leave a Reply

Your email address will not be published. Required fields are marked *