@@ -24,12 +24,14 @@ def get_token(url):
24
24
# Combine URL, API call and parameters variables
25
25
url += api_call
26
26
27
- response = requests .post (url , data = json .dumps (payload ), headers = headers , verify = False ).json ()
27
+ response = requests .post (url , data = json .dumps (payload ), headers = headers , verify = False )
28
+ response .raise_for_status ()
29
+ response = response .json ()
28
30
29
31
# Return authentication token from respond body
30
32
return response ["response" ]["serviceTicket" ]
31
33
32
-
34
+
33
35
def get_device_id (token , url ):
34
36
35
37
# Define API Call
@@ -40,50 +42,54 @@ def get_device_id(token, url):
40
42
41
43
# Combine URL, API call and parameters variables
42
44
url += api_call
43
-
44
- response = requests .get (url , headers = headers , verify = False ).json ()
45
+
46
+ response = requests .get (url , headers = headers , verify = False )
47
+ response .raise_for_status ()
48
+ response = response .json ()
45
49
46
50
# Iterate over the response and find first device with access role.
47
51
# Return ID number of the first device matching the criteria
48
52
for item in response ['response' ]:
49
53
if item ['role' ] == 'ACCESS' :
50
54
return item ['id' ]
51
55
52
-
56
+
53
57
def get_config (token , url , id ):
54
58
#Define API Call. To get specific device's configuration
55
59
#we will need to add device's ID in the API call
56
60
api_call = "/network-device/" + id + "/config"
57
-
61
+
58
62
#Header information
59
63
headers = {"X-AUTH-TOKEN" : token }
60
-
64
+
61
65
# Combine URL, API call variables
62
66
url += api_call
63
-
64
- response = requests .get (url , headers = headers , verify = False ).json ()
65
-
67
+
68
+ response = requests .get (url , headers = headers , verify = False )
69
+ response .raise_for_status ()
70
+ response = response .json ()
71
+
66
72
#Find the hostname in the response body and save it to a hostname variable
67
73
hostname = re .findall ('hostname\s(.+?)\s' , response ['response' ])[0 ]
68
-
74
+
69
75
#Create a date_time variable which will hold current time
70
76
date_time = datetime .datetime .now ()
71
-
77
+
72
78
#Create a variable which will hold the hostname combined with the date and time
73
79
#The format will be hostname_year_month_day_hour.minute.second
74
80
file_name = hostname + '_' + str (date_time .year ) + '_' + str (date_time .month ) + '_' + \
75
81
str (date_time .day ) + '_' + str (date_time .hour ) + '.' + str (date_time .minute ) + \
76
82
'.' + str (date_time .second )
77
83
78
84
file = open (file_name + '.txt' , 'w' )
79
-
85
+
80
86
#Write response body to the file
81
87
file .write (response ['response' ])
82
-
88
+
83
89
#Close the file when writing is complete
84
90
file .close ()
85
91
86
-
92
+
87
93
# Assign obtained authentication token to a variable. Provide APIC-EM's
88
94
# URL address
89
95
auth_token = get_token (apic_em_ip )
0 commit comments