-
Notifications
You must be signed in to change notification settings - Fork 1
/
brocade48p_config.py
142 lines (120 loc) · 4.18 KB
/
brocade48p_config.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
from telnetlib import Telnet
def brocade48p_config(switch, config, access_password, new_password):
ip = switch["ip"]
# TODO: put hostname
name = switch["name"]
# 1 - do nothing
ports = config["ports"]
# 2 - we create an array of all existing (untagged AND tagged) VLANs in the configuration
vlans = []
for port_range in ports:
if "untagged" in ports[port_range]:
untagged = ports[port_range]["untagged"]
if untagged not in vlans:
vlans.append(untagged)
tagged_list = ports[port_range]["tagged"]
for tagged in tagged_list:
if tagged not in vlans:
vlans.append(tagged)
# 3 - we have all required datas, so let's go for creating the config file that will get pushed to the switch
print(f"Creating temporary config for switch {ip} with vlans: {vlans}")
tmpConfigName = "config48ports/tmpScript.cfg"
# start writing config file
output = open("/var/tftp/" + tmpConfigName, "w")
# system
output.write(
"""ver 08.0.30qT311
!
stack unit 1
module 1 icx6430-48p-poe-port-management-module
module 2 icx6430-sfp-4port-4g-module\n!\n!\n!\n!
"""
)
for vlan in vlans:
output.write(f"vlan {vlan} name vlan{vlan} by port\n")
if vlan != 1: # Vlan #1's config is done later via dual mode
for port_range, config in ports.items():
if vlan in config["tagged"]:
output.write(f" tagged ethe {port_range_to_brocade(port_range)}\n")
elif config["untagged"] == vlan:
output.write(
f" untagged ethe {port_range_to_brocade(port_range)}\n"
)
output.write(" no spanning-tree\n!\n")
output.write(
f"""!\n!\n!\n!
enable super-user-password {new_password}
snmp-server community hotlinemontreal ro
!
hostname management
ip address {ip} 255.255.255.0 dynamic
ip default-gateway 172.16.1.1
!
!
clock timezone us Alaska
web-management https\n"""
)
for port_range, config in ports.items():
if config["untagged"] == 1:
if "-" in port_range:
separation = port_range.split("-")
premier = int(separation[0])
deuxieme = int(separation[1])
if premier <= 48 and deuxieme > 48:
for i in range(premier, 49):
output.write(f"interface ethernet 1/1/{i}\n" " dual-mode\n!\n")
else:
for i in range(premier, deuxieme + 1):
output.write(f"interface ethernet 1/1/{i}\n" " dual-mode\n!\n")
else:
output.write(f"interface ethernet 1/1/{port_range}\n" " dual-mode\n!\n")
output.write(
f"""!
!
!
!
!
!
end
"""
)
output.close()
print("Done creating config")
# 4 - we send the config!
tftp_server_ip = "172.16.1.1"
with Telnet(ip) as tn:
print("Authenticating...")
tn.read_until(b">")
tn.write(b"enable\n")
tn.read_until(b"Password:")
tn.write(access_password.encode() + b"\n")
tn.read_until(b"#")
print(f"Copying and applying config from {tftp_server_ip}...")
tn.write(
b"copy tftp startup-config "
+ tftp_server_ip.encode()
+ b" "
+ tmpConfigName.encode()
+ b"\n"
)
tn.read_until(b"Download startup-config from TFTP server done.")
print("Config saved, rebooting switch...")
tn.write(b"reload\n")
tn.read_until(b"Are you sure? (enter 'y' or 'n'):")
tn.write(b"y")
tn.read_until(b"(enter 'y' or 'n'):", 10)
tn.write(b"y")
print("Done!")
def port_range_to_brocade(
port_range,
): # Convert "1-48" to "1/1/1 to 1/1/48" for example, weird brocade syntax
if "-" in port_range:
separation = port_range.split("-")
premier = int(separation[0])
deuxieme = int(separation[1])
if premier <= 48 and deuxieme > 48:
return "1/1/{} to 1/1/48".format(premier)
else:
return "1/1/{} to 1/1/{}".format(premier, deuxieme)
else:
return "1/1/{}".format(port_range)