« Sick Boy | Main | So far so good... »

December 24, 2003

Fun with WWW::Mechanize

While I am out here at my mom's I figured it'd be a good time to redo the network. I set up a D-Link wireless router instead of the Linksys. That way I don't need to create a wireless network with my G4 when I come over. The only problem is that I have a script that runs every hour on the G4 which gets the external IP address from the Linksys and emails me if it changes. That way I can always SSH into my G4. Unfortunately the D-Link admin webpage is set up a little different, so I went and rewrote the script using Andy Lester's WWW::Mechanize perl module.

The trick here was that the D-Link uses a basic browser authentication (those little dialogs that prompt you for a username and password for protected sites). It took me a little while to google out an answer for how to do that.

Here's the way to do it:

my $agent = WWW::Mechanize->new();
my @args = (Authorization => "Basic " .
            MIME::Base64::encode(USER . ':' . PASS));
$agent->credentials(IP_ADDRESS, REALM, USER, PASS);
$agent->get(ADMIN_URL, @args);

The cool thing with the new script is that it's really easy to adapt it to work with other routers. I have an Asante wireless router that I can easily adapt the script to talk to. Nifty!

Posted by ed at December 24, 2003 4:37 PM

Comments

Thanks for your post. I've been trying to figure out how to get HTTP Basic authentcation working with WWW Mechanize. The documentation for ->credentials() in LWP UserAgent sucks, or I'm just not looking in the right location. Anyway, you helped lots. Thanks again!

Posted by: Gabe at November 16, 2004 9:17 PM

Many thanks for this, was trying to figure this out; your post did it.

Posted by: Jason at December 7, 2004 9:19 PM

Post a comment




Type the characters you see in the picture above.


Remember Me?