forked from kdardoufa/DeviceInfo_from_PI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetFullDevices_from_PI.py
121 lines (106 loc) · 4.34 KB
/
GetFullDevices_from_PI.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import requests
import json
import csv
import logging
from requests.auth import HTTPBasicAuth
import time
# Define Global Variables
USERNAME = "username" # define REST API username
PASSWORD = "password" # define REST API passowrd
PI_ADDRESS = "ip_address" # define IP Address of Prime Infrastructure Server
requests.packages.urllib3.disable_warnings()
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',
filename='GetAllDevices.log', level=logging.INFO)
controller_url = "https://"+PI_ADDRESS+"/webacs/api/v4/data/InventoryDetails/"
Group_List = []
timestr = time.strftime("%Y%m%d_%H%M")
Device_List = "DeviceList_"+timestr+".csv"
def getDeviceGroups():
logging.info(" - Getting all device groups")
url = "https://"+PI_ADDRESS+"/webacs/api/v2/data/DeviceGroups.json?.full=true"
response = requests.get(url, auth=HTTPBasicAuth(USERNAME, PASSWORD), verify=False)
r_json = response.json()
for entity in r_json['queryResponse']['entity']:
group = entity["deviceGroupsDTO"]["groupName"]
Group_List.append(group)
return(Group_List)
Group_List.append(group)
logging.info(f' - {group} added to list')
logging.info(" - Initial groups ok... moving on")
return(Group_List)
# End of Function
def RemoveGeneric(Group_List):
# if thing in some_list: some_list.remove(thing)
logging.info(" - Removing Generic Groups")
if "Device Type" in Group_List:
Group_List.remove("Device Type")
if "Routers" in Group_List:
Group_List.remove("Routers")
if "Security and VPN" in Group_List:
Group_List.remove("Security and VPN")
if "Switches and Hubs" in Group_List:
Group_List.remove("Switches and Hubs")
if "Unified AP" in Group_List:
Group_List.remove("Unified AP")
if "Unsupported Cisco Device" in Group_List:
Group_List.remove("Unsupported Cisco Device")
if "Wireless Controller" in Group_List:
Group_List.remove("Wireless Controller")
new_Group_List = Group_List
logging.info(" - Final groups ok... moving on")
return(new_Group_List)
# End of Function
def getDevices(Group_List):
logging.info(" - Getting Device Info")
i = 0
DeviceFileList = []
NumOfGroups = len(Group_List)
# remove last / from controller url
new_url = controller_url[:-1]
while i < NumOfGroups:
group = Group_List[i]
url = new_url + ".json?.full=true&.group=" + group + "&.maxResults=300"
response = requests.get(url, auth=HTTPBasicAuth(USERNAME, PASSWORD), verify=False)
r_json = response.json()
count = (r_json.get("queryResponse", "")).get("@count", "")
if count != 0: # no devices in group
logging.info(f' - Getting devices in group {group}')
for entity in r_json["queryResponse"]["entity"]:
Info = entity["inventoryDetailsDTO"]
DeviceName = Info.get("summary").get("deviceName", "")
IP_Addr = Info.get("summary").get("ipAddress", "")
Location = Info.get("summary", "").get("location", "")
if group != ("Unsupported Cisco Device"):
Model = Info["chassis"]["chassis"][0]["modelNr"]
SN = Info["chassis"]["chassis"][0]["serialNr"]
else:
Model = "-"
SN = "-"
new_row = [DeviceName, IP_Addr, Location, Model, SN]
DeviceFileList.append(new_row)
# Move on to next group
i += 1
# no devices in group
else:
# move on to next group
i += 1
logging.info(" - All info has been collected.\nEND")
# open a file for writing
DeviceList = open(Device_List, 'w')
# create the csv writer object
csvwriter = csv.writer(DeviceList)
header = ["DeviceName", "IP_Address", "Location", "Type", "Serial Number"]
csvwriter.writerow(header)
for line in DeviceFileList:
csvwriter.writerow(line)
DeviceList.close()
logging.info(" - All done.\nEND")
return()
# End of function
def main():
InitialGroups = getDeviceGroups()
Groups = RemoveGeneric(InitialGroups)
getDevices(Groups)
return()
if __name__ == "__main__":
main()