-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnornir_get_routes-v3.py
More file actions
220 lines (167 loc) · 6.74 KB
/
Copy pathnornir_get_routes-v3.py
File metadata and controls
220 lines (167 loc) · 6.74 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
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
from nornir import InitNornir
from nornir.core.filter import F
from nornir.plugins.functions.text import print_result , print_title
from nornir.plugins.tasks.networking import netmiko_send_config , netmiko_send_command, netmiko_save_config
#import ntc_templates
import time
from pprint import pprint
from nornir.core.deserializer.inventory import InventoryElement
import json
import os
def create_dir(dir_to_create):
if not os.path.exists(dir_to_create):
os.mkdir(dir_to_create)
print ("Directory",dir_to_create,"created")
def main():
create_dir('outputs')
'''
THIS IS FOR KNOWING FOR WHERE IS LOADING THE MODULE
#print (ntc_templates.__file__)
'''
start = time.time()
nr = InitNornir (config_file='config/config.yml', dry_run=True,logging={"file": "logfile","level": "debug"})
#nrdict = nr.dict()
global total
total = (len(nr.inventory.hosts))
'''
#print (type(nrdict))
#for key, value in nrdict.items():
# print (key, value)
'''
if nr.filter(F(groups__contains='nexus')):
nexus = nr.filter(F(groups__contains='nexus'))
#print (nexus.inventory.hosts.keys())
result = nexus.run(task=get_facts_nxos)
if nr.filter(~F(groups__contains='nexus')):
ios = nr.filter(~F(groups__contains='nexus'))
iosdict = ios.dict()
#pprint (iosdict['inventory']['hosts'])
#print (ios.inventory.hosts.items())
result2 = ios.run(task=get_facts)
end = time.time()
elapssed = end - start
tt = str(elapssed)
print ("ELAPSED "+ tt)
def get_facts(task):
comm = 'show cdp neighbors detail'
x = task.run( netmiko_send_command, command_string=comm, use_textfsm=True)
neigh = x.result
host = task.host.hostname
newdir = f'outputs/{host}'
create_dir(newdir)
dump_interfaces(comm,neigh,host,newdir)
comm = 'show ip interface brief'
y = task.run( netmiko_send_command, command_string=comm, use_textfsm=True)
parsed_ints = y.result
host = task.host.hostname
newdir = f'outputs/{host}'
create_dir(newdir)
clean_interfaces(parsed_ints)
while {} in parsed_ints:
parsed_ints.remove({})
dump_interfaces(comm,parsed_ints,host,newdir)
r = task.run( netmiko_send_command, command_string="show vrf", use_textfsm=True)
task.host["facts"] = r.result
#print (task.host.hostname)
if isinstance (task.host["facts"], str):
comm = 'show ip route'
s = task.run (netmiko_send_command, command_string = comm, use_textfsm=True)
task.host["facts"] = s.result
#print (task.host.hostname)
host = task.host.hostname
#print (f'{host} {comm} ESTE NO TIENE VRFS')
vrf_name = 'default'
#print (comm)
#print (task.host["facts"])
parsed = task.host["facts"]
clean_routes(parsed)
dump_command(comm,parsed,host,newdir,vrf_name)
else:
for i in task.host["facts"]:
vrf_name = (i['name'])
comm = f'show ip route vrf {vrf_name}'
#print (comm)
#print (task.host.hostname)
host = task.host.hostname
newdir = f'outputs/{host}'
create_dir(newdir)
#print (comm)
s = task.run (netmiko_send_command, command_string = comm, use_textfsm=True)
task.host["facts"] = s.result
parsed = task.host["facts"]
clean_routes(parsed)
dump_command(comm,parsed,host,newdir,vrf_name)
comm = 'show ip route'
#print (comm)
s = task.run (netmiko_send_command, command_string = comm, use_textfsm=True)
task.host["facts"] = s.result
vrf_name = 'default'
#print (task.host.hostname)
host = task.host.hostname
#print (comm)
#print (task.host["facts"])
parsed = task.host["facts"]
dump_command(comm,parsed,host,newdir,vrf_name)
def clean_interfaces(parsed):
for i in (parsed):
if i['ipaddr']=='unassigned':
#print (parsed[i])
#del parsed[i]
i.pop('ipaddr')
i.pop('intf')
i.pop('status')
i.pop('proto')
#print(type(i))
def clean_routes(parsed):
for i in parsed:
nhip = i.pop('nexthop_ip')
nhif = i.pop('nexthop_if')
i.pop('uptime')
subnet = i['network'] +'/'+ i['mask']
i['subnet']= subnet
i['nexthop'] = nhip
i['interface'] = nhif
typ = i.pop('type')
distance = i.pop('distance')
metric = i.pop('metric')
i.pop('network')
i.pop('mask')
if i['protocol'] == 'B':
i.pop('protocol')
i['protocol'] = 'BGP'
if i['protocol'] == 'O':
i.pop('protocol')
i['protocol'] = 'OSPF'
if i['protocol'] == 'S':
i.pop('protocol')
i['protocol'] = 'STATIC'
if i['protocol'] == 'C':
i.pop('protocol')
i['protocol'] = 'CONNECTED'
if i['protocol'] == 'D':
i.pop('protocol')
i['protocol'] = 'EIGRP'
if i['protocol'] == 'L':
i.pop('protocol')
i['protocol'] = 'LOCAL'
i['type'] = typ
i['distance'] = distance
i['metric'] = metric
def get_facts_nxos(task):
r = task.run( netmiko_send_command, command_string="show vrf", use_textfsm=True)
task.host["facts"] = r.result
def dump_interfaces (command,command_parsed,host,newdir):
command = command.replace(' ','_')
filename = command +'.json'
filename = '/'.join((newdir, filename))
END_DICT = { command : command_parsed }
with open(filename,'w') as f:
json.dump(END_DICT,f, indent = 2)
def dump_command(command,command_parsed,host,newdir,vrf_name):
filename = command.replace(' ','_')+'.json'
filename = '/'.join((newdir, filename))
END_DICT = { vrf_name : command_parsed }
with open(filename,'w') as f:
json.dump(END_DICT,f, indent = 2)
if __name__ == '__main__':
main()