Created
September 9, 2009 13:30
-
-
Save malcom/183709 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# | |
# adw4301a.pl - Prosta obsluga routera ADW-4301A firmy Planet | |
# http://projects.malcom.pl/scripts/adw4301a.xhtml | |
# | |
# Copyright (c) 2007 Marcin 'MalCom' Malich <me@malcom.pl> | |
# Licencja GPL | |
# | |
# 2007-08-30 17:23:22 | |
use strict; | |
use LWP::UserAgent; | |
my $routerip = '192.168.0.1'; | |
my $username = 'admin'; | |
my $password = ''; | |
my($m)=($0=~m,([^/\\]+)$,); | |
my $cmd = $ARGV[0] || '-cd'; | |
my ($ua, $req, $res); | |
# opcje wymagajace polaczenia z routerem | |
if ($cmd =~ /^-(c|d|st|cd|ad)$/) { | |
$ua = LWP::UserAgent->new; | |
$ua->credentials("$routerip:80", 'ADW-4301A', $username, $password); | |
$res = $ua->get("http://$routerip/"); | |
print STDERR "System Authentication Failed\n" and exit 1 | |
if ($res->code == 401); | |
check_res($res); | |
} | |
if ($cmd eq '-c') { | |
$res = $ua->post("http://$routerip/setup.cgi", { todo => 'connect' }); | |
check_res($res); | |
} elsif ($cmd eq '-d') { | |
$res = $ua->post("http://$routerip/setup.cgi", { todo => 'disconnect' }); | |
check_res($res); | |
} elsif ($cmd eq '-st') { | |
$res = $ua->get("http://$routerip/s_status.htm"); | |
check_res($res); | |
my $i=0; | |
my (@t) = $res->content =~ m!<th.*?>(.*?)</th>!sgi; | |
#('ADSL','Internet', 'LAN','Wireless', 'System'); | |
foreach ($res->content =~ m!<table.*?width="\d*%">(.*?)</table>!sgi) { | |
print "\n $t[$i++]\n"; | |
my $j=0; | |
foreach (m!<td.*?>(.*?)</td>!sgi) { | |
s/[\s:]+$//; # wywalenie bialych znakow i : z konca linii | |
next if (/^<input/); | |
if ($j++ % 2) { print "$_\n"; } | |
else { print sprintf(" %-30s", "$_:"); } | |
} | |
} | |
} elsif ($cmd eq '-cd') { | |
$res = $ua->get("http://$routerip/st_poe.htm"); | |
check_res($res); | |
print "\n"; | |
my $i=0; | |
foreach ($res->content =~ m!<td nowrap>(.*?)</td>!sgi) { | |
s/[\s:]+$//; | |
s/ +$//; | |
if ($i++ % 2) { print "$_\n"; } | |
else { print sprintf(" %-24s", "$_:"); } | |
} | |
} elsif ($cmd eq '-ad') { | |
$res = $ua->get("http://$routerip/devices.htm&todo=nbtscan"); | |
check_res($res); | |
print "\n #\tIP Address\tMAC Address\t\tDevice Name\n"; | |
my $i=0; | |
foreach ($res->content =~ m!<span class="thead">\d*</span></td>(.*?)</tr>!sgi) { | |
$i++; | |
my (@a) = m!<b>(.*?)</b>!gi; | |
print " $i\t$a[0]\t$a[2]\t$a[1]\n"; | |
} | |
} elsif ($cmd eq '-v' || $cmd eq '--version') { | |
print "$m version 0.4 (c) 2007 Malcom <me\@malcom.pl>\n"; | |
exit; | |
} else { | |
print <<EOH | |
Prosta obsluga routera ADW-4301A firmy Planet | |
$m [-c] [-d] [-st] [-cd] [-ad] | |
-c Connect ADSL | |
-d Disconnect ADSL | |
-st Status | |
-cd Connection Detalis | |
-ad Attached Devices | |
Strona projektu: | |
http://projects.malcom.pl/scripts/adw4301a.xhtml | |
EOH | |
} | |
sub check_res { | |
my $res = shift; | |
print STDERR 'Error: ' . $res->status_line ."\n" and exit 1 | |
if (!$res->is_success); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment