-
Notifications
You must be signed in to change notification settings - Fork 0
/
getToken.py
57 lines (44 loc) · 1.56 KB
/
getToken.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
#!/usr/bin/env python3
import http.client
import argparse, json
def parse_args():
parser = argparse.ArgumentParser(description =
"""
Generare a Zabbix server authorization token.
""")
parser.add_argument('-s', '--server', action = "store", required = True,
help = "The Zabbix server's IP address")
parser.add_argument('-P', '--port', action = "store",
help = "The Zabbix server's listening port. Default: %(default)s",
default = 80)
parser.add_argument('-u', '--user', action = "store", required = True,
help = "The admin's username")
parser.add_argument('-p', '--password', action = "store", required = True,
help = "The admin's password")
parser.add_argument('-t', '--tls', action = 'store_true',
help = "Connect over HTTPS")
return parser.parse_args()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~ MAIN ~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
args = parse_args()
connClass = http.client.HTTPSConnection if args.tls else http.client.HTTPConnection
conn = connClass(args.server, args.port)
payload = '''{{
"jsonrpc": "2.0",
"method": "user.login",
"params": {{
"username": "{}",
"password": "{}"
}},
"id": 1,
"auth": null
}}'''.format(args.user, args.password)
headers = {
'Content-Type': 'application/json-rpc'
}
conn.request("GET", "/api_jsonrpc.php", payload, headers)
res = conn.getresponse()
data = res.read().decode("utf-8")
data_dir = json.loads(data)
print(f"Generated Token: {data_dir['result']}")