Grabbing WAN IP of D-Link DI-704 or DI-704P
By David Efflandt
If one of these sites is unresponsive, try the other [
My Home
| Freeshell.org
]
I am currently using a modified version of the Perl
monitoring script to monitor the public PPPoE IP of a 2Wire DSL
modem/router which I could provide upon request (tested with 2700HG-B
and 2701HG-B). No firmware modification was necessary.
efflandt@ameritech.net
Disclaimer
While every reasonable effort has been made to assure the accuracy of
the information provided, the information is free, and the only guarantee
is that it works for me. You are encouraged to follow manufacturers instructions
and documentation, and verify that any information or files provided are
suitable for your use. Especially make certain that any firmware downloaded
has exactly the same file size as original firmware of the same version, to
make certain that there were no download problems. If you have tools to compare
binary files, the only difference should be part of one (1) string in a text
block that begins on a new line with "Web Conf" and is the same length as
"Web Configuration".
This has only been tested on a D-Link DI-704 that came with firmware
version 2.56 build 20. I am now using firmware version 2.57 build
3 with packet filtering which also works fine. Consult your manual
or D-Link website for instructions on updating firmware from the web configuration
interface.
If the DI-704P uses the same exact firmware (I did see mention
of "Print Server" in it), then it should work for it as well, but is
untested .
Background
The author has been working with Perl scripts (mostly CGI) on assorted
Unix systems (Solaris, Linux, FreeBSD, NetBSD) since 1995. While I had a
perfectly functioning Linux firewall/masq/gateway connected with pppoe,
I could not pass up the opportunity to purchase a DI-704 broadband gateway
for $29 US after rebate, to see how the other half lives. With only a few
incoming ports open from outside (ssh, smtp, www, and irc when running)
my system logs are much quieter.
Since I use a dynamic DNS service to assign static names to my
dynamic IP, I wanted to find the internet IP of my DI-704 from inside,
so I could notify the service when my IP changed. I have a DNS update client
for no-ip.com that can run as a daemon and contact them periodically. The
question was, how often is enough to recover quickly if the system has
reconnected with new IP, but not excessive when things are stable? So my
goal was to check the internet IP of my DI-704 frequently without creating
unnecessary internet traffic. Some dynamic DNS services will drop you if
your client contacts them too often when your IP has not changed.
I quickly figured out how to grab /status.htm and parse the IP and
Connection Time from it with a Perl script in Linux. But the problem was,
I could not figure out how to login from a script, since the login
is a confusing mixture of frames and JavaScript. Logging in to the 704
from a web browser first, allowed the script to work, but also opens a potential
security hole. Once someone has logged into the DI-704 from a particular
IP, anyone from that IP could potentially access it until either logout
or timeout due to inactivity.
Then someone explained to me how to modify text in the firmware to
put a variable in /begin.htm showing the WAN IP on the login screen without
having to login. This eliminated the potential security problem. I
went one better and found the variable to also show Connection Time
. However, that variable differs between one used for DHCP or PPP0E, and
another one used for dialup PPP serial interface. So I am including versions
with and without connection time. If you only use a modem and want to show
connection time let me know and I can provide it.
Compressed Firmware Mod with Related Perl Script Monitor
The firmware modification involves simply replacing some text on the included
/begin.htm page with variables. No binary code was changed, so it should
be safe if the firmware version is suitable for your device. These are related
pairs of firmware and Perl script to in a compressed format (.tar.gz for
Unix and .zip for Windows). The Perl script can monitor the information provided
by the modified firmware, and take some action if the IP changes.
Each of these compressed files (130-136K) includes 2 files: modified firmware
that begins "DI704", and a script that begins "wanip". Scroll down for details
or individual files.
Connect time is for non-static WAN interface (DHCP, PPPoE, etc.). Should
display 00:00:00 if disconnected or does not apply. See below for details.
Adding WAN IP to Login Display
Changing "Web Configuration" to "Web Config IP $XI" in /begin.htm ends
up looking like:
Web Config IP 66.73.196.163
This should work for any connection, but I have only tested it with
PPPoE. Files are about 200K.
You are encouraged to confirm that this modified firmware is the same
file size after downloading as your original firmware and use any other
tools at your disposal to assure that the only thing changed was the one
line that begins with "Web Config". Use a wanip script below to monitor
it.
Adding WAN IP and Connection Time to Login Display
Changing "Web Configuration" to "Web Conf $XI $MT0" in /begin.htm ends
up looking like:
Web Conf 66.73.196.163 33:58:40
This is for non-static WAN connections like DHCP, PPPoE, etc. Serial PPP
connection time uses a different variable ($MT1), so the $MT0 used here
would display 00:00:00 connection time for a serial modem connection. Files
are about 200K.
Again you are encouraged to confirm that these files are the same size
as original same version firmware and that the only thing different
is the text line that begins with "Web Conf". This will work with
either wanip or wanipup scripts below, but latter may show 00:00:00 time
for certain connection types (static ethernet or serial PPP).
NEW - WAN IP and DHCP Lease Time on Login
Display
Changed "Web Configuration" to "Web Conf: $XI $XL" in /begin.htm. This
should work for cable modem or other connections that use DHCP and get a
lease for WAN IP. I lifted the $XL variable from the status.htm where it
displays Lease Time. I cannot verify what the actual display looks like,
but it should not hurt anything to try it since it is just a text modification
of same number of characters. Files are about 200K
Note: These display DHCP Lease Time, and time would likely just be 00:00:00
for PPPoE, PPP or set Static IP. Confirm that downloaded files are same size
as original same version firmware. This should work with either wanip or
wanipup scripts below, but you may want to alter log output to something
suitable for lease time instead of "up".
Related Perl scripts to Monitor IP changes
These scripts can be run once to display information, or can daemonize
themselves (or loop in Windows) to automatically poll the DI-704 at regular
intervals and take some action if the IP changes (like run a program or
script). The wanip scripts will work with either firmware mod to show the
IP. The wanipup versions will also display connection time if you installed
that firmware mod.
The default is to run once. To run in a continuous loop (background
daemon in Unix), just put anything other than 0 (zero) on the commandline,
bg or loop or anything, it does not matter. Or change the
$bg = shift or 0; to $bg = shift or 1; in the script. If you change this
to 1 just put a 0 (zero) alone on the commandline to run once.
System logging is enabled by default in Unix version, and file logging
is enabled in Win version. See script for details.
The Unix scripts can be run by anyone to poll the DI-704, but might
need to be run by root for your DNS update client to work, depending upon
how that is set up (permissions of related files). Files are about 5K.
Script Source for DOS/Win versions
(Activestate Perl)
|
wanip.txt
|
wanipup.txt
|
rename to filename
with .pl file extension
|
Note that if you move scripts between operating systems, you need to
use ftp ASCII text mode (not binary) or otherwise correct the line endings.
DOS/Win uses carriage return, linefeed pairs, Unix just uses linefeeds. and
Mac just uses carriage returns.
Sourcing Variables from .waniprc (waniprc.txt in DOS/Win)
User variables can be set in a separate file pointed to by $rcfile.
This file just needs read permission. Any variables you define there are
written just like in a Perl script. For example:
$di704 = '172.16.1.1';
Variables set in the rc file will override any default user variables
in the main script. Variables that can be set in the rc file are: $di704
(LAN IP), $ipfile (last IP), $bg (daemonize/loop or not), $poll (check
IP interval), $tick (log interval) and $logfile (undef to not log).
Variables that can only be set in the main script are $rcfile, a flag
to use system log (Unix only), and the subroutine to run when IP changes
(I need to test if that sub can be redefined externally).
Conclusion
If all goes well you will end up with a script that can daemonize itself
and run in the background (or loop in a DOS window) to check your external
IP and automatically do something useful if your IP changes, without generating
unnecessary internet traffic while unchanged.
efflandt@ameritech.net