blob: 5303e2e7de6cbc7793118693c58da1d014e2d0b8 [file] [log] [blame]
Steve McIntyred6759dd2014-08-12 18:10:00 +01001#! /usr/bin/python
2
3# Copyright 2014 Linaro Limited
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18# MA 02110-1301, USA.
19
20import logging
21import pexpect
22import sys
Steve McIntyred6759dd2014-08-12 18:10:00 +010023import re
Steve McIntyre5fa22652015-04-01 18:01:45 +010024from drivers.common import SwitchDriver, SwitchErrors
Steve McIntyre3dfd36f2015-02-12 06:37:56 +000025
Steve McIntyre72a8bce2015-01-23 18:02:19 +000026if __name__ == '__main__':
Steve McIntyrea67473a2015-02-12 08:26:33 +000027 import os
Steve McIntyre72a8bce2015-01-23 18:02:19 +000028 vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
29 sys.path.insert(0, vlandpath)
30 sys.path.insert(0, "%s/.." % vlandpath)
31
Steve McIntyre5fa22652015-04-01 18:01:45 +010032from errors import InputError, PExpectError
Steve McIntyred6759dd2014-08-12 18:10:00 +010033
34class CiscoCatalyst(SwitchDriver):
35
36 connection = None
Steve McIntyre3dfd36f2015-02-12 06:37:56 +000037 _username = None
38 _password = None
39 _enable_password = None
Steve McIntyre3f287882014-08-18 19:02:15 +010040
41 _capabilities = [
42 'TrunkWildCardVlans' # Trunk ports are on all VLANs by
43 # default, so we shouldn't need to
44 # bugger with them
45 ]
46
Steve McIntyred6759dd2014-08-12 18:10:00 +010047 # Regexp of expected hardware information - fail if we don't see
48 # this
Steve McIntyre3f287882014-08-18 19:02:15 +010049 _expected_descr_re = re.compile('WS-C\S+-\d+P')
Steve McIntyred6759dd2014-08-12 18:10:00 +010050
Steve McIntyred6759dd2014-08-12 18:10:00 +010051 logfile = None
52
Steve McIntyre48dc6ae2014-12-23 16:08:19 +000053 def __init__(self, switch_hostname, switch_telnetport=23, debug = False):
Steve McIntyre5fa22652015-04-01 18:01:45 +010054 SwitchDriver.__init__(self)
55 self._systemdata = []
Steve McIntyre48dc6ae2014-12-23 16:08:19 +000056 if debug:
Steve McIntyre9f5a0232014-12-23 16:14:28 +000057 self.logfile = sys.stderr
Steve McIntyred6759dd2014-08-12 18:10:00 +010058 self.exec_string = "/usr/bin/telnet %s %d" % (switch_hostname, switch_telnetport)
Steve McIntyre3dfd36f2015-02-12 06:37:56 +000059 self.errors = SwitchErrors()
Steve McIntyred6759dd2014-08-12 18:10:00 +010060
61 ################################
62 ### Switch-level API functions
63 ################################
64
65 # Connect to the switch and log in
Steve McIntyre9b09b9d2014-09-24 15:08:10 +010066 def switch_connect(self, username, password, enablepassword):
Steve McIntyre3dfd36f2015-02-12 06:37:56 +000067 self._username = username
68 self._password = password
69 self._enable_password = enablepassword
70 self._switch_connect()
Steve McIntyred6759dd2014-08-12 18:10:00 +010071
72 # Log out of the switch and drop the connection and all state
Steve McIntyre9b09b9d2014-09-24 15:08:10 +010073 def switch_disconnect(self):
Steve McIntyred6759dd2014-08-12 18:10:00 +010074 self._logout()
Steve McIntyre5fa22652015-04-01 18:01:45 +010075 logging.debug("Closing connection: %s", self.connection)
Steve McIntyred6759dd2014-08-12 18:10:00 +010076 self.connection.close(True)
Steve McIntyre3dfd36f2015-02-12 06:37:56 +000077 self._ports = []
78 self._prompt_name = ''
79 self._systemdata = []
Steve McIntyred6759dd2014-08-12 18:10:00 +010080 del(self)
81
82 # Save the current running config into flash - we want config to
83 # remain across reboots
Steve McIntyre9b09b9d2014-09-24 15:08:10 +010084 def switch_save_running_config(self):
Steve McIntyre3dfd36f2015-02-12 06:37:56 +000085 try:
86 self._cli("copy running-config startup-config")
87 self.connection.expect("startup-config")
88 self._cli("startup-config")
89 self.connection.expect("OK")
Steve McIntyre2d84e522015-02-12 06:44:42 +000090 except (PExpectError, pexpect.EOF):
Steve McIntyre3dfd36f2015-02-12 06:37:56 +000091 # recurse on error
92 self._switch_connect()
93 self.switch_save_running_config()
Steve McIntyred6759dd2014-08-12 18:10:00 +010094
Steve McIntyre095b4452014-12-19 17:53:43 +000095 # Restart the switch - we need to reload config to do a
96 # roll-back. Do NOT save running-config first if the switch asks -
97 # we're trying to dump recent changes, not save them.
98 #
99 # This will also implicitly cause a connection to be closed
100 def switch_restart(self):
101 self._cli("reload")
102 index = self.connection.expect(['has been modified', 'Proceed'])
103 if index == 0:
104 self._cli("n") # No, don't save
105 self.connection.expect("Proceed")
106
107 # Fall through
108 self._cli("y") # Yes, continue to reset
109 self.connection.close(True)
110
Steve McIntyre3f287882014-08-18 19:02:15 +0100111 # List the capabilities of the switch (and driver) - some things
112 # make no sense to abstract. Returns a dict of strings, each one
113 # describing an extra feature that that higher levels may care
114 # about
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100115 def switch_get_capabilities(self):
Steve McIntyre3f287882014-08-18 19:02:15 +0100116 return self._capabilities
Steve McIntyred6759dd2014-08-12 18:10:00 +0100117
118 ################################
119 ### VLAN API functions
120 ################################
121
122 # Create a VLAN with the specified tag
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100123 def vlan_create(self, tag):
Steve McIntyre5fa22652015-04-01 18:01:45 +0100124 logging.debug("Creating VLAN %d", tag)
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000125 try:
126 self._configure()
127 self._cli("vlan %d" % tag)
128 self._end_configure()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100129
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000130 # Validate it happened
131 vlans = self.vlan_get_list()
132 for vlan in vlans:
133 if vlan == tag:
134 return
135 raise IOError("Failed to create VLAN %d" % tag)
136
137 except PExpectError:
138 # recurse on error
139 self._switch_connect()
140 self.vlan_create(tag)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100141
142 # Destroy a VLAN with the specified tag
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100143 def vlan_destroy(self, tag):
Steve McIntyre5fa22652015-04-01 18:01:45 +0100144 logging.debug("Destroying VLAN %d", tag)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100145
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000146 try:
147 self._configure()
148 self._cli("no vlan %d" % tag)
149 self._end_configure()
150
151 # Validate it happened
152 vlans = self.vlan_get_list()
153 for vlan in vlans:
154 if vlan == tag:
155 raise IOError("Failed to destroy VLAN %d" % tag)
156
157 except PExpectError:
158 # recurse on error
159 self._switch_connect()
160 self.vlan_destroy(tag)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100161
162 # Set the name of a VLAN
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100163 def vlan_set_name(self, tag, name):
Steve McIntyre5fa22652015-04-01 18:01:45 +0100164 logging.debug("Setting name of VLAN %d to %s", tag, name)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100165
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000166 try:
167 self._configure()
168 self._cli("vlan %d" % tag)
169 self._cli("name %s" % name)
170 self._end_configure()
171
172 # Validate it happened
173 read_name = self.vlan_get_name(tag)
174 if read_name != name:
175 raise IOError("Failed to set name for VLAN %d (name found is \"%s\", not \"%s\")"
176 % (tag, read_name, name))
177 except PExpectError:
178 # recurse on error
179 self._switch_connect()
180 self.vlan_set_name(tag, name)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100181
182 # Get a list of the VLAN tags currently registered on the switch
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100183 def vlan_get_list(self):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100184 logging.debug("Grabbing list of VLANs")
Steve McIntyred6759dd2014-08-12 18:10:00 +0100185
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000186 try:
187 vlans = []
Steve McIntyred6759dd2014-08-12 18:10:00 +0100188
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000189 regex = re.compile('^ *(\d+).*(active)')
190
191 self._cli("show vlan brief")
192 for line in self._read_long_output():
193 match = regex.match(line)
194 if match:
195 vlans.append(int(match.group(1)))
196 return vlans
197
198 except PExpectError:
199 # recurse on error
200 self._switch_connect()
201 return self.vlan_get_list()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100202
203 # For a given VLAN tag, ask the switch what the associated name is
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100204 def vlan_get_name(self, tag):
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000205
206 try:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100207 logging.debug("Grabbing the name of VLAN %d", tag)
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000208 name = None
209 regex = re.compile('^ *\d+\s+(\S+).*(active)')
210 self._cli("show vlan id %d" % tag)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100211 for line in self._read_long_output("show vlan id"):
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000212 match = regex.match(line)
213 if match:
214 name = match.group(1)
215 name.strip()
216 return name
217
218 except PExpectError:
219 # recurse on error
220 self._switch_connect()
221 return self.vlan_get_name(tag)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100222
223 ################################
224 ### Port API functions
Steve McIntyree1bf11a2014-08-14 17:56:25 +0100225 ################################
Steve McIntyred6759dd2014-08-12 18:10:00 +0100226
Steve McIntyre9936d002014-10-01 15:54:10 +0100227 # Set the mode of a port: access or trunk
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100228 def port_set_mode(self, port, mode):
Steve McIntyre5fa22652015-04-01 18:01:45 +0100229 logging.debug("Setting port %s to %s", port, mode)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100230 if not self._is_port_mode_valid(mode):
Steve McIntyre72a8bce2015-01-23 18:02:19 +0000231 raise InputError("Port mode %s is not allowed" % mode)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100232 if not self._is_port_name_valid(port):
Steve McIntyre72a8bce2015-01-23 18:02:19 +0000233 raise InputError("Port name %s not recognised" % port)
Steve McIntyre3f287882014-08-18 19:02:15 +0100234
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000235 try:
236 self._configure()
237 self._cli("interface %s" % port)
238 self._cli("switchport mode %s" % mode)
239 if mode == "trunk":
240 self._cli("switchport trunk encapsulation dot1q")
241 self._end_configure()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100242
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000243 # Validate it happened
244 read_mode = self.port_get_mode(port)
Steve McIntyre3f287882014-08-18 19:02:15 +0100245
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000246 if read_mode != mode:
247 raise IOError("Failed to set mode for port %s" % port)
248
249 except PExpectError:
250 # recurse on error
251 self._switch_connect()
252 self.port_set_mode(port, mode)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100253
Steve McIntyre9936d002014-10-01 15:54:10 +0100254 # Get the mode of a port: access or trunk
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100255 def port_get_mode(self, port):
Steve McIntyre5fa22652015-04-01 18:01:45 +0100256 logging.debug("Getting mode of port %s", port)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100257 mode = ''
258 if not self._is_port_name_valid(port):
Steve McIntyre72a8bce2015-01-23 18:02:19 +0000259 raise InputError("Port name %s not recognised" % port)
Steve McIntyre9936d002014-10-01 15:54:10 +0100260 regex = re.compile('Administrative Mode: (.*)')
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000261
262 try:
263 self._cli("show interfaces %s switchport" % port)
264 for line in self._read_long_output():
265 match = regex.match(line)
266 if match:
267 mode = match.group(1)
268 if mode == 'static access':
269 return 'access'
270 if mode == 'dynamic auto':
271 return 'trunk'
272 return mode
273
274 except PExpectError:
275 # recurse on error
276 self._switch_connect()
277 return self.port_get_mode(port)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100278
Steve McIntyre9936d002014-10-01 15:54:10 +0100279 # Set an access port to be in a specified VLAN (tag)
280 def port_set_access_vlan(self, port, tag):
Steve McIntyre5fa22652015-04-01 18:01:45 +0100281 logging.debug("Setting access port %s to VLAN %d", port, tag)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100282 if not self._is_port_name_valid(port):
Steve McIntyre72a8bce2015-01-23 18:02:19 +0000283 raise InputError("Port name %s not recognised" % port)
Steve McIntyre9936d002014-10-01 15:54:10 +0100284 if not (self.port_get_mode(port) == "access"):
Steve McIntyre72a8bce2015-01-23 18:02:19 +0000285 raise InputError("Port %s not in access mode" % port)
Steve McIntyre3f287882014-08-18 19:02:15 +0100286
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000287 try:
288 self._configure()
289 self._cli("interface %s" % port)
290 self._cli("switchport access vlan %d" % tag)
291 self._cli("no shutdown")
292 self._end_configure()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100293
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000294 # Finally, validate things worked
295 read_vlan = int(self.port_get_access_vlan(port))
296 if read_vlan != tag:
297 raise IOError("Failed to move access port %d to VLAN %d - got VLAN %d instead"
298 % (port, tag, read_vlan))
299
300 except PExpectError:
301 # recurse on error
302 self._switch_connect()
303 self.port_set_access_vlan(port, tag)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100304
Steve McIntyred6759dd2014-08-12 18:10:00 +0100305 # Add a trunk port to a specified VLAN (tag)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100306 def port_add_trunk_to_vlan(self, port, tag):
Steve McIntyre5fa22652015-04-01 18:01:45 +0100307 logging.debug("Adding trunk port %s to VLAN %d", port, tag)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100308 if not self._is_port_name_valid(port):
Steve McIntyre72a8bce2015-01-23 18:02:19 +0000309 raise InputError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100310 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyre72a8bce2015-01-23 18:02:19 +0000311 raise InputError("Port %s not in trunk mode" % port)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100312
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000313 try:
314 self._configure()
315 self._cli("interface %s" % port)
316 self._cli("switchport trunk allowed vlan add %d" % tag)
317 self._end_configure()
318
319 # Validate it happened
320 read_vlans = self.port_get_trunk_vlan_list(port)
321 for vlan in read_vlans:
322 if vlan == tag or vlan == "ALL":
323 return
324 raise IOError("Failed to add trunk port %s to VLAN %d" % (port, tag))
325
326 except PExpectError:
327 # recurse on error
328 self._switch_connect()
329 self.port_add_trunk_to_vlan(port, tag)
330
Steve McIntyred6759dd2014-08-12 18:10:00 +0100331 # Remove a trunk port from a specified VLAN (tag)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100332 def port_remove_trunk_from_vlan(self, port, tag):
Steve McIntyre5fa22652015-04-01 18:01:45 +0100333 logging.debug("Removing trunk port %s from VLAN %d", port, tag)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100334 if not self._is_port_name_valid(port):
Steve McIntyre72a8bce2015-01-23 18:02:19 +0000335 raise InputError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100336 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyre72a8bce2015-01-23 18:02:19 +0000337 raise InputError("Port %s not in trunk mode" % port)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100338
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000339 try:
340 self._configure()
341 self._cli("interface %s" % port)
342 self._cli("switchport trunk allowed vlan remove %d" % tag)
343 self._end_configure()
344
345 # Validate it happened
346 read_vlans = self.port_get_trunk_vlan_list(port)
347 for vlan in read_vlans:
348 if vlan == tag:
349 raise IOError("Failed to remove trunk port %s from VLAN %d" % (port, tag))
350
351 except PExpectError:
352 # recurse on error
353 self._switch_connect()
354 self.port_remove_trunk_from_vlan(port, tag)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100355
Steve McIntyre9936d002014-10-01 15:54:10 +0100356 # Get the configured VLAN tag for an access port (tag)
357 def port_get_access_vlan(self, port):
Steve McIntyre5fa22652015-04-01 18:01:45 +0100358 logging.debug("Getting VLAN for access port %s", port)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100359 vlan = 1
360 if not self._is_port_name_valid(port):
Steve McIntyre72a8bce2015-01-23 18:02:19 +0000361 raise InputError("Port name %s not recognised" % port)
Steve McIntyre9936d002014-10-01 15:54:10 +0100362 if not (self.port_get_mode(port) == "access"):
Steve McIntyre72a8bce2015-01-23 18:02:19 +0000363 raise InputError("Port %s not in access mode" % port)
Steve McIntyre3f287882014-08-18 19:02:15 +0100364 regex = re.compile('Access Mode VLAN: (\d+)')
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000365
366 try:
367 self._cli("show interfaces %s switchport" % port)
368 for line in self._read_long_output():
369 match = regex.match(line)
370 if match:
371 vlan = match.group(1)
372 return int(vlan)
373
374 except PExpectError:
375 # recurse on error
376 self._switch_connect()
377 return self.port_get_access_vlan(port)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100378
379 # Get the list of configured VLAN tags for a trunk port
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100380 def port_get_trunk_vlan_list(self, port):
Steve McIntyre5fa22652015-04-01 18:01:45 +0100381 logging.debug("Getting VLANs for trunk port %s", port)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100382 vlans = [ ]
383 if not self._is_port_name_valid(port):
Steve McIntyre72a8bce2015-01-23 18:02:19 +0000384 raise InputError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100385 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyre72a8bce2015-01-23 18:02:19 +0000386 raise InputError("Port %s not in trunk mode" % port)
Steve McIntyre3f287882014-08-18 19:02:15 +0100387 regex_start = re.compile('Trunking VLANs Enabled: (.*)')
388 regex_continue = re.compile('\s*(\d.*)')
Steve McIntyre3f287882014-08-18 19:02:15 +0100389
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000390 try:
391 self._cli("show interfaces %s switchport" % port)
392
393 # Horrible parsing work - VLAN list may extend over several lines
394 in_match = False
395 vlan_text = ''
396
397 for line in self._read_long_output():
398 if in_match:
399 match = regex_continue.match(line)
400 if match:
401 vlan_text += match.group(1)
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000402 else:
403 in_match = False
Steve McIntyre3f287882014-08-18 19:02:15 +0100404 else:
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000405 match = regex_start.match(line)
406 if match:
407 vlan_text += match.group(1)
408 in_match = True
Steve McIntyre3f287882014-08-18 19:02:15 +0100409
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000410 vlans = self._parse_vlan_list(vlan_text)
411 return vlans
Steve McIntyre3f287882014-08-18 19:02:15 +0100412
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000413 except PExpectError:
414 # recurse on error
415 self._switch_connect()
416 return self.port_get_trunk_vlan_list(port)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100417
418 ################################
419 ### Internal functions
420 ################################
421
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000422 # Connect to the switch and log in
423 def _switch_connect(self):
424
425 if not self.connection is None:
426 self.connection.close(True)
427 self.connection = None
428
Steve McIntyre5fa22652015-04-01 18:01:45 +0100429 logging.debug("Connecting to Switch with: %s", self.exec_string)
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000430 self.connection = pexpect.spawn(self.exec_string, logfile = self.logfile)
431
432 self._login()
433
434 # Avoid paged output
435 self._cli("terminal length 0")
436
437 # And grab details about the switch. in case we need it
438 self._get_systemdata()
439
440 # And also validate them - make sure we're driving a switch of
441 # the correct model! Also store the serial number
442 descr_regex = re.compile('^cisco\s+(\S+)')
443 sn_regex = re.compile('System serial number\s+:\s+(\S+)')
444 descr = ""
445
446 for line in self._systemdata:
447 match = descr_regex.match(line)
448 if match:
449 descr = match.group(1)
450 match = sn_regex.match(line)
451 if match:
452 self.serial_number = match.group(1)
453
Steve McIntyre5fa22652015-04-01 18:01:45 +0100454 logging.debug("serial number is %s", self.serial_number)
455 logging.debug("system description is %s", descr)
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000456
457 if not self._expected_descr_re.match(descr):
458 raise IOError("Switch %s not recognised by this driver: abort" % descr)
459
460 # Now build a list of our ports, for later sanity checking
461 self._ports = self._get_port_names()
462 if len(self._ports) < 4:
463 raise IOError("Not enough ports detected - problem!")
464
465 def _login(self):
Steve McIntyre5fa22652015-04-01 18:01:45 +0100466 logging.debug("attempting login with username %s, password %s", self._username, self._password)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100467 self.connection.expect('User Access Verification')
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000468 if self._username is not None:
Steve McIntyred6759dd2014-08-12 18:10:00 +0100469 self.connection.expect("User Name:")
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000470 self._cli("%s" % self._username)
471 if self._password is not None:
Steve McIntyred6759dd2014-08-12 18:10:00 +0100472 self.connection.expect("Password:")
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000473 self._cli("%s" % self._password, False)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100474 while True:
475 index = self.connection.expect(['User Name:', 'Password:', 'Bad passwords', 'authentication failed', r'(.*)(#|>)'])
476 if index != 4: # Any other means: failed to log in!
Steve McIntyre5fa22652015-04-01 18:01:45 +0100477 logging.error("Login failure: index %d\n", index)
478 logging.error("Login failure: %s\n", self.connection.match.before)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100479 raise IOError
480
481 # else
Steve McIntyre3f287882014-08-18 19:02:15 +0100482 self._prompt_name = self.connection.match.group(1).strip()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100483 if self.connection.match.group(2) == ">":
484 # Need to enter "enable" mode too
485 self._cli("enable")
Steve McIntyread12cd72015-02-12 06:41:29 +0000486 if self._enable_password is not None:
Steve McIntyred6759dd2014-08-12 18:10:00 +0100487 self.connection.expect("Password:")
Steve McIntyread12cd72015-02-12 06:41:29 +0000488 self._cli("%s" % self._enable_password, False)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100489 index = self.connection.expect(['Password:', 'Bad passwords', 'authentication failed', r'(.*)(#|>)'])
490 if index != 3: # Any other means: failed to log in!
Steve McIntyre5fa22652015-04-01 18:01:45 +0100491 logging.error("Enable password failure: %s\n", self.connection.match)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100492 raise IOError
493 return 0
494
495 def _logout(self):
496 logging.debug("Logging out")
497 self._cli("exit", False)
498
499 def _configure(self):
500 self._cli("configure terminal")
501
502 def _end_configure(self):
503 self._cli("end")
504
Steve McIntyrec68d6d72014-12-24 00:23:36 +0000505 def _read_long_output(self):
Steve McIntyre3f287882014-08-18 19:02:15 +0100506 prompt = self._prompt_name + '#'
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000507 try:
508 self.connection.expect(prompt)
509 except (pexpect.EOF, pexpect.TIMEOUT):
510 # Something went wrong; logout, log in and try again!
Steve McIntyre7a9c8192015-02-12 07:15:52 +0000511 logging.error("PEXPECT FAILURE, RECONNECT")
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000512 self.errors.log_error_in("")
513 raise PExpectError("_read_long_output failed")
514 except:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100515 logging.error("prompt is \"%s\"", prompt)
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000516 raise
517
Steve McIntyre5fa22652015-04-01 18:01:45 +0100518 longbuf = []
Steve McIntyrea7cdefc2014-12-24 00:48:19 +0000519 for line in self.connection.before.split('\r\n'):
Steve McIntyre5fa22652015-04-01 18:01:45 +0100520 longbuf.append(line.strip())
521 return longbuf
Steve McIntyred6759dd2014-08-12 18:10:00 +0100522
523 def _get_port_names(self):
524 logging.debug("Grabbing list of ports")
525 interfaces = []
526
527 # Use "Up" or "Down" to only identify lines in the output that
528 # match interfaces that exist
529 regex = re.compile('^\s*([a-zA-Z0-9_/]*).*(connect)(.*)')
530 regex1 = re.compile('.*Not Present.*')
Steve McIntyre6c279b42014-12-23 22:09:04 +0000531 regex2 = re.compile('.*routed.*')
Steve McIntyred6759dd2014-08-12 18:10:00 +0100532
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000533 try:
534 self._cli("show interfaces status")
535 for line in self._read_long_output():
536 match = regex.match(line)
537 if match:
538 interface = match.group(1)
539 junk = match.group(3)
540 match1 = regex1.match(junk) # Deliberately drop things
541 # marked as "Not Present"
542 match2 = regex2.match(junk) # Deliberately drop things
543 # marked as "routed"
544 if not match1 and not match2:
545 interfaces.append(interface)
546 return interfaces
547
548 except PExpectError:
549 # recurse on error
550 self._switch_connect()
551 return self._get_port_names()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100552
Steve McIntyred6759dd2014-08-12 18:10:00 +0100553 def _show_config(self):
554 logging.debug("Grabbing config")
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000555 try:
556 self._cli("show running-config")
557 return self._read_long_output()
558 except PExpectError:
559 # recurse on error
560 self._switch_connect()
561 return self._show_config()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100562
563 def _show_clock(self):
564 logging.debug("Grabbing time")
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000565 try:
566 self._cli("show clock")
567 return self._read_long_output()
568 except PExpectError:
569 # recurse on error
570 self._switch_connect()
571 return self._show_clock()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100572
Steve McIntyred6759dd2014-08-12 18:10:00 +0100573 def _get_systemdata(self):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100574 logging.debug("Grabbing system sw and hw versions")
Steve McIntyreffb9b5a2014-10-10 16:31:58 +0100575
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000576 try:
577 self._systemdata = []
578 self._cli("show version")
579 for line in self._read_long_output():
580 self._systemdata.append(line)
581
582 except PExpectError:
583 # recurse on error
584 self._switch_connect()
585 return self._get_systemdata()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100586
Steve McIntyre2b4c07b2014-12-22 16:10:04 +0000587 def _parse_vlan_list(self, inputdata):
Steve McIntyre3f287882014-08-18 19:02:15 +0100588 vlans = []
589
Steve McIntyre2b4c07b2014-12-22 16:10:04 +0000590 if inputdata == "ALL":
Steve McIntyre3f287882014-08-18 19:02:15 +0100591 return ["ALL"]
Steve McIntyre2b4c07b2014-12-22 16:10:04 +0000592 elif inputdata == "NONE":
Steve McIntyre3f287882014-08-18 19:02:15 +0100593 return []
594 else:
595 # Parse the complex list
Steve McIntyre2b4c07b2014-12-22 16:10:04 +0000596 groups = inputdata.split(',')
Steve McIntyre3f287882014-08-18 19:02:15 +0100597 for group in groups:
598 subgroups = group.split('-')
599 if len(subgroups) == 1:
600 vlans.append(int(subgroups[0]))
601 elif len(subgroups) == 2:
602 for i in range (int(subgroups[0]), int(subgroups[1]) + 1):
603 vlans.append(i)
604 else:
Steve McIntyre6d5594f2014-12-23 14:28:47 +0000605 logging.debug("Can't parse group \"" + group + "\"")
Steve McIntyre3f287882014-08-18 19:02:15 +0100606
607 return vlans
Steve McIntyred6759dd2014-08-12 18:10:00 +0100608
609 # Wrapper around connection.send - by default, expect() the same
610 # text we've sent, to remove it from the output from the
611 # switch. For the few cases where we don't need that, override
612 # this using echo=False.
613 # Horrible, but seems to work.
614 def _cli(self, text, echo=True):
615 self.connection.send(text + '\r')
616 if echo:
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000617 try:
618 self.connection.expect(text)
619 except (pexpect.EOF, pexpect.TIMEOUT):
620 # Something went wrong; logout, log in and try again!
Steve McIntyre7a9c8192015-02-12 07:15:52 +0000621 logging.error("PEXPECT FAILURE, RECONNECT")
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000622 self.errors.log_error_out(text)
623 raise PExpectError("_cli failed on %s" % text)
624 except:
Steve McIntyrea67473a2015-02-12 08:26:33 +0000625 logging.error("Unexpected error: %s", sys.exc_info()[0])
Steve McIntyre3dfd36f2015-02-12 06:37:56 +0000626 raise
Steve McIntyred6759dd2014-08-12 18:10:00 +0100627
628if __name__ == "__main__":
Steve McIntyre48dc6ae2014-12-23 16:08:19 +0000629
630 import optparse
631
632 switch = 'vlandswitch01'
633 parser = optparse.OptionParser()
634 parser.add_option("--switch",
635 dest = "switch",
636 action = "store",
637 nargs = 1,
638 type = "string",
639 help = "specify switch to connect to for testing",
640 metavar = "<switch>")
641 (opts, args) = parser.parse_args()
642 if opts.switch:
643 switch = opts.switch
644
645 p = CiscoCatalyst(switch, 23, debug=True)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100646 p.switch_connect(None, 'lngvirtual', 'lngenable')
Steve McIntyred6759dd2014-08-12 18:10:00 +0100647
648 print "VLANs are:"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100649 buf = p.vlan_get_list()
Steve McIntyre5fa22652015-04-01 18:01:45 +0100650 p.dump_list(buf)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100651
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100652 buf = p.vlan_get_name(2)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100653 print "VLAN 2 is named \"%s\"" % buf
654
655 print "Create VLAN 3"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100656 p.vlan_create(3)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100657
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100658 buf = p.vlan_get_name(3)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100659 print "VLAN 3 is named \"%s\"" % buf
660
661 print "Set name of VLAN 3 to test333"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100662 p.vlan_set_name(3, "test333")
Steve McIntyred6759dd2014-08-12 18:10:00 +0100663
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100664 buf = p.vlan_get_name(3)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100665 print "VLAN 3 is named \"%s\"" % buf
666
667 print "VLANs are:"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100668 buf = p.vlan_get_list()
Steve McIntyre5fa22652015-04-01 18:01:45 +0100669 p.dump_list(buf)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100670
671 print "Destroy VLAN 3"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100672 p.vlan_destroy(3)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100673
674 print "VLANs are:"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100675 buf = p.vlan_get_list()
Steve McIntyre5fa22652015-04-01 18:01:45 +0100676 p.dump_list(buf)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100677
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100678 buf = p.port_get_mode("Gi1/0/10")
Steve McIntyreb7adc782014-08-13 00:22:21 +0100679 print "Port Gi1/0/10 is in %s mode" % buf
680
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100681 buf = p.port_get_mode("Gi1/0/11")
Steve McIntyreb7adc782014-08-13 00:22:21 +0100682 print "Port Gi1/0/11 is in %s mode" % buf
Steve McIntyred6759dd2014-08-12 18:10:00 +0100683
Steve McIntyre9936d002014-10-01 15:54:10 +0100684 # Test access stuff
685 print "Set Gi1/0/9 to access mode"
686 p.port_set_mode("Gi1/0/9", "access")
Steve McIntyre3f287882014-08-18 19:02:15 +0100687
688 print "Move Gi1/0/9 to VLAN 4"
Steve McIntyre9936d002014-10-01 15:54:10 +0100689 p.port_set_access_vlan("Gi1/0/9", 4)
Steve McIntyre3f287882014-08-18 19:02:15 +0100690
Steve McIntyre9936d002014-10-01 15:54:10 +0100691 buf = p.port_get_access_vlan("Gi1/0/9")
Steve McIntyre3f287882014-08-18 19:02:15 +0100692 print "Read from switch: Gi1/0/9 is on VLAN %s" % buf
693
694 print "Move Gi1/0/9 back to VLAN 1"
Steve McIntyre9936d002014-10-01 15:54:10 +0100695 p.port_set_access_vlan("Gi1/0/9", 1)
Steve McIntyre3f287882014-08-18 19:02:15 +0100696
Steve McIntyre9936d002014-10-01 15:54:10 +0100697 # Test access stuff
Steve McIntyre3f287882014-08-18 19:02:15 +0100698 print "Set Gi1/0/9 to trunk mode"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100699 p.port_set_mode("Gi1/0/9", "trunk")
Steve McIntyre3f287882014-08-18 19:02:15 +0100700 print "Read from switch: which VLANs is Gi1/0/9 on?"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100701 buf = p.port_get_trunk_vlan_list("Gi1/0/9")
Steve McIntyre5fa22652015-04-01 18:01:45 +0100702 p.dump_list(buf)
Steve McIntyre3f287882014-08-18 19:02:15 +0100703 print "Add Gi1/0/9 to VLAN 2"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100704 p.port_add_trunk_to_vlan("Gi1/0/9", 2)
Steve McIntyre3f287882014-08-18 19:02:15 +0100705 print "Add Gi1/0/9 to VLAN 3"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100706 p.port_add_trunk_to_vlan("Gi1/0/9", 3)
Steve McIntyre3f287882014-08-18 19:02:15 +0100707 print "Add Gi1/0/9 to VLAN 4"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100708 p.port_add_trunk_to_vlan("Gi1/0/9", 4)
Steve McIntyre3f287882014-08-18 19:02:15 +0100709 print "Read from switch: which VLANs is Gi1/0/9 on?"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100710 buf = p.port_get_trunk_vlan_list("Gi1/0/9")
Steve McIntyre5fa22652015-04-01 18:01:45 +0100711 p.dump_list(buf)
Steve McIntyre3f287882014-08-18 19:02:15 +0100712
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100713 p.port_remove_trunk_from_vlan("Gi1/0/9", 3)
714 p.port_remove_trunk_from_vlan("Gi1/0/9", 3)
715 p.port_remove_trunk_from_vlan("Gi1/0/9", 4)
Steve McIntyre3f287882014-08-18 19:02:15 +0100716 print "Read from switch: which VLANs is Gi1/0/9 on?"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100717 buf = p.port_get_trunk_vlan_list("Gi1/0/9")
Steve McIntyre5fa22652015-04-01 18:01:45 +0100718 p.dump_list(buf)
Steve McIntyre3f287882014-08-18 19:02:15 +0100719
Steve McIntyre7460d972014-12-23 14:45:30 +0000720# print 'Restarting switch, to explicitly reset config'
721# p.switch_restart()
Steve McIntyre3f287882014-08-18 19:02:15 +0100722
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100723# p.switch_save_running_config()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100724
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100725# p.switch_disconnect()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100726# p._show_config()