我想把路由器上的数据丢弃,以实现一些家庭自动化,但我遇到了一些无法解决/破解的问题。
我已经成功地登录到路由器,但是当使用python脚本(在路由器web界面中打开链接)访问数据时,我收到一个错误消息:您无权访问此路由器!
如果我手动将python脚本正在访问的url复制并粘贴到浏览器中(设置了cookies),则响应是相同的。但如果我点击路由器网络界面内的按钮,我就不会得到“权威”的投诉。有什么办法解决这个问题吗?
这是剧本:
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!".
最佳答案
我通过添加以下内容解决了这个问题:
br.addheaders.append(
('Referer', "http://192.168.1.1/userRpm/LoginRpm.htm?Save=Save")
)
原因:
搜索网页上的信息,我导航到一个论坛,在那里使用firefox版本(旧版)的用户抱怨同样的警告。修正是为了让推荐人被发送,所以我在脚本中也做了同样的操作,结果成功了。
关于python - 从路由器解析数据时出现授权错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39476591/