forked from PresidioCode/NXAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nxos_api.py
92 lines (76 loc) · 2.67 KB
/
nxos_api.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
__author__ = 'rgomez'
import csv
from nxapi_base import *
class nxos_api_parser(NXOS):
def __init__(self, IP):
NXOS.__init__(self, IP)
self.file_name = None
self.headers = None
self.list = None
self.output = None
self.output_list = []
self.path = None
#self.cmd = None
def find_name(self):
file_name=self.output.keys()[0]
#print(file_name)
sufix=self.output[file_name].keys()[0]
#print(sufix)
#print(file_name + '_' + sufix + '.csv')
self.file_name = file_name + '_' + sufix + '.csv'
def find_list(self):
my_lists=[]
d_in = self.output
def find_list_in(d_in):
for key in d_in:
if isinstance(d_in[key], list):
my_lists.append(d_in[key])
else:
print('no list:', d_in[key])
find_list_in(d_in[key])
find_list_in(d_in)
self.output_list=my_lists
def find_header(self):
for i in self.output_list:
for ii in i:
for iii in ii:
print(iii)
'''def find_list(self):
for key in self.output:
if isinstance(self.output[key], list):
self.output_list.append(self.output[key])
else:
self.output=self.output[key]
def find_list1(self, value):
if isinstance(value, list):
self.output_list.append(value)
else:
self.output='''
def createCSV(self):
with open(self.file_name, 'wb') as file:
csvw=csv.DictWriter(file, fieldnames=self.headers, extrasaction='ignore', restval='Empty')
csvw.writeheader()
for i in self.output_list:
csvw.writerow(i[0])
def convert(input):
if isinstance(input, dict):
return {convert(key): convert(value) for key, value in input.iteritems()}
elif isinstance(input, list):
return [convert(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
def main(nxapi_device):
output=nxapi_device.cli_show(nxapi_device.cmd)
nxapi_device.output = convert(output)
nxapi_device.find_name()
nxapi_device.createCSV()
'''#def parse_json_rpc(j_out):
# j_rpc, result, j_id = tuple(list(j_out.keys()))
# if j.out[j_rpc] != j_ver: print('WARNING: This parser is based on Version: {}, no in Version: {}'.format(j_ver, j.out[j_rpc]))
# real_output=j_out[result].get()
# if real_output == None:
# raise Exception ('There is no body')
# else:
# def parse_real_out(real_output)'''