What Is My IP Address? - IP Address Lookup, Bandwidth Speed Test, IP Info, plus more

Go Back   What Is My IP 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.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old 10-20-2009, 09:10 PM   #1
dwasifar
Member
 
Join Date: Sep 2009
Posts: 9
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  
 

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 11:16 AM.


What Is My IP - IP Command Lines - What is an IP Address - Speed Test - IP Address FAQ

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

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