-
Notifications
You must be signed in to change notification settings - Fork 55
/
nxapi_utils.py
434 lines (355 loc) · 12.2 KB
/
nxapi_utils.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# Copyright (C) 2013 Cisco Systems Inc.
# All rights reserved
import urllib2
import contextlib
import base64
import socket
import httplib
from httplib import HTTPConnection, HTTPS_PORT
import ssl
from xml import etree
import xml.etree.ElementTree as ET
import json
import xmltodict
from cisco import *
from errors import *
set_global_vrf("management")
class HTTPSConnection(HTTPConnection):
'''This class allows communication via SSL.'''
default_port = HTTPS_PORT
def __init__(
self,
host,
port=None,
key_file=None,
cert_file=None,
strict=None,
timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
source_address=None,
):
HTTPConnection.__init__(
self,
host,
port,
strict,
timeout,
source_address,
)
self.key_file = key_file
self.cert_file = cert_file
def connect(self):
'''Connect to a host on a given (SSL) port.'''
sock = socket.create_connection((self.host, self.port),
self.timeout, self.source_address)
if self._tunnel_host:
self.sock = sock
self._tunnel()
# this is the only line we modified from the httplib.py file
# we added the ssl_version variable
self.sock = ssl.wrap_socket(sock, self.key_file,
self.cert_file,
ssl_version=ssl.PROTOCOL_SSLv3)
httplib.HTTPSConnection = HTTPSConnection
class RequestMsg:
def __init__(
self,
msg_type='cli_show',
ver='0.1',
sid='1',
input_cmd='show version',
out_format='json',
do_chunk='0',
):
self.msg_type = msg_type
self.ver = ver
self.sid = sid
self.input_cmd = input_cmd
self.out_format = out_format
self.do_chunk = do_chunk
def get_req_msg_str(
self,
msg_type='cli_show',
ver='0.1',
sid='1',
input_cmd='show version',
out_format='json',
do_chunk='0',
):
req_msg = '<?xml version="1.0" encoding="ISO-8859-1"?>\n'
req_msg += '<ins_api>\n'
req_msg += '<type>' + msg_type + '</type>\n'
req_msg += '<version>' + ver + '</version>\n'
req_msg += '<chunk>' + do_chunk + '</chunk>\n'
req_msg += '<sid>' + sid + '</sid>\n'
req_msg += '<input>' + input_cmd + '</input>\n'
req_msg += '<output_format>' + out_format + '</output_format>\n'
req_msg += '</ins_api>\n'
return req_msg
class RespFetcher:
def __init__(
self,
username='admin',
password='insieme',
url='http://172.21.128.227/ins',
):
self.username = username
self.password = password
self.url = url
self.base64_str = base64.encodestring('%s:%s' % (username,
password)).replace('\n', '')
def get_resp(
self,
req_str,
cookie,
timeout,
):
print "URLURLURL : ", self.url
req = urllib2.Request(self.url, req_str)
req.add_header('Authorization', 'Basic %s' % self.base64_str)
req.add_header('Cookie', '%s' % cookie)
try:
with contextlib.closing(urllib2.urlopen(req,
timeout=timeout)) as resp:
resp_str = resp.read()
resp_headers = resp.info()
return (resp_headers, resp_str)
except socket.timeout, e:
print 'Req timeout'
raise
class RespFetcherHttps:
def __init__(
self,
username='admin',
password='insieme',
url='https://172.21.128.227/ins',
):
self.username = username
self.password = password
self.url = url
self.base64_str = base64.encodestring('%s:%s' % (username,
password)).replace('\n', '')
def get_resp(
self,
req_str,
cookie,
timeout,
):
req = urllib2.Request(self.url, req_str)
req.add_header('Authorization', 'Basic %s' % self.base64_str)
req.add_header('Cookie', '%s' % cookie)
try:
with contextlib.closing(urllib2.urlopen(req,
timeout=timeout)) as resp:
resp_str = resp.read()
resp_headers = resp.info()
return (resp_headers, resp_str)
except socket.timeout, e:
print 'Req timeout'
raise
class NXAPITransport:
'''N9000 Python objects off-the-box transport utilizing NX-API'''
target_url = ''
username = ''
password = ''
timeout = 10.0
out_format = 'xml'
do_chunk = '0'
sid = 'sid'
cookie = 'no-cookie'
req_obj = RequestMsg()
@classmethod
def init(cls, target_url, username, password,timeout=timeout):
cls.target_url = target_url
cls.username = username
cls.password = password
cls.req_fetcher = RespFetcher(username=username,
password=password, url=target_url)
@classmethod
def send_cmd_int(cls, cmd, msg_type):
'''Construct NX-API message. Send commands through NX-API. Only single
command for show commands. Internal usage'''
if msg_type == "cli_show" or msg_type == "cli_show_ascii":
if " ;" in cmd:
raise cmd_exec_error("Only single show command supported in internal api")
req_msg_str = cls.req_obj.get_req_msg_str(msg_type=msg_type,
input_cmd=cmd, out_format=cls.out_format,
do_chunk=cls.do_chunk, sid=cls.sid)
(resp_headers, resp_str) = \
cls.req_fetcher.get_resp(req_msg_str, cls.cookie,
cls.timeout)
if 'Set-Cookie' in resp_headers:
cls.cookie = resp_headers['Set-Cookie']
content_type = resp_headers['Content-Type']
root = etree.fromstring(resp_str)
body = root.findall('.//body')
code = root.findall('.//code')
msg = root.findall('.//msg')
output = ""
status = 0
if len(body) != 0:
if msg_type == 'cli_show':
output = etree.tostring(body[0])
else:
output = body[0].text
if output == None:
output = ""
if code[0].text == "200":
status = 0
else:
status = int(code[0].text)
return [output, status, msg[0].text]
@classmethod
def send_cmd(cls, cmd, msg_type):
'''Construct NX-API message. Send commands through NX-API. Multiple
commands okay'''
req_msg_str = cls.req_obj.get_req_msg_str(msg_type=msg_type,
input_cmd=cmd, out_format=cls.out_format,
do_chunk=cls.do_chunk, sid=cls.sid)
print "TITU : %s : %s : %d" % (req_msg_str, cls.cookie, cls.timeout)
(resp_headers, resp_str) = \
cls.req_fetcher.get_resp(req_msg_str, cls.cookie,
cls.timeout)
if 'Set-Cookie' in resp_headers:
cls.cookie = resp_headers['Set-Cookie']
content_type = resp_headers['Content-Type']
#root = etree.fromstring(resp_str)
root = ET.fromstring(resp_str)
body = root.findall('.//body')
code = root.findall('.//code')
msg = root.findall('.//msg')
# Any command execution error will result in the entire thing fail
# This is to align with vsh multiple commands behavior
if len(code) == 0:
raise unexpected_error("Unexpected error")
for i in range(0, len(code)):
if code[i].text != "200":
raise cmd_exec_error("Command execution error: {0}".format(msg[i].text))
output = ""
if msg_type == 'cli_show':
for i in range(0, len(body)):
#output += tostring(body[i])
# TITU
output += ET.tostring(body[i])
else:
for i in range(0, len(body)):
if body[i].text is None:
continue
else:
output += body[i].text
return output
@classmethod
def cli(cls, cmd):
'''Run cli show command. Return show output'''
try:
output = cls.send_cmd(cmd, "cli_show_ascii")
return output
except:
raise
@classmethod
def clip(cls, cmd):
'''Run cli show command. Print show output'''
try:
output = cls.send_cmd(cmd, "cli_show_ascii")
print output
except:
raise
@classmethod
def clic(cls, cmd):
'''Run cli configure command. Return configure output'''
try:
output = cls.send_cmd(cmd, "cli_conf")
return output
except:
raise
@classmethod
def clid(cls, cmd):
'''Run cli show command. Return JSON output. Only XMLized commands
have outputs'''
if " ;" in cmd:
raise cmd_exec_error("Only single command is allowed in clid()")
try:
output = cls.send_cmd(cmd, "cli_show")
o = xmltodict.parse(output)
json_output = json.dumps(o["body"])
return json_output
except:
raise
class NXAPI:
'''A better NX-API utility'''
def __init__(self):
self.target_url = 'http://localhost/ins'
self.username = 'admin'
self.password = 'admin'
self.timeout = 10
self.ver = '0.1'
self.msg_type = 'cli_show'
self.cmd = 'show version'
self.out_format = 'xml'
self.do_chunk = '0'
self.sid = 'sid'
self.cookie = 'no-cookie'
def set_target_url(self, target_url='http://localhost/ins'):
self.target_url = target_url
def set_username(self, username='admin'):
self.username = username
def set_password(self, password='admin'):
self.password = password
def set_timeout(self, timeout=0):
if timeout < 0:
raise data_type_error('timeout should be greater than 0')
self.timeout = timeout
def set_cmd(self, cmd=''):
self.cmd = cmd
def set_out_format(self, out_format='xml'):
if out_format != 'xml' and out_format != 'json':
raise data_type_error('out_format xml or json')
self.out_format = out_format
def set_do_chunk(self, do_chunk='0'):
if do_chunk != 0 and do_chunk != 1:
raise data_type_error('do_chunk 0 or 1')
self.do_chunk = do_chunk
def set_sid(self, sid='sid'):
self.sid = sid
def set_cookie(self, cookie='no-cookie'):
self.cookie = cookie
def set_ver(self, ver='0.1'):
if ver != '0.1':
raise data_type_error('Only ver 0.1 supported')
self.ver = ver
def set_msg_type(self, msg_type='cli_show'):
if msg_type != 'cli_show' and msg_type != 'cli_show_ascii' \
and msg_type != 'cli_conf' and msg_type != 'bash':
raise data_type_error('msg_type incorrect')
self.msg_type = msg_type
def get_target_url(self):
return self.target_url
def get_username(self):
return self.username
def get_password(self):
return self.username
def get_timeout(self):
return self.timeout
def get_cmd(self):
return self.cmd
def get_out_format(self):
return self.out_format
def get_do_chunk(self):
return self.do_chunk
def get_sid(self):
return self.sid
def get_cookie(self):
return self.cookie
def req_to_string(self):
req_msg = '<?xml version="1.0" encoding="ISO-8859-1"?>\n'
req_msg += '<ins_api>\n'
req_msg += '<type>' + self.msg_type + '</type>\n'
req_msg += '<version>' + self.ver + '</version>\n'
req_msg += '<chunk>' + self.do_chunk + '</chunk>\n'
req_msg += '<sid>' + self.sid + '</sid>\n'
req_msg += '<input>' + self.cmd + '</input>\n'
req_msg += '<output_format>' + self.out_format + '</output_format>\n'
req_msg += '</ins_api>\n'
return req_msg
def send_req(self):
req = RespFetcher(self.username, self.password, self.target_url)
return req.get_resp(self.req_to_string(), self.cookie, self.timeout)