-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
81 lines (60 loc) · 1.86 KB
/
cli.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
#!/usr/bin/env python3
"""
call-api.py : Shadowserver Foundation API Utility
This script requires your API details to be stored in ~/.shadowserver.api
with the following contents:
--
[api]
key = 123456798
secret = MySecret
uri = https://transform.shadowserver.org/api2/
--
This script may be called with two or three arguments:
call-api.py <method> <request> [pretty|binary]
The request must be a valid JSON object.
Simple usage:
$ ./call-api.py test/ping '{}'
{"pong":"2020-10-26 23:06:37"}
Pretty output:
$ ./call-api.py test/ping '{}' pretty
{
"pong": "2020-10-26 23:06:42"
}
"""
import os
import sys
import json
import configparser
from shadowapi.call_api import Config, ShadowAPI
if __name__ == '__main__':
config = configparser.ConfigParser()
config.read(os.environ['HOME'] + "/.shadowserver.api")
if (len(sys.argv) < 3):
exit("Usage: call-api.py method json [pretty|binary]")
try:
api_request = json.loads(sys.argv[2])
except Exception as e:
exit("JSON Exception: " + format(e))
try:
config.get('api', 'key')
except configparser.NoSectionError:
exit("Exception: " + os.environ['HOME'] + "/.shadowserver.api could "
"not be found. Exiting.")
try:
conf = Config(config.get('api', 'key'), config.get('api', 'secret'), config.get('api', 'uri'))
Shadow = ShadowAPI(conf)
res = Shadow.api_call(sys.argv[1], api_request)
except Exception as e:
exit("API Exception: " + format(e))
if (len(sys.argv) > 3):
if (sys.argv[3] == "pretty"):
try:
print(json.dumps(json.loads(res), indent=4))
except:
print(res.decode('utf-8'))
elif (sys.argv[3] == "binary"):
os.write(1, res);
else:
exit("Unknown option " + sys.argv[3])
else:
print(res.decode('utf-8'))