1

I found interesting article about Huawei mobile router: https://blog.hqcodeshop.fi/archives/259-Huawei-E5186-AJAX-API.html In second comment someone named rvl provided his script to auto reboot by API if needed.

I tried to fix the indentation by myself. Here is a result http://pastebin.com/KqF5RsS0 I am not sure if it is correct. Not even sure which version of Python should I use to run it.

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

or

[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__')

I have no Python skills. Can somebody help me figure it out how to run it?

edit

[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

Which version of python should I use, and what kind of parameter (like -m) should I use?

Community
  • 1
  • 1
sabbath
  • 11
  • 1
  • 3

1 Answers1

3

Two issues:

  1. It's python 2.x, you don't see from __future__ import print_function in python 3.
  2. There were some formatting issues in the code in your pastebin. Python uses whitespace to indicate which blocks of code are grouped together into functions, classes, etc. The white space wasn't quite right. I've fixed that below (see also this pastebin)

code:

###########################
#!/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)
#########################

Finally, note that the last 10 lines (apart from the empty line and the #####) need to be updated for your own purposes. You need to set the correct WEB, USERNAME and PASS for your router. Then you need to uncomment the 3 lines starting with if __name__ == "__main__": as i've done above.

If you're still getting errors because you're missing the requests package, check out the answer to this question.

Community
  • 1
  • 1
labradore
  • 313
  • 2
  • 7