Keeping Track of My IP Address

December 31, 2012 by
Filed under: bash, command line, HowTo, linux 

My main computer at home acts as a file and media server for the whole house, so it's pretty much turned on all the time. There are times when I'm away from home that I want to remotely access or download a file from my home computer. My home internet service uses a dynamic IP address and every once in a while we get a short interruption to our power that forces the modem to reset. Of course, when that happens, the modem pulls a new IP address. I thought it would be handy if I could come up with a script to keep track of my current IP address for me.

Finding my IP address

The first step is to find out my current IP address. In my case, I have a SpeedStream 5100 DSL modem. The address of the modem on the local network is and entering that address into a web browser will bring up a page that looks like this.

Modem Web Page

I've blurred some identifying information, but you get the idea. You can see from this image that the external IP address is one of the pieces of information displayed.

Affiliate Link

Getting the IP address as text

So now that I know where to find my IP address, I need to get it in text format and save it in a log file. The text based Lynx web browser is up to the task. There's a command line switch,  -dump, that will display the text of a web page to standard out.

lynx -dump
Modem Web Page Lynx

Modem Web Page Lynx

So now that I have the web page in text form, I can do some further processing to clip the IP address information.

lynx -dump | grep "IP Address" | head -1 | cut -d' ' -f12

In retrospect, awk would have been an easier tool to pull the IP address rather than cut at the end of the pipeline, but the above command did the job just fine.

Automating the task

One of the things I wanted was to know when my IP address changed even when I'm not at home. So I figured the best way was to send myself an email which I could read from my smart phone or other remote computer. I use Gmail for personal email, so I used my procedure for sending Gmail from the Linux command line. I chose to set it up under the root ID for security reasons since the email account ID and password end up getting stored as plain text in the configuration file.

I then wrote a bash script to check the IP address and save it to a file called ip-address.log under /var/log. The script then compares the IP address to the previous one and if there has been a change, it sends me an email. Here's a copy of the script.


#Check to see if ip-address.log exists
if [ -e /var/log/ip-address.log ]
   mv /var/log/ip-address.log /var/log/ip-address-old.log

#Get my current IP address from the modem
lynx -dump | grep "IP Address" | head -1 | cut -d' ' -f12 > /var/log/ip-address.log

if [ -e /var/log/ip-address-old.log ]
   #Check to see if they are the same. If the same then exit, otherwise send email.
   if diff /var/log/ip-address-old.log /var/log/ip-address.log >/dev/null
      #They are the same, exit
      exit 0
      #They are different,send email
      mail -s "IP Update" [email protected] < /var/log/ip-address.log

exit 0

Save the above script as the root user and save it to /usr/local/bin/get_ip. Of course, replace [email protected] with your actual email address. Make sure that the file is executable with

sudo chmod +x /usr/local/bin/get_ip

Now all that's left is to set up the script to run every so often. Linux has the cron command to take care of repetitive tasks like that. Since I set up my command line email to run as the root user, I also want my script to run as root. Edit the root crontab with

sudo crontab -e

Then just add a line to the bottom of the file that looks like this:

*/10 * * * * /usr/local/bin/get_ip

This will run a cron job every ten minutes to run my get_ip script. Now I get an update through my email whenever my modem resets and pulls a new IP address.

A similar technique could be used to monitor all sorts of things. What kinds of tasks would you like to automate? Let me know in the comments.

This post originally appeared at


5 Responses to “Keeping Track of My IP Address”

  1. I'm sorry, I meant "external IP address" where I said "adress" and "sudo apt-get install curl" instead of "sudo apt-get curl".
    Now, I'm trying to modify my external IP address at my domain provider's website automatically. I'm thinking in lynx to login at the website's control panel and modify it. Have you any idea to illustrate me? Thanks in advance.

  2. You can use curl to obtain your external IP adress:
    sudo apt-get curl


  3. AC says:

    I run ifconfig to get the IP address (Ubuntu 12.04).

    • Linerd says:

      ifconfig will only tell you your IP address on your LAN. It will not tell you the external Internet IP address.

  4. asfix90 says:

    There are many methods and tutorials that teach you how to find an ip address,I use this tool to display information about any ip address, just enter any IP address into the box and you will get the country, city, region, ISP, street address and the satellite location map for every query.

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>