解析来自路由器

时间:2016-09-13 18:18:29

标签: python mechanize router

我想从我的路由器中删除一些家庭自动化的数据,但我遇到了一些麻烦,我无法解决/破解。

我已成功登录路由器,但在使用python脚本访问数据时(在路由器Web界面中打开链接)我收到一条错误消息:您无权访问此路由器!

如果我手动复制并粘贴python脚本正在访问浏览器的URL(设置了cookie),则响应是相同的。但是,如果我点击路由器网络界面内的按钮,我就得不到"权限"抱怨。任何想法如何解决这个问题?

这是脚本:

import re
import mechanize
import cookielib

br = mechanize.Browser()

cookies = cookielib.LWPCookieJar()
br.set_cookiejar(cookies)
#they "encrypt" the username and password and store it into the cookie. I stole this value from javascript in runtime.
br.addheaders = [('Cookie','Authorization=Basic YWRtaW46MjEyMzJmMjk3YTU3YTVhNzQzODk0YTBlNGE4MDFmYzM=;')] 
#open connection to the router address
br.open('http://192.168.1.1/')
#the only form is "login" form (which we dont have to fill up, because we already have the cookie)
br.select_form(nr=0) 
br.form.enctype = "application/x-www-form-urlencoded"
br.submit() 

#then the router returns redirect script, so we have to parse it (get the url).
redirect_url = re.search('(http:\/\/[^"]+)',br.response().read()).group(1)
token = re.search("1\/([A-Z]+)\/",redirect_url).group(1) #url always has a random token inside (some kind of security?)

#So with this url I should be able to navigate to page containing list of DHCP clients
br.open("http://192.168.1.1/"+token+"/userRpm/AssignedIpAddrListRpm.htm")

print(br.response().read()) #But response contains html saying "You have no authority to access this router!".

1 个答案:

答案 0 :(得分:1)

我已经通过添加以下内容解决了这个问题:

br.addheaders.append(
    ('Referer', "http://192.168.1.1/userRpm/LoginRpm.htm?Save=Save")
)

原因:

在网上搜索消息我导航到一个论坛,其中有firefox版本(旧)的用户抱怨同样的警告。修复是为了启用引用来发送,所以我在脚本中也做了同样的事情并且它已经完成了。