use strict;
use LWP;
use File::Basename;
use warnings;
use constant false => 0;
use constant true => 1;
my $host = "192.168.1.1";
my $prot = "https://";
my ($user, $pwd) = ("admin", "1234");
my @pathes = qw(/status_quickview.cgi);
my $ua = LWP::UserAgent->new();
$ua->cookie_jar({ file => "$ENV{HOME}/.cookies.txt", autosave=>1 });
push @{ $ua->requests_redirectable}, 'POST';
$ua->show_progress(true);
if ( is_login()==false ) { do_login(); }
foreach my $p (@pathes) {
do_get($p);
}
do_logout();
sub do_post {
my ($path, $query) = @_;
print "do_post($path)\n";
my $url = $prot . $host . $path;
my $req = HTTP::Request->new(POST => $url);
$req->content_type('application/x-www-form-urlencoded');
$req->content($query);
# print $req->as_string(),"\n";
my $resp = $ua->request($req);
if ( $resp->is_error() ) {
print $resp->status_line(), "\n";
exit();
}
# print $resp->content, "\n"; return $resp;
}
sub do_get {
my ($fullpath) = @_;
my ($name, $path, $suffix) = fileparse($fullpath);
print "do_get($fullpath)\n";
my $url = $prot . $host . $fullpath;
my $req = HTTP::Request->new(GET => $url);
# my $resp = $ua->request($req, $name);
my $resp = $ua->request($req);
if ( $resp->is_error() ) {
print $resp->status_line(), "\n";
exit();
}
# print $resp->content(), "\n";
return $resp;
}
sub do_login {
print "do_login()\n";
my $path = "/login_handler.cgi";
my $query = sprintf("username=%s&password=%s&Signin=Login", $user, $pwd);
my $resp = do_post($path, $query);
if ( 1 and $resp->content=~m/type=(\w+)/ ) {
# print "type=$1\n";
$ua->cookie_jar->set_cookie(0, 'type', $1, '/', $host);
$ua->cookie_jar->save();
}
}
sub is_login {
print "is_login()\n";
my $path = "/status_quickview.cgi";
my $resp = do_get($path);
if ( index($resp->content, "login.cgi")!=-1 ) {
return false;
} else {
return true;
}
}
sub do_logout {
print "do_logout()\n";
my $path = "/tools_logout.cgi";
my $resp = do_post($path, "apply=");
# print $resp->content, "\n";
}
No comments:
Post a Comment