SHARE
TWEET

Huawei EchoLife HG8245H GPON ONT optical informations

a guest Mar 10th, 2016 319 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. Huawei EchoLife HG8245H GPON ONT optical informations
  6. V20160310001
  7.  
  8. Tested on: Xubuntu 14.04 + Python 2.7.6 + PyCurl 7.19.3
  9. """
  10.  
  11. import pycurl
  12. import argparse
  13. import os
  14. import re
  15. import hashlib
  16. try:
  17.     from io import BytesIO
  18. except ImportError:
  19.     from StringIO import StringIO as BytesIO
  20.  
  21. def dorequest(url,headers=[]):
  22.     c = pycurl.Curl()
  23.     buff = BytesIO()
  24.     hdr = BytesIO()
  25.     if args.verbose != False:
  26.         c.setopt(pycurl.VERBOSE, True)
  27.     if os.path.isfile('cookie.txt'):
  28.         c.setopt(pycurl.COOKIEFILE, 'cookie.txt')
  29.     c.setopt(pycurl.COOKIEJAR, 'cookie.txt')
  30.     if len(headers) > 0:
  31.         c.setopt(pycurl.HTTPHEADER, headers)
  32.     c.setopt(pycurl.URL, url)
  33.     try:
  34.         c.setopt(pycurl.WRITEFUNCTION, buff.write)
  35.         c.setopt(pycurl.HEADERFUNCTION, hdr.write)
  36.         c.perform()
  37.     except:
  38.         sys.exit('Ouch, an error occured, during connection to '+url+'!')
  39.     else:
  40.         status_code = c.getinfo(pycurl.HTTP_CODE)
  41.     finally:
  42.         c.close()
  43.     if str(status_code) == '200': #If we got good response
  44.         out = buff.getvalue().decode('utf-8')
  45.     else:
  46.         sys.exit('HTTP problem: ' + str(status_code))
  47.     return out
  48.    
  49. parser = argparse.ArgumentParser(description='Huawei EchoLife HG8245H GPON ONT optical informations')
  50. parser.add_argument('-u', '--user', required=True, type=str, help='User\'s e-mail address')
  51. parser.add_argument('-p', '--password', required=True, type=str, help='User\'s passwword')
  52. parser.add_argument('-v','--verbose', action='store_true', help='verbose output')
  53. args = parser.parse_args()
  54.  
  55. if os.path.isfile('cookie.txt'):
  56.     os.remove('cookie.txt')
  57. # 1. Login form
  58. page_loginform = dorequest('http://192.168.1.254/')
  59. cnt = str(re.findall('function GetRandCnt\(\) { return \'(.[^\']*)\'; }',page_loginform)[0])
  60. if args.verbose != False:
  61.     print (cnt)
  62.  
  63. if ((args.user != None) and (args.password != None)):
  64.     rid = hashlib.sha256(cnt).hexdigest() + hashlib.sha256(args.user + cnt ).hexdigest() + hashlib.sha256(hashlib.sha256(hashlib.md5(args.password).hexdigest()).hexdigest() + cnt).hexdigest()
  65.     # 2. Login page
  66.     page_login = dorequest('http://192.168.1.254/login.cgi', ['Cookie: Cookie=rid=' + rid + ':Language:english:id=-1; path=/', 'Referer: http://192.168.1.254/'])
  67.     if args.verbose != False:
  68.         print (page_login)
  69.     # 2. Index page
  70.     page_index = dorequest('http://192.168.1.254/index.asp',['Referer: http://192.168.1.254/login.asp'])
  71.     if args.verbose != False:
  72.         print (page_index)
  73.     # 3. Optical information
  74.     page_opticinfo = dorequest('http://192.168.1.254/html/status/opticinfo.asp',['Referer: http://192.168.1.254/index.asp'])
  75.     if args.verbose != False:
  76.         print (page_opticinfo)
  77.     opticinfos = re.findall('var opticInfos = new Array\(new stOpticInfo\(\"InternetGatewayDevice.X_HW_DEBUG.AMP.Optic\",\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\"\),null\);',page_opticinfo)
  78.     print ("TX optical power:\t" + str(opticinfos[0][0]) + ' dBm')
  79.     print ("RX optical power:\t" + str(opticinfos[0][1]) + ' dBm')
  80.     print ("Working voltage:\t" + str(opticinfos[0][2]) + ' mV')
  81.     print ("Bias current:\t\t" + str(opticinfos[0][4]) + ' mA')
  82.     print ("Working temperature:\t" + str(opticinfos[0][3]) + ' C')
  83. else:
  84.     sys.exit('Username and password needed!')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top