-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocurve24p_config.py
165 lines (130 loc) · 4.92 KB
/
procurve24p_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Procurve is awesome
from telnetlib import Telnet
import re
def bypass_dumb_prompts(tn, password):
# this is dumb
# keep giving the switch what it wants until it doesn't ask for anything anymore
idx = 1
ls_users = [b"manager", b"admin"]
i_usr = 0
is_password_set = False
while idx >= 0:
idx, match, buf = tn.expect([b"Password", b"continue", b"Username"], timeout=3)
printbytes(buf)
if match is None:
break
if match.group(0).lower() == b"username":
print(f"username requested. sending {ls_users[i_usr]}")
tn.write(ls_users[i_usr] + b"\n")
i_usr += 1
elif match.group(0).lower() == b"continue":
print("continue requested. sending newline")
tn.write(b"\n")
elif match.group(0).lower() == b"password":
print("Authenticating...")
tn.write(password.encode() + b"\n")
is_password_set = True
else:
print(f"Shit broke my dude, match={match}")
break
if not is_password_set:
print(
"\n\n[WARNING] (SPOOKY) No password configured! This script will try to set it.\n\n"
)
return is_password_set
def setpassword(tn, password):
tn.write(b"config\n")
idx, match, buf = tn.expect([b"#"], timeout=3)
printbytes(buf)
if match is None:
print("BROKE: couldn't set password, couldn't enter config")
return
tn.write(b"password manager\n")
idx, match, buf = tn.expect([b":"], timeout=3)
printbytes(buf)
if match is None:
print("BROKE: couldn't set password, couldn't put password")
return
tn.write(password.encode("ascii") + b"\n")
idx, match, buf = tn.expect([b":"], timeout=3)
printbytes(buf)
if match is None:
print("BROKE: couldn't set password, couldn't put password again")
return
tn.write(password.encode("ascii") + b"\n")
idx, match, buf = tn.expect([b"#"], timeout=3)
printbytes(buf)
if match is None:
print("BROKE: couldn't set password, couldn't enter config")
return
tn.write(b"write memory\n")
idx, match, buf = tn.expect([b"#"], timeout=3)
printbytes(buf)
if match is None:
print("BROKE: couldn't set password, couldn't enter config")
return
tn.write(b"exit\n")
print("password set successfully")
def printbytes(x):
regex = re.compile(b"\\x1b[^hH]*?[hH]")
x = regex.sub(b"", x).decode()
print(x.split("\n")[-1])
def procurve24p_config(switch, config, access_password, new_password):
ip = switch["ip"]
name = switch["name"]
model_id = switch["model_id"]
# 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 = "tmpScript.cfg"
# Début de l'écriture du fichier de config
output = open("/var/tftp/" + tmpConfigName, "w")
output.write(f"; {model_id} Configuration Editor; Created on release #Y.11.51\n\n")
output.write(f'hostname "{name}"\n')
output.write('snmp-server community "hotlinemontreal" Unrestricted\n')
for vlan in vlans:
output.write("vlan {}\n".format(vlan))
output.write(f' name "vlan{vlan}"\n')
if vlan == 1:
output.write(" ip address dhcp-bootp\n")
for port_range, config in ports.items():
if config["untagged"] == vlan:
output.write(f" untagged {port_range}\n")
elif vlan in config["tagged"]:
output.write(f" tagged {port_range}\n")
output.write(" exit\n")
output.write("password manager\n")
output.close()
print("Config :")
output = open("/var/tftp/" + tmpConfigName, "r")
print(output.read())
# 4 - we send the config!
tftp_server_ip = "172.16.1.1"
with Telnet(ip) as tn:
is_password_set = bypass_dumb_prompts(tn, access_password)
if (not is_password_set) or new_password != access_password:
setpassword(tn, new_password)
print(f"Copying config from {tftp_server_ip}...")
tn.write(
b"copy tftp startup-config "
+ tftp_server_ip.encode()
+ b" "
+ tmpConfigName.encode()
+ b"\n"
)
buf = tn.read_until(b"[y/n]", timeout=1)
printbytes(buf)
tn.write(b"y")
print("Done!")