-
Notifications
You must be signed in to change notification settings - Fork 0
/
Splunk_Lookup.py
63 lines (44 loc) · 1.85 KB
/
Splunk_Lookup.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
import requests
import json
import requests
import json
import csv
def make_request(method, host, lookup_name, lookup_content):
baseurl = "https://{}:8089".format(host)
# GET requests to this endpoint will execute get_lookup_contents()
# POST requests to this endpoint will execute post_lookup_contents()
# from the lookup_editor_rest_handler.py in the lookup-editor app
splunk_management_service = "/services/data/lookup_edit/lookup_contents" # endpoint lookup-editor
splunk_lookup_table_service = "/servicesNS/admin/search/data/lookup-table-files"
if method == "POST":
r = requests.post(baseurl + splunk_management_service,
auth=("admin", "xx"),
verify=False,
data={"output_mode": "json",
"namespace": "search",
"lookup_file": lookup_name,
"contents": json.dumps(lookup_content)}
)
return r
elif method == "GET":
r = requests.get(baseurl + splunk_lookup_table_service + lookup_name,
auth=("admin", "xx"),
verify=False).content
return r
else:
return "method not implemented"
def fetch_csv(path):
lookup_content = []
try:
with open(path, 'r') as file:
csvreader = csv.reader(file)
for row in csvreader:
lookup_content.append(row)
except Exception as e:
print("Error reading {} : {}".format(path, e))
return lookup_content
if __name__ == '__main__':
r = make_request("GET", host="10.0.0.xx", lookup_name="assets.csv", lookup_content=None)
print(r)
# r = make_request(method="POST", lookup_name="assets.csv", lookup_content=fetch_csv("j1_asset_data.csv"))
# print(r)