forked from manolab/nexusvlancreate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatevlanclassic.py
executable file
·134 lines (110 loc) · 3.81 KB
/
createvlanclassic.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
#!/usr/bin/python3 -B
import requests, coloredlogs, logging
import json
import sys
from nxapi import *
logger = logging.getLogger(__name__)
if '-d' in sys.argv:
logger.setLevel(logging.DEBUG)
coloredlogs.install(level='DEBUG', logger=logger, fmt='%(asctime)s %(levelname)s %(message)s')
logger.debug("debug mode!")
sys.argv.remove('-d')
else:
logger.setLevel(logging.INFO)
coloredlogs.install(level='INFO', logger=logger, fmt='%(asctime)s %(levelname)s %(message)s')
debug = False
if '-dry' in sys.argv:
debug = True
sys.argv.remove('-dry')
logger.info("started!")
vteps_file = "tuttinexus"
logger.debug("switch list in file: "+vteps_file)
try:
command_arg = sys.argv[1]
if command_arg != 'create' and command_arg != 'destroy':
raise IndexError
except IndexError:
logger.error("create or destroy command needed!")
sys.exit(1)
try:
vlan_arg = sys.argv[2]
except IndexError:
logger.error("vlan id nedeed!")
sys.exit(1)
try:
name_arg = sys.argv[3]
except IndexError:
if command_arg == 'create':
logger.error("name needed!")
sys.exit(1)
clis = []
def create_vlan(vlan, name):
clis.append("vlan %s" % vlan)
clis.append(" name %s" % name)
def destroy_vlan(vlan):
clis.append("no vlan %s" % vlan)
#def set_vlan_on_access_port(vlan, access_port):
# clis.append("int %s" % access_port)
# clis.append(" switchport")
# clis.append(" switchport access vlan %s" % vlan)
def check_vlan(switch, user, passw, vlan):
resp = post_clis(switch, user, passw, ["show vlan id %s" % vlan])
if resp["result"]["body"]["TABLE_vlanbriefid"]["ROW_vlanbriefid"]["vlanshowbr-vlanstate"] != "active":
print("ERROR: VLAN %s validation failed on switch %s" %
(vlan, switch))
def findpass(device):
dotcloginrc = '/var/lib/rancid/.cloginrc'
user = ''
password = ''
with open(dotcloginrc) as f:
for line in f:
line = line.strip()
if line == '':
continue
if '#' in line[0]:
continue
chunks = line.split()
if chunks[0] == 'add':
if chunks[1] == 'password':
if device == chunks[2]:
password = chunks[3]
logger.debug("found password!")
if chunks[1] == 'user':
if device == chunks[2]:
user = chunks[3]
logger.debug("found username!")
if user and password:
break
return (user, password)
def main():
if command_arg == 'create':
create_vlan(vlan_arg, name_arg)
else:
destroy_vlan(vlan_arg)
#if access_arg != 'NONE':
# set_vlan_on_access_port(vlan_arg, access_arg)
clis.append("copy run sta")
try:
vteps=[line.rstrip('\n') for line in open(vteps_file)]
except FileNotFoundError:
logger.error('\''+vteps_file+'\' file not found!')
sys.exit(1)
for vtep in vteps:
switch_password=''
if '#' not in vtep:
logger.info("Switch %s" % (vtep))
(switch_user, switch_password) = findpass(vtep)
if switch_password:
logger.debug("password found for switch "+vtep)
logger.info("sending commands to switch "+vtep)
logger.debug(clis)
if not debug: post_clis(vtep, switch_user, switch_password, clis)
if command_arg == 'create' and not debug:
logger.info("verifying switch configuration")
check_vlan(vtep, switch_user, switch_password, vlan_arg)
else:
logger.warning("password not found for switch "+vtep)
logger.info(sys.argv[0]+" ended!")
sys.exit(0)
if __name__ == "__main__":
main()