-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathont.py
102 lines (95 loc) · 3.61 KB
/
ont.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pycurl
import argparse
import os
import re
import sys
import hashlib
try:
from io import BytesIO
except ImportError:
from StringIO import StringIO as BytesIO
def dorequest(url, headers=[]):
c = pycurl.Curl()
buff = BytesIO()
hdr = BytesIO()
if not args.verbose:
c.setopt(pycurl.VERBOSE, True)
if os.path.isfile('cookie.txt'):
c.setopt(pycurl.COOKIEFILE, 'cookie.txt')
c.setopt(pycurl.COOKIEJAR, 'cookie.txt')
if len(headers) > 0:
c.setopt(pycurl.HTTPHEADER, headers)
c.setopt(pycurl.URL, url)
try:
c.setopt(pycurl.WRITEFUNCTION, buff.write)
c.setopt(pycurl.HEADERFUNCTION, hdr.write)
c.perform()
except:
sys.exit('Ouch, an error occured, during connection to ' + url + '!')
else:
status_code = c.getinfo(pycurl.HTTP_CODE)
finally:
c.close()
if str(status_code) == '200': # If we got good response
out = buff.getvalue().decode('utf-8')
else:
sys.exit('HTTP problem: ' + str(status_code))
return out
parser = argparse.ArgumentParser(description='Huawei EchoLife HG8245H GPON ONT\
optical informations')
parser.add_argument('-u',
'--user',
required=True,
type=str, help='User\'s e-mail address')
parser.add_argument('-p',
'--password',
required=True,
type=str, help='User\'s passwword')
parser.add_argument('-v',
'--verbose',
action='store_true',
help='verbose output')
args = parser.parse_args()
if os.path.isfile('cookie.txt'):
os.remove('cookie.txt')
# 1. Login form
page_loginform = dorequest('http://192.168.1.254/')
cnt = str(re.findall('function GetRandCnt\(\) { return \'(.[^\']*)\'; }',
page_loginform)[0])
if not args.verbose:
print(cnt)
if args.user is not None and args.password is not None:
user_hash = hashlib.sha256(args.user + cnt).hexdigest()
pw_hash = hashlib.sha256(hashlib.sha256(
hashlib.md5(args.password).hexdigest()).hexdigest() + cnt
).hexdigest()
rid = hashlib.sha256(cnt).hexdigest() + user_hash + pw_hash
# 2. Login page
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/'])
if not args.verbose:
print(page_login)
# 2. Index page
page_index = dorequest('http://192.168.1.254/index.asp',
['Referer: http://192.168.1.254/login.asp'])
if not args.verbose:
print(page_index)
# 3. Optical information
page_opticinfo = dorequest('http://192.168.1.254/html/status/opticinfo.asp',
['Referer: http://192.168.1.254/index.asp'])
if not args.verbose:
print(page_opticinfo)
opticinfos = re.findall('var opticInfos = new Array\(new stOpticInfo\(\"InternetGatewayDevice.X_HW_DEBUG.AMP.Optic\",\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\"\),null\);',
page_opticinfo)
print("TX optical power:\t" + str(opticinfos[0][0]) + ' dBm')
print("RX optical power:\t" + str(opticinfos[0][1]) + ' dBm')
print("Working voltage:\t" + str(opticinfos[0][2]) + ' mV')
print("Bias current:\t\t" + str(opticinfos[0][4]) + ' mA')
print("Working temperature:\t" + str(opticinfos[0][3]) + ' C')
else:
sys.exit('Username and password needed!')