linux - Python - Huawei reboot script -


i found interesting article huawei mobile router: https://blog.hqcodeshop.fi/archives/259-huawei-e5186-ajax-api.html in second comment named rvl provided script auto reboot api if needed.

i tried fix indentation myself. here result http://pastebin.com/kqf5rss0 not sure if correct. not sure version of python should use 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 'router-reboot-script.py' (attributeerror: module 'router-reboot-script' has no attribute '__path__') 

i have no python skills. can me figure out how run it?

edit

[sabbath@dell ~]$ sudo pip install requests requirement satisfied (use --upgrade upgrade): requests in /usr/lib/python3.5/site-packages   using pip version 8.1.2, version 9.0.1 available.   should consider upgrading via '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: uninstalled pip-8.1.2 installed pip-9.0.1 [sabbath@dell ~]$ sudo pip install requests requirement satisfied: requests in /usr/lib/python3.5/site-packages [sabbath@dell ~]$ python -m router-reboot-script.py /usr/bin/python: error while finding spec '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 use, , kind of parameter (like -m) should use?

two issues:

  1. it's python 2.x, don't see from __future__ import print_function in python 3.
  2. there formatting issues in code in pastebin. python uses whitespace indicate blocks of code grouped functions, classes, etc. white space wasn't quite right. i've fixed below (see this pastebin)

code:

########################### #!/usr/bin/python  __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) line in html.splitlines())     return [m.group(1) 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 last 10 lines (apart empty line , #####) need updated own purposes. need set correct web, username , pass router. need uncomment 3 lines starting if __name__ == "__main__": i've done above.

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


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -