使用Perl自动输入和提交webforms

时间:2013-10-08 22:27:10

标签: html forms perl http post

我的路由器的Web界面有一个改变mac addr的东西,并调用表单的名称 Macspoof你选择了一个单选按钮,说你要手动写入一个。然后一旦你改变它,你必须单击应用。我想知道如何用perl做这件事。

use LWP::UserAgent;        
use HTML::Form;         
use HTTP::Cookies;       
use Getopt::Std;         
use WWW::Mechanize;
use HTTP::Daemon;                                                               
use HTTP::Status;
use HTTP::Request::Common;


my $test = 'ad0c1f3e0b';



my $ua = LWP::UserAgent->new(
        'cookie_jar'    => HTTP::Cookies->new(
                'file'          => 'COOKIES',
                'autosave'      => 1,
        ),
);
  $ua->credentials(
     'ROUTER_IP:8080',
      'realm-name',
       'USER' => 'PASS'
);
my $response = $ua->get(
'ROUTER_IP' . 'DOCUMENT_NAME'
);
print $response;
 my $request = POST( $url, [ 'Spoofmac' => $test ] ); 
 my $content = $ua->request($request)->as_string(); 

我连接的内容不错,但不发布提交表单输入。

这是Web表单在txt文件中粘贴它并将其更改为html的内容 然后在浏览器中打开它以查看它的外观。感谢。

      <html><head>
 <meta name="description" content="WGR624">
 <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
 <meta http-equiv="Pragma" content="no-cache">
 <meta http-equiv="Cache-Control" content="no-cache">

 <meta http-equiv="Expires" content="Mon, 06 Jan 1990 00:00:01 GMT"> 

 <title>Ethernet</title>
 <link rel="stylesheet" href="BAS_basictop_data/form.txt">
 <style type="text/javascript">
classes.num.all.fontFamily = "Courier";
classes.num.all.fontSize = "10pt" ;
   </style>
      <script language="javascript" type="text/javascript"      src="MNU_menu_data/func.js"></script>
    <script language="javascript" type="text/javascript" src="MNU_blank_data/msg.js"></script>
    <script language="javascript" type="text/javascript" src="MNU_blank_data/utility.js"></script>
    <script language="javascript" type="text/javascript" src="MNU_blank_data/browser.js"></script>
    <script language="javascript" type="text/javascript">
    <!-- hide script from old browsers
 var MacAddress;
 var DisableFixedIP = false;
 var DisableFixedDNS = false;
 var DisableFixedMAC = false;

  function goTestApply()
   {
  var winoptions =   "width=640,height=480,menubar=yes,toolbar=yes,status=yes,location=yes,resizable=yes";
if(document.forms[0].runtest.value == "yes")
    window.open('BAS_wtest_d.htm','test_win',winoptions);
    }

    function loadcheck()
   {
loadhelp('BAS_ether');
if(isIE()){
    document.formname.system_name.size="24";
    document.formname.domain_name.size="24";
}
MacAddress=document.formname.Spoofmac.value;
setIP();
setDNS();
setMAC();
   }
   function setIP()
    {
var cf = document.forms[0];
var dflag = cf.WANAssign[0].checked;
 setDisabled(dflag,cf.WPethr1,cf.WPethr2,cf.WPethr3,cf.WPethr4,cf.WMask1,cf.WMask2,cf.WMask3,cf.WMask4,cf.WGateway1,cf.WGateway2,cf.WGateway3,cf.WGateway4);
if (cf.WANAssign[1].checked)
{
    cf.DNSAssign[1].checked = true;
    setDNS();
}
DisableFixedIP = dflag;
    }
    function setDNS()
    {
var cf = document.forms[0];
var dflag = cf.DNSAssign[0].checked;
if (cf.WANAssign[1].checked && cf.DNSAssign[0].checked)
{
    cf.DNSAssign[1].checked=true;
    dflag = false;
}
setDisabled(dflag,cf.DAddr1,cf.DAddr2,cf.DAddr3,cf.DAddr4,cf.PDAddr1,cf.PDAddr2,cf.PDAddr3,cf.PDAddr4);
DisableFixedDNS = dflag;
    }
   function setMAC()
   {
var cf = document.forms[0];
if (cf.MACAssign[2].checked)
    cf.Spoofmac.disabled = false;
else
    cf.Spoofmac.disabled = true;
DisableFixedMAC = cf.Spoofmac.disabled;
  }
  function loadhelp(fname,anchname) {

  if ((loadhelp.arguments.length == 1 ) || (anchname == "" ))
top.helpframe.location.href=fname+"_h.htm";
  else
top.helpframe.location.href=fname+"_h.htm#" + anchname;
   }

    function isValidStr(str)
    {
for (i=0; i<str.length; i++)
{
    var c = str.substring(i, i+1);
    if (c < '!' || c > '~')
        return false;
}
return true;
    }

  function checkData()
  {
var cf = document.forms[0];
var msg = "";
if(cf.WANAssign[1].checked)
{
    if(checkIP(cf.WPethr1,cf.WPethr2,cf.WPethr3,cf.WPethr4,254)||(parseInt(cf.WPethr4.value)==0))
        msg+= "Invalid IP Address, please enter again!\n";
    if(checkIP(cf.WMask1,cf.WMask2,cf.WMask3,cf.WMask4,255))
        msg+= "Invalid Subnet Mask, please enter again!\n";
    if(checkIP(cf.WGateway1,cf.WGateway2,cf.WGateway3,cf.WGateway4,254)||(parseInt(cf.WGateway4.value)==0))
        msg+= "Invalid Gateway IP Address, please enter again!\n";
}
if(cf.DNSAssign[1].checked)
{
    if(checkIP(cf.DAddr1,cf.DAddr2,cf.DAddr3,cf.DAddr4,254)||(parseInt(cf.DAddr4.value)==0))
        msg+= "Invalid Primary DNS Address, please enter again!\n";
    if(cf.PDAddr1.value.length>0 || cf.PDAddr2.value.length>0 || cf.PDAddr3.value.length>0 || cf.PDAddr4.value.length>0)
        if(checkIP(cf.PDAddr1,cf.PDAddr2,cf.PDAddr3,cf.PDAddr4,254)||(parseInt(cf.PDAddr4.value)==0))
            msg+= "Invalid Secondary DNS Address, please enter again!\n";
}
if (msg.length > 1)
{
    alert(msg);
    return false;
}
if (!isValidStr(cf.system_name.value) || !isValidStr(cf.domain_name.value))
{
    if (!confirm("The Account Name or Domain Name contains illegal characters. This may cause the system to behave unexpectedly. Do you want to continue?"))
        return false;
}
cf.wan_ipaddr.value = cf.WPethr1.value+'.'+cf.WPethr2.value+'.'+cf.WPethr3.value+'.'+cf.WPethr4.value;
cf.wan_netmask.value = cf.WMask1.value+'.'+cf.WMask2.value+'.'+cf.WMask3.value+'.'+cf.WMask4.value;
cf.wan_gateway.value = cf.WGateway1.value+'.'+cf.WGateway2.value+'.'+cf.WGateway3.value+'.'+cf.WGateway4.value;
cf.wan_gateway.value = cf.WGateway1.value+'.'+cf.WGateway2.value+'.'+cf.WGateway3.value+'.'+cf.WGateway4.value;
cf.wan_dns1_pri.value = cf.DAddr1.value+'.'+cf.DAddr2.value+'.'+cf.DAddr3.value+'.'+cf.DAddr4.value;
cf.wan_dns1_sec.value = cf.PDAddr1.value+'.'+cf.PDAddr2.value+'.'+cf.PDAddr3.value+'.'+cf.PDAddr4.value;
cf.wan_hwaddr2.value = cf.Spoofmac.value;            
return sumvalue(cf);
  }

 function loadSettings()
  {
var cf = document.forms[0];
var wan_proto = cf.wan_proto.value;
var wan_ipaddr = cf.wan_ipaddr.value.split(".");
var wan_netmask = cf.wan_netmask.value.split(".");
var wan_gateway = cf.wan_gateway.value.split(".");
var wan_dns_sel = cf.wan_dns_sel.value;
var wan_dns1_pri = cf.wan_dns1_pri.value.split(".");
var wan_dns1_sec = cf.wan_dns1_sec.value.split(".");
var wan_hwaddr_sel = cf.wan_hwaddr_sel.value;

if (wan_proto == "static")
    cf.WANAssign[1].checked = true;
else
    cf.WANAssign[0].checked = true;
cf.WPethr1.value = wan_ipaddr[0];
cf.WPethr2.value = wan_ipaddr[1];
cf.WPethr3.value = wan_ipaddr[2];
cf.WPethr4.value = wan_ipaddr[3];
cf.WMask1.value = wan_netmask[0];
cf.WMask2.value = wan_netmask[1];
cf.WMask3.value = wan_netmask[2];
cf.WMask4.value = wan_netmask[3];
cf.WGateway1.value = wan_gateway[0];
cf.WGateway2.value = wan_gateway[1];
cf.WGateway3.value = wan_gateway[2];
cf.WGateway4.value = wan_gateway[3];

cf.DNSAssign[wan_dns_sel].checked = true;
if (cf.wan_dns1_pri.value != "")
{
    cf.DAddr1.value = wan_dns1_pri[0];
    cf.DAddr2.value = wan_dns1_pri[1];
    cf.DAddr3.value = wan_dns1_pri[2];
    cf.DAddr4.value = wan_dns1_pri[3];
}
if (cf.wan_dns1_sec.value != "")
{
    cf.PDAddr1.value = wan_dns1_sec[0];
    cf.PDAddr2.value = wan_dns1_sec[1];
    cf.PDAddr3.value = wan_dns1_sec[2];
    cf.PDAddr4.value = wan_dns1_sec[3];
  }

cf.MACAssign[wan_hwaddr_sel].checked = true;
 }

  //-->
  </script>
  </head>
  <body onload="loadSettings(); loadcheck(); document.forms[0].elements[0].focus();goTestApply();" bgcolor="#ffffff">
  <form name="formname" method="POST" action="ether.cgi" target="_parent"> 
  <table border="0" cellpadding="0" cellspacing="3" width="100%">
  <tbody><tr>
<td nowrap="nowrap"><a href="javascript:loadhelp('BAS_ether','accountname')" tabindex="-1"><b>Account Name</b></a> &nbsp;(If Required)</td>
<td align="right"><input name="system_name" maxlength="50" size="15" value="WGT624v3" tabindex="3" type="text">    </td>
    </tr>
    <tr>
<td nowrap="nowrap"><a href="javascript:loadhelp('BAS_ether','Domain')" tabindex="-1"><b>Domain Name</b></a> &nbsp;(If Required)    </td>
<td align="right"><input name="domain_name" maxlength="50" size="15" tabindex="4" type="text">     </td>
 </tr>
 <tr> <!-- RULE -->
 <td colspan="2" background="BAS_basictop_data/liteblue.gif" height="12"> &nbsp;</td>
  </tr>
   <tr>
<td colspan="2" nowrap="nowrap"><a href="javascript:loadhelp('BAS_ether','InternetIP')" tabindex="-1"><b>Internet IP Address</b></a></td>
   </tr>
   <tr>
<td colspan="2" nowrap="nowrap"><input checked="checked" name="WANAssign" value="dhcp" tabindex="5" onclick="setIP()" type="radio">Get Dynamically From ISP</td>
    </tr>
    <tr>
<td colspan="2" nowrap="nowrap"><input name="WANAssign" value="static" tabindex="6" onclick="setIP()" type="radio">Use Static IP Address</td>
  </tr>
  <tr>
<td nowrap="nowrap"><img src="MNU_menu_data/spacer.gif" border="0" height="12" width="20">IP Address</td>
<td class="num" align="right" nowrap="nowrap"><input value="71" disabled="disabled" name="WPethr1" class="num" size="4" maxlength="3" tabindex="7" onfocus="if(DisableFixedIP) this.blur()" type="text">.<input value="235" disabled="disabled" name="WPethr2" class="num" size="4" maxlength="3" tabindex="8" onfocus="if(DisableFixedIP) this.blur()" type="text">.<input value="183" disabled="disabled" name="WPethr3" class="num" size="4" maxlength="3" tabindex="9" onfocus="if(DisableFixedIP) this.blur()" type="text">.<input value="87" disabled="disabled" name="WPethr4" class="num" size="4" maxlength="3" tabindex="10" onfocus="if(DisableFixedIP) this.blur()" type="text"></td>
    </tr>
    <tr>
<td nowrap="nowrap"><img src="MNU_menu_data/spacer.gif" border="0" height="12" width="20">IP Subnet Mask</td>
<td class="num" align="right"><input value="255" disabled="disabled" name="WMask1" class="num" size="4" maxlength="3" tabindex="11" onfocus="if(DisableFixedIP) this.blur()" type="text">.<input value="255" disabled="disabled" name="WMask2" class="num" size="4" maxlength="3" tabindex="12" onfocus="if(DisableFixedIP) this.blur()" type="text">.<input value="254" disabled="disabled" name="WMask3" class="num" size="4" maxlength="3" tabindex="13" onfocus="if(DisableFixedIP) this.blur()" type="text">.<input value="0" disabled="disabled" name="WMask4" class="num" size="4" maxlength="3" tabindex="14" onfocus="if(DisableFixedIP) this.blur()" type="text"></td>
    </tr>
    <tr>
<td nowrap="nowrap"><img src="MNU_menu_data/spacer.gif" border="0" height="12" width="20">Gateway IP Address</td>
<td class="num" align="right"><input value="71" disabled="disabled" name="WGateway1" class="num" size="4" maxlength="3" tabindex="15" onfocus="if(DisableFixedIP) this.blur()" type="text">.<input value="235" disabled="disabled" name="WGateway2" class="num" size="4" maxlength="3" tabindex="16" onfocus="if(DisableFixedIP) this.blur()" type="text">.<input value="182" disabled="disabled" name="WGateway3" class="num" size="4" maxlength="3" tabindex="17" onfocus="if(DisableFixedIP) this.blur()" type="text">.<input value="1" disabled="disabled" name="WGateway4" class="num" size="4" maxlength="3" tabindex="18" onfocus="if(DisableFixedIP) this.blur()" type="text"></td>
   </tr>
   <tr> <!-- RULE -->
   <td colspan="2" background="BAS_basictop_data/liteblue.gif" height="12"> &nbsp;</td>
    </tr>
    <tr>
<td colspan="2" nowrap="nowrap"><a href="javascript:loadhelp('BAS_ether','DNSaddress')" tabindex="-1"><b>Domain Name Server (DNS) Address </b></a></td>
    </tr>
    <tr>
<td colspan="2" nowrap="nowrap"><input name="DNSAssign" value="0" tabindex="19" onclick="setDNS()" type="radio">Get Automatically From ISP</td>
    </tr>
    <tr>
<td colspan="2" nowrap="nowrap"><input checked="checked" name="DNSAssign" value="1" tabindex="20" onclick="setDNS()" type="radio">Use These DNS Servers</td>
    </tr>
    <tr>
<td nowrap="nowrap"><img src="MNU_menu_data/spacer.gif" border="0" height="12" width="20">Primary DNS</td>
<td class="num" align="right" nowrap="nowrap"><input value="208" name="DAddr1" class="num" size="4" maxlength="3" tabindex="21" onfocus="if(DisableFixedDNS) this.blur()" type="text">.<input value="67" name="DAddr2" class="num" size="4" maxlength="3" tabindex="22" onfocus="if(DisableFixedDNS) this.blur()" type="text">.<input value="222" name="DAddr3" class="num" size="4" maxlength="3" tabindex="23" onfocus="if(DisableFixedDNS) this.blur()" type="text">.<input value="222" name="DAddr4" class="num" size="4" maxlength="3" tabindex="24" onfocus="if(DisableFixedDNS) this.blur()" type="text"></td>
 </tr>
 <tr>
