Track WAN IP changes using a server's log

Post new topic   Reply to topic    DD-WRT Forum Index -> Contributions Upload
Author Message
SteveDemy
DD-WRT Novice


Joined: 30 Jul 2008
Posts: 9

PostPosted: Thu Apr 05, 2012 3:34    Post subject: Track WAN IP changes using a server's log Reply with quote
I manage a number of Buffalo routers for family and friends. The typical scenario is that I get a call requesting some computer help. The first step is getting myself into their router or computer. That means I have to know their external IP address, which usually entails talking them through getting it. I wanted a method of keeping up with their IP addresses myself, so I can take better care of their computers/networks remotely whenever I want.

I found the script on this thread which addressed the problem, but not to my satisfaction. I don't want to use e-mail for this purpose.

Instead, I have the router "call home" sending a log entry to my mac mini server at home. From there, I can use the Console application to view the router's reports.

I was a complete router/Linux newbee a couple of weeks ago, and now am only slightly more advanced. But in overview, here's the solution:

1) Turned on JFFS in the router to give me a place to store stuff
2) Established an SSH key-pair between my router (client) and server (host)
3) Put the enclosed script into a directory /jffs/usr/bin, and set up a startup script to evoke it when the router is rebooted
4) Designated a place in my mac where the router.log file goes

So far, this works like a charm. I look at my Console to see when the script (or the whole router) started, and when an IP address changes. One glance and I know the IP address associated with a router's name. I intend to modify the startup script so that my main script can be kept up to date centrally, and pulled into each router at runtime. That will ensure the concept survives firmware upgrades and the like. Work lies ahead ...

I ran into two major problems along the way.

1) I could not get dropbearkey in the router to generate a public SSH key. Turns out you can make openSSH keys on the host (my mac) and transport the private key component to the router and convert it to the dropbear format using dropbearconvert on the router. It's not normally done that way but worked fine.

2) I am a newbie at all things Linux, so the terminal interface was a mystery at first. It took a heck of a lot of reading to solve even the most minor syntax problems. From reading the forums, I see a lot of us are in that same boat...

Credit goes to cyberde for the basic idea and script logic.

Code:
#!/bin/sh
# WANIPReport
# Checks a router's WAN IP address at a given interval.
# Sends a message to a server's log on startup and after a wan IP address changes.
#
# This script is kept in /jffs/usr/bin on the router.

# Created by Steve Demy for DD-WRT routers
# Version 2.0

# Settings
# The WAN IP address checking interval, in seconds
INTERVAL=3600

# Log messages are written to a server identified here
SSH_USER=<your user name on host>
SSH_SERVER_NAME=<host domain name>
SSH_SERVER_PORT=<host port>
SSH_SERVER_PATH=<path on host to log file>
SSH_SERVER_FILE=<name of log file (.log extension)>

# Set output message variables
TIME_NOW=`date`
CURR_WAN_IPADDR=`nvram get wan_ipaddr`
THIS_ROUTER=`nvram get router_name`

# Note that the script is starting
MESSAGE="$TIME_NOW: $CURR_WAN_IPADDR $THIS_ROUTER: WAN IP address monitoring started"
# Send the note to the log
ssh -i /tmp/root/.ssh/id_rsa -p $SSH_SERVER_PORT $SSH_USER@$SSH_SERVER_NAME\
 "echo \"$MESSAGE\" >> $SSH_SERVER_PATH$SSH_SERVER_FILE"

# Does the nvram variable wan_ipaddr_last exist?
if ! nvram show | grep -q wan_ipaddr_last; then
   # If not, set it to the current WAN IP address and commit the changes
   nvram set wan_ipaddr_last=`nvram get wan_ipaddr`
   nvram commit
fi

# Periodic WAN IP address check, logging the results as required
while sleep $INTERVAL
do
   CURR_WAN_IPADDR=`nvram get wan_ipaddr`
   LAST_WAN_IPADDR=`nvram get wan_ipaddr_last`
   # Router name re-acquired in case it has been changed
   THIS_ROUTER=`nvram get router_name`

   if [ $CURR_WAN_IPADDR != $LAST_WAN_IPADDR ]; then
     
      # Note that the IP address has changed
      MESSAGE="$TIME_NOW: $CURR_WAN_IPADDR $THIS_ROUTER: WAN IP address changed"
      # Send the note to the log
      ssh -i /tmp/root/.ssh/id_rsa -p $SSH_SERVER_PORT $SSH_USER@$SSH_SERVER_NAME\
       "echo \"$MESSAGE\" >> $SSH_SERVER_PATH$SSH_SERVER_FILE"
     
      # Update the nvram variable to the current IP-Address
      nvram set wan_ipaddr_last=$CURR_WAN_IPADDR
      nvram commit
   fi
done


------------
Buffalo WZR-HP-G300NH
DD-WRT v24-sp2 std (c) 2010 NewMedia-NET GmbH
Release: 08/19/10 (SVN revision: 14998)
Sponsor
SteveDemy
DD-WRT Novice


Joined: 30 Jul 2008
Posts: 9

PostPosted: Fri Apr 06, 2012 23:46    Post subject: Version 2.1 Reply with quote
OK, so I didn't make my living writing software - thank goodness.

1) I set the TIME_NOW value at startup but didn't update it, so it would report the incorrect time when the IP address change message is sent.

2) I found that despite setting an ntp time and timezone, I am not getting the correct and consistent time from these routers. Better to timestamp the incoming log messages at the server, instead of at the router. Now the `date` value is derived at the server and reports the time of the arrival of the message.

The update:

Code:
#!/bin/sh
# WANIPReport
# Checks a router's WAN IP address at a given interval.
# Sends a message to a server's log on startup and after a wan IP address changes.
#
# This script is kept in /jffs/usr/bin on the router.

# Created by Steve Demy for DD-WRT routers
# Version 2.1

# Settings
# The WAN IP address checking interval, in seconds
INTERVAL=3600

# Log messages are written to a server identified here
SSH_USER=<your user name on host>
SSH_SERVER_NAME=<host domain name>
SSH_SERVER_PORT=<host port>
SSH_SERVER_PATH=<path on host to log file>
SSH_SERVER_FILE=<name of log file (.log extension)>

# Set output message variables
CURR_WAN_IPADDR=`nvram get wan_ipaddr`
THIS_ROUTER=`nvram get router_name`

# Note that the script is starting
MESSAGE="$CURR_WAN_IPADDR $THIS_ROUTER: WAN IP address monitoring started"
# Send the note to the log
ssh -i /tmp/root/.ssh/id_rsa -p $SSH_SERVER_PORT $SSH_USER@$SSH_SERVER_NAME\
 "echo \`date\`: \"$MESSAGE\" >> $SSH_SERVER_PATH$SSH_SERVER_FILE"

# Does the nvram variable wan_ipaddr_last exist?
if ! nvram show | grep -q wan_ipaddr_last; then
   # If not, set it to the current WAN IP address and commit the changes
   nvram set wan_ipaddr_last=`nvram get wan_ipaddr`
   nvram commit
fi

# Periodic WAN IP address check, logging the results as required
while sleep $INTERVAL
do
   CURR_WAN_IPADDR=`nvram get wan_ipaddr`
   LAST_WAN_IPADDR=`nvram get wan_ipaddr_last`
   # Router name re-acquired in case it has been changed
   THIS_ROUTER=`nvram get router_name`

   if [ $CURR_WAN_IPADDR != $LAST_WAN_IPADDR ]; then
     
      # Note that the IP address has changed
      MESSAGE="$CURR_WAN_IPADDR $THIS_ROUTER: WAN IP address changed"
      # Send the note to the log
      ssh -i /tmp/root/.ssh/id_rsa -p $SSH_SERVER_PORT $SSH_USER@$SSH_SERVER_NAME\
       "echo \`date\`: \"$MESSAGE\" >> $SSH_SERVER_PATH$SSH_SERVER_FILE"
     
      # Update the nvram variable to the current IP-Address
      nvram set wan_ipaddr_last=$CURR_WAN_IPADDR
      nvram commit
   fi
done
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Index -> Contributions Upload All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum