[Memphis.pm] Re: [GOLUM] How to get Linksys routers WAN ip???
Keith W. Sheffield
sheff at pobox.com
Wed Oct 10 20:52:20 CDT 2001
>>>>> "Emory" == Emory Smith <stratcat at midsouth.rr.com> writes:
> I have a Linksys router on RR and would like to have
> my server (MDK8.1) poll it about 2-3 times a day and
> get its WAN ip and send it to my work email.
> I can get into the routers status page by browsing to
> http://yada:password@192.168.1.1/Status.htm with
> Mozilla or Netscape. (Username (yada) can be anything).
> Lynx complains, ask questions and ultimately doesn't get in.
> Is there another text based browser that might do this?
> It would seem that I should be able to redirect the browser's
> output to grep and mail the result.
> Or, Brock might have some really eloquent method. (hint, hint)
LWP is your friend.
Like many things, there's more than one way to do it:
very simple way:
--------------------------
#!/usr/bin/perl
use MIME::Lite::HTML;
my $mailHTML = new MIME::Lite::HTML
From => 'me at mymachine',
To => 'my at atwork',
Subject => 'Mail in HTML with images';
$MIMEmail = $mailHTML->parse('http://yada:password@192.168.1.1/Status.htm');
$MIMEmail->send;
-------------------
That's assuming that it knows how to do the yada:password stuff and it will
include any images in the email
Here's the way I do it at work and it creates absolute links to images and
other stuff if needed:
--------------------------------
#!/usr/bin/perl -- # -*-Perl-*-
use strict;
use LWP::UserAgent ;
use HTTP::Cookies;
use HTML::TreeBuilder;
use MIME::Lite;
use URI::URL;
use URI::Escape qw(uri_unescape);
# id/password combo for getting into status page
my $user_id = 'yada';
my $user_pw = 'password';
# if you need an id/password combo to get out of the proxy
#my $proxy_id = "";
#my $proxy_pw = "";
my $from_addr = "me at mymachine";
my $to_addr = "me at work";
my $baseurl = "http://192.168.1.1";
my $url = "$baseurl/Status.htm";
# create a web user agent object
my $ua = new LWP::UserAgent || die "$!";
#$ua->proxy(['http', 'ftp'] =>'http://myproxy:8080');
#$ua->no_proxy(qw(domain));
$ua->agent('Mozilla/4.03');
#In case you want cookies
my $cookie_jar = HTTP::Cookies->new;
# get a request object for sending the data file
my $req = new HTTP::Request 'GET', $url;
die "Error building request\n", unless($req);
# set all the password stuff and make sure we can store cookies
$req->authorization_basic($user_id, $user_pw);
# uncomment if using a proxy that needs passwords
#$req->proxy_authorization_basic($proxy_id, $proxy_pw);
$cookie_jar->add_cookie_header($req);
# get the results
my $res = $ua->request($req);
die "Error getting results from request\n" unless ($res);
if ($res->is_success)
{
# we get a valid result back, so send email to anyone that cares.
$cookie_jar->extract_cookies($res);
print " ----- Cookies from $url ------\n";
print $cookie_jar->as_string;
# parse the html
my $html = new HTML::TreeBuilder;
$html->parse($res->content);
&xform_urls($html); # and make all the links/images absolute so we can
# see them
# now create the email object
my $msg = new MIME::Lite
From => $from_addr,
To => $to_addr,
Subject => "Status results",
Type => 'multipart/mixed';
# include our response page
attach $msg Type => 'text/html',
Data => $html->as_HTML(undef, " ");
# Send the mail and do the honorable thing to do if you fail.
$msg->send || die "Error sending email!\n";
print "Mail sent!\n";
exit(0);
}
else
{
print $res->error_as_HTML() if ($res->is_error);
print $res->status_line, "\n";
}
exit(1);
#
# This function takes a parsed html tree or subtree and transforms any
# relative urls or images to absolute ones so that the html can be viewed
# in an html email client or on another web server
#
sub xform_urls
{
my $x = $_[0];
my $tag = lc $x->tag;
$x->attr('href', uri_unescape(url($x->attr('href'),$baseurl )->abs))
if ($tag eq "a" && defined $x->attr('href'));
$x->attr('src', uri_unescape(url($x->attr('src'),$baseurl )->abs))
if ($tag eq "img" && defined $x->attr('src'));
foreach my $c ($x->content_list)
{
xform_urls($c) if ref $c; # ignore text nodes
}
}
----------------------------------------------------------------------------
To unsubscribe, please send email to majordomo at pm.org
with 'unsubscribe memphis-pm-list' in the body of the message.
----------------------------------------------------------------------------
More information about the Memphis-pm
mailing list