<td nowrap="nowrap"><img src="MNU_menu_data/spacer.gif" border="0" height="12" width="20">Secondary DNS</td>
<td class="num" align="right" nowrap="nowrap"><input value="208" name="PDAddr1" class="num" size="4" maxlength="3" tabindex="25" onfocus="if(DisableFixedDNS) this.blur()" type="text">.<input value="67" name="PDAddr2" class="num" size="4" maxlength="3" tabindex="26" onfocus="if(DisableFixedDNS) this.blur()" type="text">.<input value="220" name="PDAddr3" class="num" size="4" maxlength="3" tabindex="27" onfocus="if(DisableFixedDNS) this.blur()" type="text">.<input value="220" name="PDAddr4" class="num" size="4" maxlength="3" tabindex="28" onfocus="if(DisableFixedDNS) this.blur()" type="text"></td>
   </tr>
   <tr> <!-- RULE -->
    <td colspan="2" background="BAS_basictop_data/liteblue.gif" height="12"> &nbsp;     </td>
     </tr>
    <tr>
 <td colspan="2" nowrap="nowrap"><a  href="javascript:loadhelp('BAS_ether','localaddress')" tabindex="-1"><b>Router MAC  Address</b></a></td>
   </tr>
   <tr>
<td colspan="2" nowrap="nowrap"><input checked="checked" name="MACAssign" value="0" tabindex="29" onclick="setMAC()" type="radio">Use Default Address     </td>
    </tr>
    <tr>
<td colspan="2" nowrap="nowrap"><input name="MACAssign" value="1" tabindex="29" onclick="setMAC()" type="radio">Use Computer MAC Address      </td>
    </tr>
    <tr>
 <td align="left" nowrap="nowrap"><input name="MACAssign" value="2" tabindex="29"  onclick="setMAC()" type="radio">Use This MAC Address    </td>
<td class="num" align="right" nowrap="nowrap"><input disabled="disabled" class="num" name="Spoofmac" size="17" maxlength="17" value="FA:CE:B0:0B:13:37" tabindex="30" onfocus="if(DisableFixedMAC) this.blur()" type="text">
</td>
   </tr>

   <tr>     <!-- RULE -->
  <td colspan="2" background="BAS_basictop_data/liteblue.gif" height="12"> &nbsp;</td>
   </tr>

   <tr>
<td colspan="2" align="center"><div id="basbuttons" onmouseover="loadhelp('BAS_ether','buttons')">
<input name="apply" value="Apply" onclick="return checkData()" tabindex="31" type="SUBMIT">
<input name="Cancel" value="Cancel" onclick="reset();loadSettings();loadcheck();"  tabindex="32" type="BUTTON">
<input name="Test" value="Test" onclick="return checkData()" tabindex="33" type="SUBMIT"></div>
</td></tr>
  </tbody></table>
 <input name="runtest" value="no" type="hidden">
 <input name="wan_proto" value="dhcp" type="hidden">
 <input name="wan_ipaddr" value="xxx.xxx.xxx.xxx" type="hidden">
 <input name="wan_netmask" value="255.255.254.0" type="hidden">
 <input name="wan_gateway" value="xxx.xxx.xxx.xxx" type="hidden">
 <input name="wan_dns_sel" value="1" type="hidden">
 <input name="wan_dns1_pri" value="208.67.222.222" type="hidden">
 <input name="wan_dns1_sec" value="208.67.220.220" type="hidden">
 <input name="wan_hwaddr_sel" value="0" type="hidden">
 <input name="wan_hwaddr2" type="hidden">
 </form>
 <p></p>


 </body></html>         

1 个答案:

答案 0 :(得分:0)

页面有javascript代码,我认为最好通过像FF这样的网页浏览器来机械化页面,所以你可以使用WWW::Mechanize::Firefox模块。