This repository has been archived by the owner on Oct 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvalidator.py
118 lines (105 loc) · 3.77 KB
/
validator.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
import os
from yaml import load
from jinja2 import Template
from napalm_base import get_network_driver
from pprint import pprint
inventory_f=open('python_inventory.yml', 'r')
inventory_s=inventory_f.read()
inventory_f.close()
python_inventory=load(inventory_s)
f=open('validate.j2')
s=f.read()
f.close()
validate_template=Template(s)
# make sure the directory validatation exists
cwd = os.getcwd()
validation_directory = os.path.dirname(cwd + "/validation/")
if not os.path.exists(validation_directory):
os.makedirs(validation_directory)
junos_driver = get_network_driver('junos')
for device_item in python_inventory:
print '-'*60
print ('rendering the template validate.j2 for device ' + device_item)
f=open(python_inventory[device_item]['vars'])
s=f.read()
f.close()
vars=load(s)
validator_file=open(validation_directory + '/' + device_item + '.yml','w')
validator_file.write(validate_template.render(vars))
validator_file.close()
print '-'*60
print ('auditing the device ' + device_item)
junos_device = junos_driver(hostname=python_inventory[device_item]['ip'], username='pytraining', password='Poclab123', optional_args={'port': 830})
junos_device.open()
pprint(junos_device.compliance_report(validation_directory + '/' + device_item + '.yml'))
print '-'*60
print ('the device ' + device_item + " complies:")
print (junos_device.compliance_report(validation_directory + '/' + device_item + '.yml')['complies'])
junos_device.close()
"""
# python validator.py
------------------------------------------------------------
rendering the template validate.j2 for device ex4200_8
------------------------------------------------------------
auditing the device ex4200_8
{u'complies': True,
'get_bgp_neighbors_detail': {u'complies': True,
u'extra': [],
u'missing': [],
u'present': {'inet.0': {u'complies': True,
u'nested': True}}},
u'skipped': []}
------------------------------------------------------------
the device ex4200_8 complies:
True
------------------------------------------------------------
rendering the template validate.j2 for device ex4200_7
------------------------------------------------------------
auditing the device ex4200_7
{u'complies': True,
'get_bgp_neighbors_detail': {u'complies': True,
u'extra': [],
u'missing': [],
u'present': {'inet.0': {u'complies': True,
u'nested': True}}},
u'skipped': []}
------------------------------------------------------------
the device ex4200_7 complies:
True
------------------------------------------------------------
rendering the template validate.j2 for device ex4200_12
------------------------------------------------------------
auditing the device ex4200_12
{u'complies': True,
'get_bgp_neighbors_detail': {u'complies': True,
u'extra': [],
u'missing': [],
u'present': {'inet.0': {u'complies': True,
u'nested': True}}},
u'skipped': []}
------------------------------------------------------------
the device ex4200_12 complies:
True
"""
"""
# ls validation
ex4200_12.yml ex4200_7.yml ex4200_8.yml
"""
"""
# more validation/ex4200_8.yml
- get_bgp_neighbors_detail:
inet.0:
209:
- connection_state: Established
204:
- connection_state: Established
"""
"""
# more validation/ex4200_7.yml
- get_bgp_neighbors_detail:
inet.0:
210:
- connection_state: Established
204:
- connection_state: Established
"""