简体   繁体   English

如何使用Perl自动登录和备份

[英]How to automate login and backing up with Perl

I'm trying to login into the web configuration panel of a device (for the test I'm using my router) and create backup. 我正在尝试登录设备的Web配置面板(以测试我正在使用路由器),并创建备份。 The module is WWW::Scripter. 该模块是WWW :: Scripter。 The page is using JavaScript. 该页面正在使用JavaScript。 I think that I'm not passing the parameters properly because it returns error : 我认为我没有正确传递参数,因为它返回错误:

No such field 'userName' at /home/angel/perl5/lib/perl5/WWW/Mechanize.pm line 750.

JS function on the web page: 网页上的JS功能:

function CheckUserPswInvalid()
{
var userName = $("userName");
var pcPassword = $("pcPassword");   
if(!CheckUserPswChars(userName.value))
{
    userName.select();
    userName.focus();
    return false;   
}

if(!CheckUserPswChars(pcPassword.value))
{
    pcPassword.select();
    pcPassword.focus();
    return false;   
}

return true;
}

My code: 我的代码:

use WWW::Scripter;
use HTTP::Cookies;
my $url  = 'http://192.168.0.1/';
my $username = 'username';
my $pass = 'password';
my $agent = new WWW::Scripter;

$agent->use_plugin('JavaScript');
$agent->get('http://192.168.0.1/');
$agent->cookie_jar(HTTP::Cookies->new);
$agent->form_name("loginForm");
$agent->field('userName' => $username);
$agent->field("pcPassword" => $pass);
$agent->submit();

EDIT: Adding part of the HTML: 编辑:添加HTML的一部分:

` <div class="loginBox">    
    <div class="noteDiv">
        <span id="note"></span>
        <span id="tip"></span>
    </div>
    <div class="panelThre" align="center">
        <div align="center" class="picDiv" align="center">
            <ul>
                <li id="unLi" class="unLi"><input class="text" id="userName" type="text" maxlength="15" /></li>
                <li class="blank"></li>
                <li id="pwLi" class="pwLi"><input class="text" id="pcPassword" type="password" maxlength="15"/></li>
            </ul>
            <label id="loginBtn" class="loginBtn" onclick="PCSubWin()"/></label>
            <div>
            <label id="copyright">Copyright &copy; 2016 TP-LINK Technologies Co., Ltd. All rights reserved. </label>            
            </div>
        </div>
    </div>
</div>
<form action="/userRpm/LoginRpm.htm" method="get" id="loginForm" enctype="multipart/form-data">
    <input type="hidden" value="Save" name="Save" />
</form>

` `

I'm pretty sure WWW::Mechanize wouldn't work for this: 我很确定WWW :: Mechanize对此不起作用:

  1. There's no form (no <form> tag in HTML); 没有表单(HTML中没有<form>标记);
  2. The input field doesn't have a name and $agent->field takes field name as a parameter, rather than ID. 输入字段没有名称,而$agent->field将字段名称作为参数而不是ID。

JavaScript probably issues an Ajax request. JavaScript可能会发出Ajax请求。 I suggest that you find the Ajax request in your browser development console and replay it (you don't need WWW::Mechanize for that, LWP::UserAgent should do). 我建议您在浏览器开发控制台中找到Ajax请求并重播(不需要WWW::MechanizeLWP::UserAgent应该这样做)。 HTH. HTH。

Try the DOM and submitting via callback: 尝试DOM并通过回调提交:

$agent->document->getElementById('userName')->value($username);
$agent->document->getElementById('pcPassword')->value($pass);
$agent->eval('PCSubWin()');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM