Shell Script: Report External IP Address Via Email

Posted on

Why would someone want to do this? Well I wanted to do this because my ISP (internet service provider) changes my IP address every 3-6 months for an unknown reason (security?). This makes it hard to host your own webserver. After some searching, I found this script and modified it to my own liking, since it didn’t quite work right when I first got it. I am NOT assuming responsibility if some goofball breaks their computer because they don’t know know what they are doing. Please realize that you will need to install wget and sendmail if you don’t already have them on your Unix system. I will not get into installing these packages in this post. To get this working you will need to set up a cron job to run this script every hour. If the IP hasn’t changed it will not send an email. Here is more information how to set up your cron jobs. Please refer to my last post to send text messages to your cell phone vie email. There is more than enough information on the given websites for the packages. Here is the original script.

Here is my Bash Script which is named ip_change.sh

#!/bin/bash

#########################################################################

  1. ip_change.sh Ver 1.0 #
  2. Script to notify the user when the computer's external IP changes #
  3. Created by Suramya Tomar (suramya@suramya.com)#
  4. Modified by Cory Solovey (csolovey@gmail.com) #
  5. Last updated 28th March 2007 #

#-----------------------------------------------------------------------#

  1. This program is free software; you can redistribute it and/or modify #
  2. it under the terms of the GNU General Public License as published by #
  3. the Free Software Foundation; either version 2 of the License, or #
  4. (at your option) any later version. #
  5. #
  6. This program is distributed in the hope that it will be useful, but #
  7. WITHOUT ANY WARRANTY; without even the implied warranty of #
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
  9. General Public License for more details. #
  10. #
  11. You should have received a copy of the GNU General Public License #
  12. along with this program; if not, write to the #
  13. Free Software Foundation, Inc., #
  14. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #

#########################################################################

touch old_ip.dat

wget halfzware.com/whatismyip.php

SITE=$"whatismyip.php"
SENDER=$"Little@Gremlins.com"
RCVR=$"5745551234@vtext.com"
RCVR2=$"YOUR_EMAIL@gmail.com"

read T1 < $SITE
read T2 <old_ip.dat
IP=$(cat $SITE)

if [ "$T1" = "$T2" ]; then
echo "IP Is the same doing nothing:";
else
echo "From: $SENDER"; echo "To: $RCVR"; echo "Subject: IP Address Update"; echo; echo "Computer's IP Changed. The new IP is:"; echo $IP | sendmail -f $SENDER $RCVR; echo $IP | sendmail -f $SENDER $RCVR2;

fi

echo $IP
rm old_ip.dat
mv $SITE old_ip.dat


One thought on “Shell Script: Report External IP Address Via Email

  1. cory,

    hey cory,

    seattle is cool, and you wish you were here!!!! ahahahaha

    you could play with your computers all day!!!!
    haha

    peace out!
    g

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>