微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用FritzConnection更改WIFI密码

如何解决使用FritzConnection更改WIFI密码

我想在FritzBox路由器上更改访客WIFI的密码。为了连接到FritzBox,我使用Python中的FritzConnection库。通常,FritzConnection可以工作:我可以从FritzBox中读取信息,也可以启用或禁用访客WIFI。但是,我在更改密码时遇到麻烦。我可以读取密码。

这是我到目前为止所做的:

#!/usr/bin/python3
from fritzconnection import FritzConnection
import pprint

pp = pprint.PrettyPrinter(indent=2)
fc=FritzConnection(address='192.168.2.1',password='XYZ',use_tls=True)

#switch guest WIFI off and on again - this works
state = fc.call_action('WLANConfiguration:3','GetInfo')
pp.pprint(state)
print("disable WLAN")
fc.call_action('WLANConfiguration3','SetEnable',arguments={'NewEnable':0})
state = fc.call_action('WLANConfiguration:3','GetInfo')
pp.pprint(state)
input("Press Enter to continue...")
print("Enable WLAN")
fc.call_action('WLANConfiguration3',arguments=NewEnable=1)
state = fc.call_action('WLANConfiguration:3','GetInfo')
pp.pprint(state)

代码段可打开和关闭WIFI,效果很好。请注意,我使用了2种不同的参数传递方法来启用和禁用。两者都可以。

现在,在更改密码时,我首先读取了安全密钥(有效),然后尝试重新设置安全密钥(无效)。为了阅读,我做以下事情:

securityKeys = fc.call_action('WLANConfiguration:3','GetSecurityKeys')
pp.pprint(securityKeys)

我首先尝试写作:

fc.call_action('WLANConfiguration:3','SetSecurityKeys',NewKeyPassphrase='BetterPassword')

这导致

fritzconnection.core.exceptions.FritzArgumentError: UPnPError:
errorCode: 402
errorDescription: Invalid Args

我也尝试过

fc.call_action('WLANConfiguration3',arguments={'NewKeyPassphrase':'MeinPasswortIstBesser'})

fc.call_action('WLANConfiguration:3',NewWEPKey0='',NewWEPKey1='',NewWEPKey2='',NewWEPKey3='',NewPreSharedKey='',NewKeyPassphrase="MeinPasswortIstBesser")

securityKeys['NewKeyPassphrase']='MeinPasswortIstBesser'
fc.call_action('WLANConfiguration3',arguments=securityKeys)

,但结果始终相同。显然我对参数做错了,但是我不能说应该是什么。使用FritzConnection CLI工具会告诉我该界面:

fritzconnection --ip-address 192.168.2.1 -A 'WLANConfiguration3' 'SetSecurityKeys'

fritzconnection v1.3.4
FRITZ!Box 7590 at http://192.168.2.1
FRITZ!OS: 7.20


Service:            WLANConfiguration3
Action:             SetSecurityKeys
Parameters:

    Name                                  direction     data type

    NewWEPKey0                            -> in         string
    NewWEPKey1                            -> in         string
    NewWEPKey2                            -> in         string
    NewWEPKey3                            -> in         string
    NewPreSharedKey                       -> in         string
    NewKeyPassphrase                      -> in         string

据我了解,我为该界面提供服务。

对我在这里做错的任何想法吗?

解决方法

FritzConnction 存在缺陷。解决方法可以在这里找到:

https://github.com/kbr/fritzconnection/issues/66

本质是FritzConnection的模板太多了 s: 前缀。特别是 s:NewWEPKey0s:NewWEPKey1s:NewWEPKey2s:NewWEPKey3s:NewPreSharedKeys:NewKeyPassphrase。从正文中删除 s: 即可解决问题。

我做了以下肮脏的黑客来验证这个论文: 在 soaper.py 中,我在第 226 行下方添加了(这是在方法 execute() 中,以下代码片段:

    if 'NewKeyPassphrase' in body:
        print (body)
        print ('replacing......................................')
        body = body.replace('s:NewWEPKey0','NewWEPKey0')
        body = body.replace('s:NewWEPKey1','NewWEPKey1')
        body = body.replace('s:NewWEPKey2','NewWEPKey2')
        body = body.replace('s:NewWEPKey3','NewWEPKey3')
        body = body.replace('s:NewPreSharedKey','NewPreSharedKey')
        body = body.replace('s:NewKeyPassphrase','NewKeyPassphrase')
        print (body)

在那种情况下,这会从那个正文中删除 s:。这工作正常,密码已更改。

所以我的脚本如下所示:

from fritzconnection import FritzConnection
import pprint

pp = pprint.PrettyPrinter(indent=2)
fc=FritzConnection(address='192.168.2.1',user='FritzConnection',password='Dr3QL78ZYG58Nn98GVsx',use_tls=True)

securityKeys = fc.call_action('WLANConfiguration:3','GetSecurityKeys')
pp.pprint(securityKeys)

securityKeys['NewKeyPassphrase']='MeinPasswortIstUnfassbarGut'
pp.pprint(securityKeys)
fc.call_action('WLANConfiguration3','SetSecurityKeys',arguments=securityKeys)

securityKeys = fc.call_action('WLANConfiguration:3','GetSecurityKeys')
pp.pprint(securityKeys)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?