2015-12-21 71 views
2

我目前正在研究一個python腳本,它需要我在本地路由器的DHCP服務器上獲取當前客戶端的列表。我首先想到的是如何使用requests來獲取包含列表(http://192.168.2.1/lan_dhcp.stm)的網頁,然後解析數據並使用它。使用Python獲取DHCP客戶端列表

問題是,有時,路由器的Web界面會要求我登錄,即使沒有設置密碼,您只需點擊提交。我如何通過Python發送請求登錄(http://192.168.2.1/login.htm)以保證我能夠訪問客戶端列表?有沒有更好的方法來做到這一點? The login screen with a look at the network login.cgi:網頁

<form action="login.cgi" method="post" name="tF" onsubmit="checkfwVersion()"> 
    <input type="hidden" name="totalMSec" value=""> 
    <input type="hidden" name="pws" value=""> 
    <input type="hidden" name="arc_action" value="login"> 
    <table border="0" cellspacing="0" cellpadding="0" align="left" width="100%" height="100%"> 
     <tbody> 
      <tr> 
       <td valign="top" width="99%" height="100%" class="head_bold"> 
        <br> 
        <font color="#0000ff">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Login</font> 
        <br> 
        <!--This is Login.stm--> 
        <table border="0" class="copy_1"> 
         <tbody> 
          <tr> 
           <td width="5" colspan="2">&nbsp;</td> 
          </tr> 
          <tr> 
           <td width="5">&nbsp;</td> 
           <td> 
            <table width="480" border="0" cellspacing="5" cellpadding="0" align="left" class="copy_1"> 
             <tbody> 
              <tr> 
               <td width="69" height="41">&nbsp;</td> 
               <td height="41" class="body" colspan="2"> 
                Before you can change any settings, you need to login with a password. If you have 
                not yet set a custom password, 
                then leave this field blank and 
                click "Submit." 
               </td> 
              </tr> 
              <tr> 
               <td colspan="3">&nbsp;</td> 
              </tr> 
              <tr> 
               <td width="69">&nbsp;</td> 
               <td width="146" class="body"><b>Password</b> 
               </td> 
               <td width="250"> 
                <input type="password" maxlength="12" size="16" name="pws_temp"> 
               </td> 
              </tr> 
              <tr> 
               <td width="69" class="body">&nbsp;</td> 
               <td colspan="2"> 
                <font color="#6F4AFD"><b>Default = leave blank</b></font> 
               </td> 
              </tr> 
              <tr> 
               <td width="69" colspan="3">&nbsp;</td> 
              </tr> 
              <tr> 
               <td width="69">&nbsp;</td> 
               <td colspan="2" align="center"> 
                <nobr> 
                 <input type="button" onclick="javascript:document.tF.reset();" value="Clear" style="{width=120px;height=22px;}" 
                 class="submitBtn" onmouseover="window.status='Clear'; return true;" 
                 onmouseout="window.status=''; return true;"> &nbsp;&nbsp; 
                 <input type="submit" name="action" value="Submit" style="{width=120px;height=22px;}" 
                 class="submitBtn"> 
                </nobr> 
               </td> 


              </tr> 
             </tbody> 
            </table> 
           </td> 
          </tr> 
         </tbody> 
        </table> 
       </td> 
      </tr> 
     </tbody> 
    </table> 
</form> 

回答

1

我會切換到真正的瀏覽器自動化和selenium。除了常規瀏覽器FirefoxChrome之外,您還可以控制無頭瀏覽器,如PhantomJS

這裏是完整的登錄代碼,讓你開始。需要注意的是,根據頁面的源代碼,登錄表單本身是一個iframe裏面 - 你將不得不切換到它的上下文爲了找到形式的元素:

from selenium import webdriver 

password = "My password" 

driver = webdriver.PhantomJS() 
driver.get("http://192.168.2.1/login.htm") 

# switching to the main frame on the page  
driver.switch_to.frame("mainFrame") 

password = driver.find_element_by_name("pws_temp") 
password.send_keys(password) 
password.submit() # this would find the corresponding form and submit it 

# or you can also submit the form this way: password.send_keys(password + "\n") 
3

它看起來的

<html><head><META http-equiv="Content-Type" content="text/html; charset=UTF-8" ></head><script> 
             function reload(){          top.topFrame.location.href=top.topFrame.location.href;          document.location.href="lan_dhcp.stm";} 
setTimeout("reload()", 900); 
</script></html> 

form部分像有JavaScript代碼在瀏覽器中執行一些客戶端 - 看你pws_temp密碼值是如何轉化成pws加密值:

enter image description here

requests是不是一個瀏覽器,並沒有內置的JavaScript引擎。

+0

嘿@alecxe,遺憾的遲到的答案。我試着用[** this **](https://gist.github.com/Arengorn/98a540fa8081d7453677)這樣的方法,但是我得到了下面的[**錯誤**](https:// gist。 github.com/Arengorn/f0a571356d0610f80b7e)。我究竟做錯了什麼? –

+0

[** ghostdriver.log **](https://gist.github.com/Arengorn/c70b265b81d33110386d) –

+0

@BernardMeurer可能是多種原因。您能否使用登錄表單完成頁面的完整HTML?謝謝! – alecxe