关于linux:Python – 华为重启脚本

Python - Huawei reboot script

我发现了一篇关于华为移动路由器的有趣文章:网址:https://blog.hqcodeshop.fi/archives/259-huawei-e5186-ajax-api.html在第二条注释中,一个名为rvl的人提供了自己的脚本,以便在需要时由api自动重启。

我试着自己修复凹痕。以下是一个结果http://pastebin.com/kqf5rss0我不确定是否正确。甚至不确定我应该使用哪个版本的python来运行它。

1
2
3
4
5
sabbath@dell ~> /usr/bin/python2 router-reboot-script.py
Traceback (most recent call last):
  File"router-reboot-script.py", line 6, in <module>
    import requests
ImportError: No module named requests

1
2
[sabbath@dell ~]$ python -m router-reboot-script.py
/usr/bin/python: Error while finding spec for 'router-reboot-script.py' (AttributeError: module 'router-reboot-script' has no attribute '__path__')

我没有Python技能。有人能帮我弄清楚怎么操作吗?

编辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[sabbath@dell ~]$ sudo pip install requests
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/lib/python3.5/site-packages  
You are using pip version 8.1.2, however version 9.0.1 is available.  
You should consider upgrading via the 'pip install --upgrade pip' command.  
[sabbath@dell ~]$ sudo pip install --upgrade pip
Collecting pip  
Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)  
100% |████████████████████████████████| 1.3MB 686kB/s
Installing collected packages: pip
Found existing installation: pip 8.1.2
Uninstalling pip-8.1.2:
Successfully uninstalled pip-8.1.2
Successfully installed pip-9.0.1
[sabbath@dell ~]$ sudo pip install requests
Requirement already satisfied: requests in /usr/lib/python3.5/site-packages
[sabbath@dell ~]$ python -m router-reboot-script.py
/usr/bin/python: Error while finding spec for 'router-reboot-script.py' (AttributeError: module 'router-reboot-script' has no attribute '__path__')
[sabbath@dell ~]$ python router-reboot-script.py

我应该使用哪个版本的python,应该使用什么类型的参数(如-m)?


两个问题:

  • 它是python 2.x,在python 3中看不到from __future__ import print_function
  • 您的Pastebin中的代码存在一些格式问题。python使用空白来指示哪些代码块被分组到函数、类等中。空白不太正确。我已经把它修好了(另请看这个贴纸盒)
  • 代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    ###########################
    #!/usr/bin/python

    from __future__ import print_function

    import requests
    import re
    import hashlib
    import base64


    def login(baseurl, username, password):
        s = requests.Session()
        r = s.get(baseurl +"html/index.html")
        csrf_tokens = grep_csrf(r.text)
        s.headers.update({'__RequestVerificationToken': csrf_tokens[0]})

        # test token on statistics api
        # r = s.get(baseurl +"api/monitoring/statistic-server")

        data = login_data(username, password, csrf_tokens[0])
        r = s.post(baseurl +"api/user/login", data=data)

        s.headers.update({'__RequestVerificationToken': r.headers["__RequestVerificationTokenone"]})
        return s


    def reboot(baseurl, session):
        s.post(baseurl +"api/device/control", data='1')


    def grep_csrf(html):
        pat = re.compile(r".*meta name="csrf_token" content="(.*)"", re.I)
        matches = (pat.match(line) for line in html.splitlines())
        return [m.group(1) for m in matches if m]


    def login_data(username, password, csrf_token):
        def encrypt(text):
            m = hashlib.sha256()
            m.update(text)
            return base64.b64encode(m.hexdigest())

        password_hash = encrypt(username + encrypt(password) + csrf_token)
        return '%s%s4' % (username, password_hash)


    WEB ="http://192.168.1.1/"
    USERNAME ="admin"
    PASSWORD ="admin"

    if __name__ =="__main__":
        s = login(WEB, USERNAME, PASSWORD)
    reboot(WEB, s)
    #########################

    最后,注意最后10行(除了空行和)需要根据您自己的目的进行更新。您需要为您的路由器设置正确的WEBUSERNAMEPASS。然后您需要取消对以if __name__ =="__main__":开头的3行的注释,正如我上面所做的。

    如果由于缺少请求包而仍然出现错误,请查看此问题的答案。