-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmccast.py
107 lines (94 loc) · 3.93 KB
/
mccast.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
103
104
105
106
107
import requests
import csv
from datetime import datetime
from pathlib import Path
import time
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
center_port = 443
center_base_url = "api/3.0"
def get_devices(center_token, center_ip):
try:
headers = { "x-token-id": center_token }
r_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/devices",headers=headers,verify=False)
r_get.raise_for_status() #if there are any request errors
#raw JSON data response
raw_json_data = r_get.json()
return raw_json_data
except Exception as e:
return f"Error when connecting: {e}"
def build_device_row(row,d):
try:
row['Hardware Name'] = d['label']
if len(d['ip']) == 1:
row['Internal IP Address'] = d['ip'][0]
else:
row['Internal IP Address'] = d['ip']
row['External IP Address'] = ""
row['Function'] = d['deviceType']
os = []
host = ""
vendor = ""
serial = ""
models = []
for component in d['components']:
if component['normalizedProperties']:
for property in component['normalizedProperties']:
if property['key'] == 'os-name':
os.append(property['value'])
elif property['key'] == 'model-ref':
models.append(property['value'])
elif property['key'] == 'serial-number':
serial = property['value']
if component['otherProperties']:
for prop in component['otherProperties']:
if prop['key'] == 'http-host':
host = prop['value']
elif prop['key'] == 'vendor':
vendor = prop['value']
if len(os) == 0:
row['Operating System'] = ""
elif len(os) == 1:
row['Operating System'] = os[0]
else:
row['Operating System'] = os
row['Installed Software'] = ""
row['Authorization Package'] = ""
row['Category'] = d['components'][0]['group']['label']
row['Default Gateway'] = ""
row['Domain Name'] = ""
row['FQDN'] = ""
row['Host Name'] = host
if len(d['mac']) == 1:
row['MAC Address'] = d['mac'][0]
else:
row['MAC Address'] = d['mac']
row['Manufacturer'] = vendor
if len(models) == 0:
row['Model'] = ""
elif len(models) == 1:
row['Model'] = models[0]
else:
row['Model'] = models
row['Network Name Office'] = ""
row['Primary DNS Server'] = ""
row['Serial Number'] = serial
row['Subnet Mask'] = ""
except Exception as e: print(e)
def write_devices(filename,csv_delimiter,devices):
with open(filename, 'w', newline='') as csvfile:
fieldnames = ['Hardware Name', 'Internal IP Address', 'External IP Address', 'Function', 'Operating System', 'Installed Software', 'Authorization Package', 'Category', 'Default Gateway', 'Domain Name', 'FQDN', 'Host Name', 'MAC Address', 'Manufacturer', 'Model', 'Network Name Office', 'Primary DNS Server', 'Serial Number', 'Subnet Mask']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter=csv_delimiter)
writer.writeheader()
for d in devices:
row = {}
build_device_row(row,d)
writer.writerow(row)
print(f"LOG: Exported {len(devices)} into '{filename}'")
def generate_mccast_inventory(center_token, center_ip):
all_devices = get_devices(center_token, center_ip)
now = datetime.now()
current_time = now.strftime("%m-%d-%Y_%H-%M-%S")
file_name = "Cyber Vision MCCAST Device Inventory_" + current_time + ".csv"
write_devices(file_name, ",", all_devices)
print("MCCAST Device Inventory Report in this directory: " + str(Path.cwd()))