forked from extremenetworks/ExtremeScripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_eaps.py
More file actions
368 lines (326 loc) · 13.9 KB
/
check_eaps.py
File metadata and controls
368 lines (326 loc) · 13.9 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python
# This script connect to the switch and checks the eaps config
# Update 1.02 Added look_for_keys=false for paramiko
__version__ = '1.02'
import telnetlib
import re
import argparse
import sys
import paramiko
import time
class ExosClass:
def __init__(self, host, user='admin',password=''):
self.host=host
self.user=user
self.password=password
self.prompt=re.compile(r'\S+\d+\s[#>]\s')
self.loginprompt=r"login"
self.passwordprompt=r"assword"
self.failedlogin = r"Login incorrect"
self.connected = True
try:
self._tn = telnetlib.Telnet(self.host)
self._tn.set_debuglevel(0)
self._tn.read_until(self.loginprompt,10)
self._tn.write(self.user + "\n")
self._tn.expect([self.passwordprompt],10)
self._tn.write(self.password + "\n")
loginresponse = self._tn.expect([self.prompt,self.failedlogin])
if loginresponse[0] == 1:
print "Login failed"
self.connected = False
self._tn.close()
else:
self._tn.write("disable clipaging\n")
self._tn.expect([self.prompt])
except:
print "Could not connect to switch "+host
self.connected = False
def exit(self):
self._tn.close()
def isConnected(self):
if self.connected:
return True
else:
return False
def cmd(self,cmd,timeout=30):
self._tn.write(cmd + "\n")
s=self._tn.expect([self.prompt],timeout)[2].lstrip(cmd).lstrip('\n\r')
if s.count('\n')>1:
return s[:s.rfind('\n')]
else:
return "OK"
def cmdFast(self,cmd,timeout=30):
self._tn.write(cmd+"\n")
output=""
go = True
while go:
time.sleep(0.5)
newoutput = self._tn.read_very_eager()
if len(newoutput) == 0:
lastline = output.splitlines()[-1]
if re.search(self.prompt,lastline):
go = False
else:
output += newoutput
return output
class SSH2EXOS:
def __init__(self, switch, user='admin',password=''):
self.connected = True
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(switch,username=user,password=password,look_for_keys=False)
stdin, stdout, stderr = self.client.exec_command("disable clipaging")
stdin.close()
def cmdFast(self,cmd,timeout=30):
stdin, stdout, stderr = self.client.exec_command(cmd)
stdin.close()
return stdout.read()
def cmd(self,cmd,timeout=30):
stdin, stdout, stderr = self.client.exec_command(cmd)
stdin.close()
return stdout.read()
def exit(self):
self.client.close()
self.connected = False
def isConnected(self):
if self.connected:
return True
else:
return False
def checkIP(address):
m = re.search('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})',address)
if m:
return m.group(1)
else:
return None
def insert_vport(vlanports, vlan, port):
if vlan not in vlanports.keys():
vlanports[vlan] = []
vlanports[vlan].append(port)
def checkconfig(config,vlanports):
print "Checking config..",
sys.stdout.flush()
for line in config.splitlines():
vp = re.search(r'configure\sv[l|m]an\s(\S+)\sadd\sports\s(.*)',line)
if vp:
ports = re.sub('[private-vlan|translated|tagged|untagged|\s]','',vp.group(2))
if ports.count(':') > 0:
for prt in ports.split(','):
if prt.count('-') == 0:
insert_vport(vlanports,vp.group(1),prt)
else:
modport = prt.split(':')
prts = modport[1].split('-')
startport = int(prts[0])
endport = int(prts[1])
while startport <= endport:
vlanprt = modport[0]+":"+str(startport)
insert_vport(vlanports,vp.group(1),vlanprt)
startport += 1
else:
for prt in ports.split(','):
if prt.count('-') == 0:
insert_vport(vlanports,vp.group(1),prt)
else:
prts = prt.split('-')
try:
startport = int(prts[0])
endport = int(prts[1])
while startport <= endport:
insert_vport(vlanports,vp.group(1),str(startport))
startport += 1
except:
print "Error on ports, value is :",
print prts
print line
def eapsCheck(data,vlanports):
print "Eaps Check start..",
sys.stdout.flush()
# eapsport[domain][primary,secondary,enable/disable]
eapsports = {}
eapsvlans = {}
problems = ""
for line in data.splitlines():
m = re.search(r'create eaps (\S+).*',line)
if m and m.group(1)!="shared-port":
eapsports[m.group(1)] = ["","","","",""]
m = re.search(r'configure\seaps\s(\S+)\ssecondary\sport\s(\S+)',line)
if m and m.group(1)!="shared-port":
eapsports[m.group(1)][1] = (m.group(2))
m = re.search(r'configure\seaps\s(\S+)\sprimary\sport\s(\S+)',line)
if m and m.group(1)!="shared-port":
eapsports[m.group(1)][0] = (m.group(2))
m = re.search(r'(enable|disable)\seaps\s(\S+)',line)
if m and m.group(2)!="shared-port":
eapsports[m.group(2)][2] = (m.group(1))
m = re.search(r'configure\seaps\s(\S+)\smode\s(transit|master)',line)
if m:
eapsports[m.group(1)][3] = (m.group(2))
# assuming pri/sec ports are always defined before the add of protected eaps vlans we start immediately
# for search of control/protected vlans
m3 = re.search(r'configure eaps (\S+) add protected \S+\s(\S+)',line)
if m3 and "enable" in eapsports[m3.group(1)]:
if m3.group(1) not in eapsvlans.keys():
eapsvlans[m3.group(1)]=[]
eapsvlans[m3.group(1)].append(m3.group(2))
# Check if primary and secondary port are added to the vlan
if eapsports[m3.group(1)][0] not in vlanports.get(m3.group(2),[]):
problems += " - Protected vlan "+m3.group(2)+" ports not added to EAPS "+m3.group(1)
problems += " port "+eapsports[m3.group(1)][0]+"\n"
if eapsports[m3.group(1)][1] not in vlanports.get(m3.group(2),[]):
problems += " - Protected vlan "+m3.group(2)+" ports not added to EAPS "+m3.group(1)
problems += " port "+eapsports[m3.group(1)][1]+"\n"
m3 = re.search(r'configure eaps (\S+) add control \S+\s(\S+)',line)
if m3 and "enable" in eapsports[m3.group(1)]:
eapsports[m3.group(1)][4] = m3.group(2)
if m3.group(1) not in eapsvlans.keys():
eapsvlans[m3.group(1)]=[]
eapsvlans[m3.group(1)].append(m3.group(2))
# Check if primary and secondary port are added to the vlan
if eapsports[m3.group(1)][0] not in vlanports.get(m3.group(2),[]):
problems += " - Control vlan "+m3.group(2)+" ports not added to EAPS "+m3.group(1)
problems += " port "+eapsports[m3.group(1)][0]+"\n"
if eapsports[m3.group(1)][1] not in vlanports.get(m3.group(2),[]):
problems += " - Control vlan "+m3.group(2)+" ports not added to EAPS "+m3.group(1)
problems += " port "+eapsports[m3.group(1)][1]+"\n"
print "Vlan check.."
sys.stdout.flush()
for vlan in vlanports.keys():
for domain in eapsports.keys():
if eapsports[domain][1] in vlanports[vlan] and eapsports[domain][0] in vlanports[vlan]:
#both eaps ports in vlan
if vlan not in eapsvlans[domain]:
problems += " - Vlan "+vlan+" added to ringports of eaps domain "+domain+" but not protected by it.\n"
if len(problems) == 0:
print "\n[+] No Eaps config problems detected"
else:
print "\n[-] Eaps config problems found : "
print problems
return eapsports,eapsvlans
def switchData(data):
switch = {}
for line in data.splitlines():
m = re.search(r'SysName:\s+(\S+)',line)
if m:
switch['Name'] = m.group(1)
m = re.search(r'System Type:\s+(\S+)',line)
if m:
switch['Type'] = m.group(1)
return switch
def eapsStatus(data):
problems = ""
domains = 0
masterdomains = []
for line in data.splitlines():
m = re.search(r'(\S+)\s+(\S+)\s+(T|M|-)\s+(Y|N).*',line)
if m:
domains += 1
if m.group(3) == "M":
if m.group(2) == "Complete":
masterdomains.append(m.group(1))
continue
elif m.group(4) == "N":
problems += " - "+m.group(1)+" domain not enabled.\n"
else:
problems += " - "+m.group(1)+" Status "+m.group(2)+".\n"
if m.group(3) == "T":
if m.group(2) == "Links-Up":
continue
elif m.group(4) == "N":
problems += " - "+m.group(1)+" domain not enabled.\n"
else:
problems += " - "+m.group(1)+" Status "+m.group(2)+".\n"
if len(problems) > 1:
print "[-] Problem eaps domains status found."
print problems
else:
print "[+] Eaps status for all "+str(domains)+" domains OK"
if domains == 0:
print "[-] No Eaps domains found"
return masterdomains
def vpifcheck(port,vlans,MySess,domain,eapsports):
print "\n[+] Checking vpif state for eaps domain "+domain+" blocked port "+port
print " This can take some time on large vlan/eaps configs."
problem = False
for vlan in vlans:
if vlan != eapsports[domain][4]:
cmd = "debug vlan show vpif "+vlan+" "+port
vpifoutput = MySess.cmd(cmd)
for line in vpifoutput.splitlines():
m = re.search(r'\s*Ingress:(0x\d+),\s*Egress:\s*(0x\d+).*',line)
if m:
if m.group(1) != "0x22":
problem = True
print " - Vlan "+vlan+" vpif Ingress state is not Blocking (0x2) on port "+port
if m.group(2) != "0x2":
problem = True
print " - Vlan "+vlan+" vpif Egress state is not Blocking (0x22) on port "+port
if not problem:
print " - All vpif states are correct on port "+port+" for domain "+domain
def main():
parser = argparse.ArgumentParser(description='Connect to switch and check Eaps config')
parser.add_argument("-s", dest="switch", default=None, help="Switch IP")
parser.add_argument("-u", dest="user", default="admin", help="Username")
parser.add_argument("-p", dest="password", default="", help="Password, leave out for none")
parser.add_argument("-f", dest="file", default=None, help="File containing switch IP addresses")
parser.add_argument("--ssh", dest="SSH", action='store_true', help="Use SSH to access switches instead of telnet")
parser.add_argument("--vpif", dest="vpifcheck", action='store_true', help="Check VPIF state on sec port master")
args = parser.parse_args()
switches = []
vlanports = {}
validInput = True
print "\n[Eaps checker version "+__version__+"]\n"
if args.switch:
if checkIP(args.switch):
switches.append(args.switch)
else:
print "No Valid IP address specified with -s"
validInput = False
elif args.file:
try:
f = open(args.file,'r')
for line in f.read().splitlines():
switches.append(checkIP(line))
f.close()
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
except:
print "Something wrong with opening the file"
validInput = False
else:
print "No valid option -s or -f specified"
validInput = False
if validInput:
for switch in switches:
print "\n[+] Checking switch: "+switch,
sys.stdout.flush()
if args.SSH:
MySess = SSH2EXOS(switch,args.user,args.password)
else:
MySess = ExosClass(switch,args.user,args.password)
if MySess.isConnected():
vlanports.clear()
shswitch = MySess.cmdFast("show switch")
switch = switchData(shswitch)
print "-SysName: "+switch['Name'], "-HW Type: "+switch['Type']
vlconf = MySess.cmdFast("show config vlan",60)
checkconfig(vlconf,vlanports)
eapsconf = MySess.cmdFast("show config eaps",60)
eapsports,eapsvlans = eapsCheck(eapsconf,vlanports)
sheaps = MySess.cmdFast("show eaps")
masterdomains = eapsStatus(sheaps)
if args.vpifcheck:
#Check vpif state on master secondary port
if len(masterdomains) == 0:
print "[+] vpif check, no Master domains found, no vpif check needed."
for domain in masterdomains:
vpifcheck(eapsports[domain][1],eapsvlans[domain],MySess,domain,eapsports)
print "\n[-] Closing connection"
print "\n######################\n"
MySess.exit()
if __name__ == "__main__":
try:
main()
except SystemExit:
pass