#!/usr/bin/perl -w

# this module will fetch html page from AP and dump stats for LAN and WiFi
# 
# it's for TrendNet AP with firmware 3.5.0, if you have different one, you
# might want to modify it.
#
# 2003-08-09 Dobrica Pavlinusic <dpavlin@rot13.org>

use strict;
use WWW::Mechanize;

# URL to your page (http or https)
my $uri = shift @ARGV || "https://192.168.95.254:8443/Status.htm";
my $user = shift @ARGV || "admin";
my $pass = shift @ARGV || "";

# how to find data on your page this are case-INsensitive regexps for that
my $lan_regex = 'LAN.+?Send: (\d+).+?Receive: (\d+)';
my $wifi_regex = 'Wireless.+?Send: (\d+).+?Receive: (\d+)';

{
	package RequestAgent;
	no strict 'vars';
        @ISA = qw(WWW::Mechanize);

	sub new {
		my $self = WWW::Mechanize::new(@_);
		$self;
	}

	sub get_basic_credentials {
		return($user,$pass);
	}
}

my $mech = RequestAgent->new( 
	cookie_jar => undef
	);


$mech->get( $uri );
$mech->success or die "Can't fetch $uri\n", $mech->res->status_line, "\n";

$mech->is_html or die "$uri returns type \"", $mech->ct, "\", not \"text/html\"\n";

# get content
my $txt = $mech->content;	# get html
$txt =~ s#</*[^>]+>##g;	# remove tags
$txt =~ s#&nbsp;# #g;
$txt =~ s#\s+# #g;

#print "AP returned:\n$txt\n\n";

if ($txt =~ m/$lan_regex/s) {
	print "$1\n$2\n";
} else {
	print "U\nU\n";
}

if ($txt =~ m/$wifi_regex/s) {
	print "$1\n$2\n";
} else {
	print "U\nU\n";
}

