Tags: IP Address Lookup, Information, and Location, Test Your Internet Connection Speed
|
#1
|
|||
|
|||
|
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
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) 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. |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
Nice work! (havent tested it myself but it looks fancy)
|
|
#4
|
|||
|
|||
|
Quote:
![]() 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. |
|
#5
|
|||
|
|||
|
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
|
|
#6
|
|||
|
|||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|