-
Notifications
You must be signed in to change notification settings - Fork 0
/
attextreme.py
executable file
·168 lines (158 loc) · 5.22 KB
/
attextreme.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
166
167
168
#!/usr/bin/env python3
import os
import sys
import time
import getpass
import telnetlib
from multiprocessing import Pool
## Import private configs or use the above, change '<PARAMS>'
import private
# tftpcmd = "download image <IP_LOCAL> %s VR-Default" # %img
# radiuscmd = "configure radius mgmt-access primary server <IP_SERVER> 1812 client-ip %s vr VR-Default\nconf radius mgmt-access primary shared-secret <PASSWD>\nenable radius mgmt-access\n"
## Fix models selection, now just commenting/ uncommenting
# model = "X440-48"
# lastv = "16.2.5.4"
# img = "X440-summitX-16.2.5.4-patch1-7.xos"
#
# model = "X440-G2"
# lastv = "22.6.1.4"
# img = "X440G2-summitX-22.6.1.4-patch1-1.xos"
#
model = "X430"
lastv = "16.2.5.4"
img = "X430-summitlite-16.2.5.4-patch1-7.xos"
def enablessh(host):
print('Ativando SSH do %s' % host)
try:
tn = telnetlib.Telnet(host)
tn.read_until(b"login:")
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b"password:")
tn.write(passwd.encode('ascii') + b"\n")
tn.read_until(b" # ",30)
tn.write(b"enable ssh2\n")
tn.write(b"y\n")
tn.write(b"save config\n")
tn.write(b"y\n")
tn.write(b"exit\n")
s = tn.read_all().decode('ascii')
except:
print('Erro ao ativar ssh no host: %s' % host)
f = open(model+'.falha','a+')
f.write(host)
f.write('\n')
f.close()
def atualiza(host):
print('Atualizando %s' % host)
tn = telnetlib.Telnet(host)
tn.read_until(b"login:")
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b"password:")
tn.write(passwd.encode('ascii') + b"\n")
tn.read_until(b" # ",30)
msg = private.tftpcmd %img
# msg = msg.replace("\n","") # need?
tn.write(msg.encode('ascii') + b"\n")
tn.write(b"y\n")
tn.read_until(b"Image installed successfully",1500)
tn.write(b"save config\n")
tn.write(b"y\n")
tn.write(b"exit\n")
s = tn.read_all().decode('ascii')
def set_radius(host):
try:
tn = telnetlib.Telnet(host,23,7)
# tn.set_debuglevel(1)
tn.read_until(b"login:")
tn.write(useradm.encode('ascii') + b"\n")
tn.read_until(b"password:")
tn.write(passwdadm.encode('ascii') + b"\n")
tn.read_until(b" # ",30)
msg = private.radiuscmd %host
tn.write(msg.encode('ascii')+b"\n")
tn.write(b"save config\n")
tn.write(b"y\n")
tn.write(b"exit\n")
s = tn.read_all().decode('ascii')
time.sleep(5)
verifica(host)
except:
print('Não foi possivel logar com \'%s\' no %s' %(useradm,host))
f = open(model+'.falha','a+')
f.write(host)
f.write('\n')
f.close()
def reboot(host):
# TODO: make the rebootcmd auto getting time from switch + 2min
# rebootcmd = "reboot time 1 15 2019 21 20 0"
try:
tn = telnetlib.Telnet(host,23,7)
tn.read_until(b"login:")
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b"password:")
tn.write(passwd.encode('ascii') + b"\n")
tn.read_until(b" # ",30)
# tn.write(rebootcmd.encode('ascii') + b"\n")
# tn.write(b"y\n")
tn.write(b"reboot\n")
tn.write(b"y\n")
# tn.write(b"exit\n")
s = tn.read_all().decode('ascii')
# print(s)
except:
print("must have rebooted")
def verifica(host):
try:
tn = telnetlib.Telnet(host,23,7)
# tn.set_debuglevel(1)
tn.read_until(b"login:")
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b"password:")
tn.write(passwd.encode('ascii') + b"\n")
tn.read_until(b" # ",30)
tn.write(b"show config exsshd\n\n")
tn.write(b"show switch\n\n")
tn.write(b"exit\n")
tn.write(b"y\n")
s = tn.read_all().decode('ascii')
# print(s)
if lastv in s:
match = 'xxxxxxxxx!!'
for line in s.splitlines():
if 'Image Booted:' in line:
match = line.split(':')[1].strip().title()
if match in line:
if lastv in line:
if not 'enable ssh2' in s:
enablessh(host)
else:
print("OK")
else:
reboot(host)
# print('Falta rebootar o %s' % host)
else:
atualiza(host)
except:
print("Tentando logar com \'%s\' e ativar o radius..." % useradm)
set_radius(host)
if __name__ == "__main__":
user = input("Enter your radius user name: ")
passwd = getpass.getpass('Enter your radius password: ')
useradm = "admredes"
passwdadm = getpass.getpass('Enter the local \'%s\' password: ' %useradm)
if not passwd:
print("Senha em branco")
exit(9)
open(model+'.falha','w').close()
pool = Pool(processes=50)
hosts = open(sys.argv[1])
count = 0
for host in hosts:
if model in host:
count += 1
pool.apply_async(verifica, args=(host.split(',')[0],))
# print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]"
pool.close()
pool.join()
print(count," Switches Encontrados")
exit(0)