pavement

Set-ddns.pl router settings list

From FreeBSDwiki
Revision as of 13:49, 21 June 2007 by Jimbo (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This article contains a list of several methods of extracting and parsing your WAN IP address from various routers and externally hosted scripts, for use with set-ddns.pl, a script you can use to update a dynamic zone on a nameserver that you control.

If you have a router we haven't covered, and you adapt a method to extract its WAN IP, please feel free to add it here in a section headed by == Your Router's Model == bracketed with twin equals signs.


Contents

Westech WireSpeed DualConnect Home DSL Gateway

# BellSouth el cheapo residential gateway: there is no configurable username and password to set!
# Just make sure that either your WireSpeed is still set to the factory default private IP address,
# or that you adjust the router_url_string below to reflect whatever you have it set to now.

$router_url_string = 'http://192.168.1.254/homeBS.htm';

$ua = LWP::UserAgent->new;
$req = HTTP::Request->new('GET',$router_url_string);
$resp = $ua->request($req)->as_string();

@body = split (/\n/, $resp);
$WAN = ;
foreach $string (@body) {
    if ($WAN eq ) {
         if ($string =~ /^var IpAddress \= \"(\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3})\"\;/) {
               $WAN = $1;
         }
    }
}

Xincom Twin Wan Router XC-DPG502

# This is a dual-homed router.  This code block assumes cable on WAN1 and dsl on WAN2, with a preference 
# for WAN1.  So three host records are kept: dynamic.domain.net for the default, cable.dynamic.domain.net
# for WAN1, and dsl.dynamic.domain.net for WAN2.  If you get fancy, you could even set something tricky 
# up on the server side to check WAN1 and WAN2 from the other side after they're established, and automatically 
# fail the "default" host, dynamic.domain.net, over to whichever side is still up if one of them fails.

$ROUTER_URL = '192.168.0.1/netstat.htm';
$ROUTER_USERNAME = 'admin';
$ROUTER_PASSWORD = 'password';

$HOST0 = 'dynamic.domain.net';
$HOST1 = 'cable.dynamic.domain.net';
$HOST2 = 'dsl.dynamic.domain.net';

$router_url_string = 'http://' . $ROUTER_USERNAME . ':' . $ROUTER_PASSWORD . '@' . $ROUTER_URL;
$ua = LWP::UserAgent->new;
$req = HTTP::Request->new('GET',$router_url_string);
$resp = $ua->request($req)->as_string();

# Simplest to just count the dotted quads: WAN1 and WAN2 are the second and third one in.
$_ = $resp;
($ip1, $ip2, $ip3) = /\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}/gs;

$WAN1 = $ip2;
$WAN2 = $ip3;

# You don't want to cycle through set-ddns.pl for each host - it's MUCH slicker and quicker to send all
# three updates in a single nsupdate invocation.  Note: I chose to skip the "show" command on nsupdate here.
# It's handy for manual troubleshooting, but unnecessary once you have everything working and you're relying
# on a crontab piping the output to /dev/null anyway.

chdir ($KEYDIR);
open (NSUPDATE, "| /usr/sbin/nsupdate -k $KEYFILE");
print NSUPDATE "server $NAMESERVER\n";
print NSUPDATE "update delete $HOST0 A\n";
print NSUPDATE "update delete $HOST1 A\n";
print NSUPDATE "update delete $HOST2 A\n";
print NSUPDATE "update add $HOST0 $TTL A $WAN1\n";
print NSUPDATE "update add $HOST1 $TTL A $WAN1\n";
print NSUPDATE "update add $HOST2 $TTL A $WAN2\n";
print NSUPDATE "send\n";
close (NSUPDATE);

Generic WAN-hosted script methods

If you're having a particularly tough time trying to worm any useful information out of your router's regularly available configs, you can always fall back on hosting a script on your webserver and using it to feed your WAN IP back to you. You have three options here; a PHP script, a Perl-CGI, and a Bourne-CGI. Each of them give output in the same format as http://checkip.dyndns.org - so tools designed to work with it will work with these, and vice versa.

This php version can be run from any php-enabled directory on your server - depending on your php configuration, you may be able to run it as ip.html, or you might need to name it ip.php.

<html>
<head>
<?php $ip = $_SERVER['REMOTE_ADDR']; ?>
<title>http://server.net/ip.php: <?php echo $ip; ?></title>
</head>
<body>
<?php echo "Current IP Address: " . $ip; ?>
</body>
</html>

If you aren't having any luck with parsing your router's output or getting php to work on your webserver, this perl-CGI version produces exactly the same output as the php version above. Place it in a cgi-enabled directory - on most servers, that will be http://server.net/cgi-bin/ip.cgi or something very like it.

#!/usr/bin/perl

print "Content-type:text/html\n\n";
print "<html><head><title>$ENV{REMOTE_ADDR}</title></head>\n";
print "<body>Current IP Address: $ENV{REMOTE_ADDR}</body>\n";
print "</html>\n";

If you still aren't having any luck (or expect TONS of traffic and prefer a bit lower overhead), you can also try this Bourne scripted version - it's text-only, so no IP address in the page title, but the body output is identical to the other two so that the same parser still works.

#!/bin/sh

cat <<EOF
Content-type:text/plain

Current IP Address: ${REMOTE_ADDR}
EOF


Generic parser for use with http://server.net/ip.php, http://server.net/cgi-bin/ip.cgi, or http://checkip.dyndns.org

(Remember to change $url_string to match the script you're referencing.)

$url_string = 'http://server.net/ip.php';
$ua = LWP::UserAgent->new;
$req = HTTP::Request->new('GET',$url_string);
$resp = $ua->request($req)->as_string();

$_ = $resp;
m/.*?Current IP Address\: (\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}).*/gs;
$WAN = $1;

D-Link DGL-4300 802.11g MIMO "Gamer's Lounge" Router

# note: this sucker does fancy 'fake authentication' that uses some sort of session ID instead of
# standard HTTP authentication.  It's certainly possible to mimic it using LWP::Credentials, but I'm not 
# feeling the need to actually fight it through right now - and since it supports true syslog output, it might 
# be easier just to set up a syslog server and monitor incoming syslog messages for the latest incoming
# line to match /.*?IP Address (\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}) and default gateway/ and work from that.
# 
# of course, since this is only a single WAN router, you could always take the cheesy route and just fall back
# on one of the external script methods outlined above. =)
Personal tools