-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetConfigs.py
More file actions
executable file
·160 lines (128 loc) · 5.75 KB
/
Copy pathgetConfigs.py
File metadata and controls
executable file
·160 lines (128 loc) · 5.75 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
# Version 22.12.20.1
# Copyright 2022 Chip Copper
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
# to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import requests
import base64
import json
import sys
import getopt
def main():
switchAddress = None
username = None
password = None
outfileName = None
prefix = "https"
verbose = False
# Retrieve and parse command line arguments.
try:
opts, args = getopt.getopt(sys.argv[1:],"u:p:i:e:d:hv",
["username=", "password=", "address=", "insecure", "outfile"])
except getopt.GetoptError:
print("usage: {} -u <username> -p <password> -i <ipaddress> -d <definedOutfile> -e <effectiveOutfile> [--insecure]".format(sys.argv[0]))
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print("usage: {} -u <username> -p <password> -i <ipaddress> -d <definedOutfile> -e <effectiveOutfile> [--insecure]".format(sys.argv[0]))
sys.exit()
elif opt in ("-u", "--username"):
username = arg
elif opt in ("-p", "--password"):
password = arg
elif opt in ("-i","--ipaddress"):
switchAddress = arg
elif opt in ("-e"):
effectiveOutfileName = arg
elif opt in ("-d"):
definedOutfileName = arg
elif opt in ("--insecure"):
prefix = "http"
elif opt in ("-v"):
verbose = True
# Verify all required arguments are present
if (username is None or password is None or switchAddress is None or effectiveOutfileName is None or definedOutfileName is None):
print("usage: {} -u <username> -p <password> -i <ipaddress> -d <definedOutfile> -e <effectiveOutfile> [--insecure]".format(sys.argv[0]))
sys.exit(2)
# Open capture files
try:
defOutfileFD = open(definedOutfileName, "w")
except:
print("Could not open outfile {}".format(definedOutfileName))
sys.exit(3)
try:
effOutfileFD = open(effectiveOutfileName, "w")
except:
print("Could not open outfile {}".format(effectiveOutfileName))
sys.exit(3)
# Base64 encode the username and password
credentials = base64.b64encode(bytearray(username + ":" + password, 'utf-8')).decode()
# Suppress warnings for self-signed certificates
requests.packages.urllib3.disable_warnings()
# Set the base for all REST calls
url_base = prefix + "://" + switchAddress + "/rest/"
session = requests.session()
# No payload
payload = {}
files = {}
# Send the login and print the return status code
headers = {
'Authorization': 'Basic ' + credentials
}
response = session.request("POST", url_base + "login", headers=headers, data=payload, files=files,verify=False)
if response.status_code != 200:
print("Error logging in: {}".format(response.status_code))
exit()
if verbose:
print("Logged in to fabric...")
session_headers = {
'Authorization': response.headers["Authorization"],
'Accept': 'application/yang-data+json',
'Content-Type': 'application/yang-data+json'
}
# Get the defined configuration
response = session.request("GET", url_base + "running/brocade-zone/defined-configuration",
headers=session_headers, data=payload, files=files,verify=False)
if response.status_code != 200:
print("Error getting defined configuration: {}".format(response.status_code))
print(response.text)
else:
json_response = json.loads(response.text)
# print(json.dumps(json_response["Response"]))
json.dump(json_response["Response"], defOutfileFD)
if verbose:
print("Defined configuration retrieved and saved...")
# Get the effective configuration
response = session.request("GET", url_base + "running/brocade-zone/effective-configuration",
headers=session_headers, data=payload, files=files,verify=False)
if response.status_code != 200:
print("Error getting effective configuration: {}".format(response.status_code))
print(response.text)
else:
json_response = json.loads(response.text)
# print(json.dumps(json_response["Response"]))
json.dump(json_response["Response"], effOutfileFD)
if verbose:
print("Effective configuration retrieved and saved...")
effOutfileFD.close()
defOutfileFD.close()
# Send the logout and print the return status code
response = session.request("POST", url_base + "logout", headers=session_headers, data=payload,verify=False)
if response.status_code != 204:
print("Error logging out: {}".format(response.status_code))
if verbose:
print("Logged out of fabric...")
if verbose:
print("Retrievals complete.")
if __name__ == "__main__":
main()