Go Back   WhatIsMyIP.comŽ Forum > All About Code > Code Requests

Notices

Code Requests People having specific needs to automate their IP retrieval can post their needs here in hopes that a coder can help them out.

Closed Thread
 
Thread Tools Display Modes
Old 10-20-2009, 09:10 PM   #1
dwasifar
Member
 
Join Date: Sep 2009
Posts: 10
dwasifar is on a distinguished road
Default Script to watch and log dynamic IP changes

Here's a Linux shell script to scrape the current IP from a Linksys router's status page once per minute and log any changes.

Code:
#!/bin/sh
# Reads internet IP from Linksys router, reports changes
clear
b=0
message='- logging start'
if [ "$2" != '' ]; then
   router_username=$2
else
   router_username="admin"
fi
if [ "$3" != '' ]; then
   output_filename=$3
else
   output_filename="~/ip_history.txt"
fi
if [ "$1" != '' ]; then
  while [ $b = 0 ]
  do IPADR2=$IPADR;
  IPADR=$(wget -q -O - http://$router_username:$1@192.168.1.1/Status_Router.asp | sed -n '/ipaddr/ {n;p;}' | sed -e 's/.*<B>//; s/<\/B>.*//')
  if [ "$IPADR" != "$IPADR2" ]; then
      dttim=$(date)
      echo $dttim $IPADR $message
      echo $dttim $IPADR $message >> $output_filename
      message=''
  fi
  sleep 60s
  done
else
  echo "Usage: ip_history [router_password] {router_username} {output /path/filename}"
  echo "Router username defaults to admin"
  echo "Output defaults to ~/ip_history.txt"
fi
Copy this code into a text editor, save it wherever you want, name it ip_history, and set it executable.

For some Linksys routers, the status page is Status_Router.htm (or something similar) instead of .asp.

If your router doesn't offer a status page, you can modify this script to pull the IP from whatismyip.com. Substitute this line to set the IPADR variable:
Code:
IPADR=$(wget www.whatismyip.com/automation/n09230945.asp -O - -q)
...and change sleep 60s to sleep 300s (or a higher value) to avoid hitting whatismyip.com's automation page more frequently than five minutes apart.

I have this script set to run automatically when I boot, so it doesn't have its own window; but if you run it manually, it will log IP changes to the terminal window as well as the log file.
dwasifar is offline  
Old 10-20-2009, 09:45 PM   #2
wimiadmin
Administrator
 
wimiadmin's Avatar
 
Join Date: May 2008
Location: Pigeon Forge, TN
Posts: 1,511
wimiadmin is a splendid one to beholdwimiadmin is a splendid one to beholdwimiadmin is a splendid one to beholdwimiadmin is a splendid one to beholdwimiadmin is a splendid one to beholdwimiadmin is a splendid one to beholdwimiadmin is a splendid one to behold
Default

Very nice dwasifar. Thanks for your contribution.

The user should also keep in mind that if the IP to their router is different than 192.168.1.1 they'll need to make the necessary changes in the script to reflect the correct IP.
__________________
Brian
Please Search Before Posting
wimiadmin is offline  
Old 10-20-2009, 11:50 PM   #3
AboveTheLogic
Super Moderator
 
AboveTheLogic's Avatar
 
Join Date: Jul 2008
Location: Las Vegas
Posts: 727
AboveTheLogic is a glorious beacon of lightAboveTheLogic is a glorious beacon of lightAboveTheLogic is a glorious beacon of lightAboveTheLogic is a glorious beacon of lightAboveTheLogic is a glorious beacon of light
Default

Nice work! (havent tested it myself but it looks fancy)
AboveTheLogic is offline  
Old 10-21-2009, 01:26 AM   #4
dwasifar
Member
 
Join Date: Sep 2009
Posts: 10
dwasifar is on a distinguished road
Default

Quote:
Originally Posted by wimiadmin View Post
Very nice dwasifar. Thanks for your contribution.

The user should also keep in mind that if the IP to their router is different than 192.168.1.1 they'll need to make the necessary changes in the script to reflect the correct IP.
Yes, absolutely. I should have pointed that out. But I'm sure anyone who has changed their Linksys router's default IP knows they have done so.

Also, if you do the alteration to pull the IP from whatismyip.com, then it doesn't matter what parms are used for the router login; they can just be any old junk, or the script could be altered so as not to require them.
dwasifar is offline  
Old 10-24-2009, 05:39 PM   #5
Juggernaut
Member
 
Join Date: Oct 2009
Posts: 1
Juggernaut is on a distinguished road
Default Modifications

I hope you don't mind, but I've modified your script to send an email if the IP is changed taking away the need to either check the screen of the box running the script or manually checking the log file.

My only issue now is to get it to start when the box boots up. It's a FreeBSD box and I just haven't got around to setting it up yet. My current work around is using the 'nohup' from a putty connection.

Thought I'd post it in case anyone else has a need for it. Here's what I've modified it to:

Code:
#!/bin/sh
# Connects to www.whatismyip.com every 12 hours to get IP address and checks if it has changed

clear
output_filename="/usr/home/<user>/ip_history.txt"
while [ 1 ]
  do IPADR2=$IPADR;
  IPADR=$(wget www.whatismyip.com/automation/n09230945.asp -O - -q)
  dttim=$(date -j +"%d %b %Y @ %H:%M:%S")
  if [ "$IPADR" != "$IPADR2" ]; then
    echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> $output_filename
    echo ~~~~~ IP Address has changed ... $dttim >> $output_filename
    echo ~~~~~ Our Old IP Address was -- $IPADR2 >> $output_filename
    echo ~~~~~ Our New IP Address is --- $IPADR >> $output_filename
    echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> $output_filename

    mail -s "IP Changed" user@mydomain.me << E_O_M
Good day Master,

  I Regret to Inform You That Our IP Has Changed

  Our Old IP Address was -- $IPADR2
  Our New IP Address is --- $IPADR

  This Occurred Within The Past 12 Hours From -- $dttim

  Our Records Will Need To Be Updated At
     https://my.registrar.com

  Reference: $output_filename
E_O_M

  else
    echo IP Address is the same ... as of $dttim ... $IPADR  >> $output_filename
  fi
  sleep 43200 # 12h = 43200
done
Juggernaut is offline  
Old 10-25-2009, 02:05 AM   #6
dwasifar
Member
 
Join Date: Sep 2009
Posts: 10
dwasifar is on a distinguished road
Default

Oh, by all means, modify and use freely. I like your version. This script started life not as a logging program, but to do ftp updates to a remote web server, keeping a remote link pointed at the dynamic IP, so what you're doing is actually closer to what I originally had.

For automatic startup, I don't know how FreeBSD handles it, but in Linux you could put a symlink to it in /etc/rc3.d or one of the other run level control directories. Since I'm running it on Ubuntu, I have the luxury of just using the Startup Applications tool instead.

Just because I pigheadedly specify sleep times in seconds doesn't mean you have to. You could do sleep 12h, just as I could have done sleep 1m or sleep 5m or what have you.
dwasifar is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:38 PM.


What Is My IP

Join WhatIsMyIP.com on Facebook Follow WIMI on Twitter

Need IP address location detection service? Get it from IP2Location.com now.

Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright - WhatIsMyIP.comŽ