Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get modem router stats problem

Status
Not open for further replies.

RodP

Programmer
Jan 9, 2001
109
GB
Hi Everyone,

I'm very new to HTML source code and Perl scripts but I've been trying to get this script working to logon to my 3com ADSL router modem, goto the Status page and record certain stats (eg signal margin) into a text file.

I found this script on the net:
##### start code #####
# --- VARIABLES --- #
# url to stats page - replace user & pass with the router username & password
my $url ='
# output file
my $log = 'C:\Temp\router-stats.txt';

# --- PROGRAM --- #

# connect to router and get raw html
use LWP::UserAgent;

my $ua = new LWP::UserAgent;
my $req = HTTP::Request->new(GET => $url);
my $response = $ua->request($req);
my $html = $response->content;

# get date/time & format
my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time());
my $dt = sprintf("%04d%02d%02d%02d%02d",1900+$year,$mon+1,$mday,$hour,$min);

# extract statistic
$html =~ s/(\s|\n)+//g;
my($stat) = $html =~ m!NoiseMargin</td><tdwidth=185class=textCell>[^&]+ dB</td><tdwidth=185class=textCell>([^&]+)!;

# print to file
open(FH,">>$log");
print FH "$dt $stat\n";
close(FH);
##### end code #####

The problem is the program doesn't seem to return anything from the web page (just the time as requested). I think it's because the source code its looking for doesn't match up with what the source code is (because this script was designed for a different modem). I've looked at the source code but am not knowledgable enough with HTML and Perl to know how to alter it. For example, from the source code below, I'd want to try and capture the "Noise Margin". Can anyone help?

Many thanks in advance.

RodP

***HTML source from stauts page from router***

<tr><td colspan=3 class=titleCell>Operation Data</td></tr>
<tr><td width=190 class=textCell>Operation Data</th>
<td width=190 class=textCell>Upstream</th>
<td width=190 class=textCell>Downstream</th></tr>
<tr><td width=190 class=textCell>Noise Margin</td>
<td width=185 class=textCell>-0.5&nbsp;dB</td>
<td width=185 class=textCell>-0.5&nbsp;dB</td></tr>
<tr><td width=190 class=textCell>Output Power</td>
<td width=185 class=textCell>-0.5&nbsp;dBm</td>
<td width=185 class=textCell>-0.5&nbsp;dBm</td></tr>
<tr><td width=190 class=textCell>Attenuation</td>
<td width=185 class=textCell>-0.5&nbsp;dB</td>
<td width=185 class=textCell>-0.5&nbsp;dB</td></tr>
 
Hmm.. looks like the problem might be quite a few missing spaces - try changing the regex line to:

Code:
(my $stat) = $html =~ m!Noise\sMargin</td>\s*   # match Noise Margin
                        <td[^>]*>[^>]*</td>\s*  # match TD tag
                        <td[^>]*>([^&]+)!sx;    # match TD tag and capture dbm lvl
print $stat;
 
Hi rharsh,

Thanks for the reply. I've tried your code but it still doesn't seem to be working. I'm wondering if the http address is correct as when I try entering the address as in the script (include the admin:<password> bit), my modem router doesn't actually take me to the status page - it takes me to the login page.

Is there a way we can test this script out using another webpage? I've noticed that the script doesn't work at all if you simply put eg You need to put a file aswell (like index.html).

Could you suggest a site I could try this code on to ensure the $stat is working right and what the $stat line is. I tried it in the yahoo front page but it didn't work - I only have the runtime version of perl and so it doesn't give me much of a clue when some code is wrong! :(

Hope you or someone else can help.

Many thanks

RodP
 
Log into your router manually, go to the configuration page you want and see what the URL is - try plugging that into your script. You might want to look at the source for that page as well and make sure it closely matches (expect, for the values) the sample HTML you posted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top