Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 1 | #! /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 | |
| 20 | import logging |
| 21 | import pexpect |
| 22 | import sys |
| 23 | import time |
| 24 | import re |
| 25 | from common import SwitchDriver |
| 26 | |
| 27 | class CiscoSX300(SwitchDriver): |
| 28 | |
| 29 | connection = None |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 30 | |
| 31 | # No extra capabilities for this switch/driver yet |
| 32 | _capabilities = [ |
| 33 | ] |
| 34 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 35 | # Regexp of expected hardware information - fail if we don't see |
| 36 | # this |
Steve McIntyre | f169932 | 2014-08-19 22:49:43 +0100 | [diff] [blame] | 37 | _expected_descr_re = re.compile('S.300-\d+') |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 38 | |
| 39 | logfile = sys.stderr |
Steve McIntyre | 16f0216 | 2014-08-14 17:47:36 +0100 | [diff] [blame] | 40 | logfile = None |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 41 | |
| 42 | def __init__(self, switch_hostname, switch_telnetport=23): |
| 43 | self.exec_string = "/usr/bin/telnet %s %d" % (switch_hostname, switch_telnetport) |
| 44 | |
| 45 | ################################ |
| 46 | ### Switch-level API functions |
| 47 | ################################ |
| 48 | |
| 49 | # Connect to the switch and log in |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 50 | def switch_connect(self, username, password): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 51 | logging.debug("Connecting to Switch with: %s" % self.exec_string) |
| 52 | self.connection = pexpect.spawn(self.exec_string, logfile = self.logfile) |
| 53 | self._login(username, password) |
| 54 | |
Steve McIntyre | df9c2c9 | 2014-08-12 18:14:49 +0100 | [diff] [blame] | 55 | # Try to avoid paged output |
Steve McIntyre | 7ced073 | 2014-08-12 18:07:56 +0100 | [diff] [blame] | 56 | self.connection.setwinsize(132,1000) |
| 57 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 58 | # And grab details about the switch. in case we need it |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 59 | self._get_systemdata() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 60 | |
| 61 | # And also validate them - make sure we're driving a switch of |
| 62 | # the correct model! Also store the serial number |
| 63 | descr_regex = re.compile('System Description:.\s+(\S.*)') |
| 64 | sn_regex = re.compile('SN:\s+(\S_)') |
| 65 | descr = "" |
| 66 | |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 67 | for line in self._systemdata: |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 68 | match = descr_regex.match(line) |
| 69 | if match: |
| 70 | descr = match.group(1) |
| 71 | match = sn_regex.match(line) |
| 72 | if match: |
| 73 | self.serial_number = match.group(1) |
| 74 | |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 75 | if not self._expected_descr_re.match(descr): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 76 | raise IOError("Switch %s not recognised by this driver: abort" % descr) |
| 77 | |
Steve McIntyre | 28d34ca | 2014-08-12 15:40:24 +0100 | [diff] [blame] | 78 | # Now build a list of our ports, for later sanity checking |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 79 | self._ports = self._get_port_names() |
| 80 | if len(self._ports) < 4: |
Steve McIntyre | 28d34ca | 2014-08-12 15:40:24 +0100 | [diff] [blame] | 81 | raise IOError("Not enough ports detected - problem!") |
| 82 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 83 | # Log out of the switch and drop the connection and all state |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 84 | def switch_disconnect(self): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 85 | self._logout() |
| 86 | logging.debug("Closing connection: %s" % self.connection) |
| 87 | self.connection.close(True) |
| 88 | del(self) |
| 89 | |
| 90 | # Save the current running config into flash - we want config to |
| 91 | # remain across reboots |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 92 | def switch_save_running_config(self): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 93 | self._cli("copy running-config startup-config") |
| 94 | self.connection.expect("Y/N") |
| 95 | self._cli("y") |
| 96 | self.connection.expect("Copy succeeded") |
| 97 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 98 | ################################ |
| 99 | ### VLAN API functions |
| 100 | ################################ |
| 101 | |
| 102 | # Create a VLAN with the specified tag |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 103 | def vlan_create(self, tag): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 104 | logging.debug("Creating VLAN %d" % tag) |
| 105 | self._configure() |
| 106 | self._cli("vlan database") |
| 107 | self._cli("vlan %d" % tag) |
| 108 | self._end_configure() |
| 109 | |
| 110 | # Validate it happened |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 111 | vlans = self.vlan_get_list() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 112 | for vlan in vlans: |
| 113 | if vlan == tag: |
| 114 | return |
| 115 | raise IOError("Failed to create VLAN %d" % tag) |
| 116 | |
| 117 | # Destroy a VLAN with the specified tag |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 118 | def vlan_destroy(self, tag): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 119 | logging.debug("Destroying VLAN %d" % tag) |
| 120 | self._configure() |
| 121 | self._cli("no vlan %d" % tag) |
| 122 | self._end_configure() |
| 123 | |
| 124 | # Validate it happened |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 125 | vlans = self.vlan_get_list() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 126 | for vlan in vlans: |
| 127 | if vlan == tag: |
| 128 | raise IOError("Failed to destroy VLAN %d" % tag) |
| 129 | |
| 130 | # Set the name of a VLAN |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 131 | def vlan_set_name(self, tag, name): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 132 | logging.debug("Setting name of VLAN %d to %s" % (tag, name)) |
| 133 | self._configure() |
| 134 | self._cli("vlan %d" % tag) |
| 135 | self._cli("interface vlan %d" % tag) |
| 136 | self._cli("name %s" % name) |
| 137 | self._end_configure() |
| 138 | |
| 139 | # Validate it happened |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 140 | read_name = self.vlan_get_name(tag) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 141 | if read_name != name: |
| 142 | raise IOError("Failed to set name for VLAN %d (name found is \"%s\", not \"%s\")" |
| 143 | % (tag, read_name, name)) |
| 144 | |
| 145 | # Get a list of the VLAN tags currently registered on the switch |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 146 | def vlan_get_list(self): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 147 | logging.debug("Grabbing list of VLANs") |
| 148 | vlans = [] |
| 149 | |
| 150 | regex = re.compile('^ *(\d+).*(D|S|G|R)') |
| 151 | |
| 152 | self._cli("show vlan") |
| 153 | for line in self._read_paged_output(): |
| 154 | match = regex.match(line) |
| 155 | if match: |
| 156 | vlans.append(int(match.group(1))) |
| 157 | return vlans |
| 158 | |
| 159 | # For a given VLAN tag, ask the switch what the associated name is |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 160 | def vlan_get_name(self, tag): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 161 | logging.debug("Grabbing the name of VLAN %d" % tag) |
| 162 | name = None |
| 163 | regex = re.compile('^ *\d+\s+(\S+).*(D|S|G|R)') |
| 164 | self._cli("show vlan tag %d" % tag) |
| 165 | for line in self._read_paged_output(): |
| 166 | match = regex.match(line) |
| 167 | if match: |
| 168 | name = match.group(1) |
| 169 | name.strip() |
| 170 | return name |
| 171 | |
| 172 | |
| 173 | ################################ |
| 174 | ### Port API functions |
| 175 | ################################ |
| 176 | |
| 177 | # Set the mode of a port: general or trunk |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 178 | def port_set_mode(self, port, mode): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 179 | logging.debug("Setting port %s to %s" % (port, mode)) |
| 180 | if not self._is_port_mode_valid(mode): |
| 181 | raise IndexError("Port mode %s is not allowed" % mode) |
| 182 | if not self._is_port_name_valid(port): |
| 183 | raise IndexError("Port name %s not recognised" % port) |
| 184 | self._configure() |
| 185 | self._cli("interface %s" % port) |
| 186 | self._cli("switchport mode %s" % mode) |
| 187 | self._end_configure() |
| 188 | |
| 189 | # Validate it happened |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 190 | read_mode = self.port_get_mode(port) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 191 | if read_mode != mode: |
| 192 | raise IOError("Failed to set mode for port %s" % port) |
| 193 | |
| 194 | |
| 195 | # Get the mode of a port: general or trunk |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 196 | def port_get_mode(self, port): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 197 | logging.debug("Getting mode of port %s" % port) |
| 198 | mode = '' |
| 199 | if not self._is_port_name_valid(port): |
| 200 | raise IndexError("Port name %s not recognised" % port) |
| 201 | regex = re.compile('Port Mode: (\S+)') |
| 202 | self._cli("show interfaces switchport %s" % port) |
| 203 | for line in self._read_paged_output(): |
| 204 | match = regex.match(line) |
| 205 | if match: |
| 206 | mode = match.group(1) |
| 207 | return mode.lower() |
| 208 | |
Steve McIntyre | 16f0216 | 2014-08-14 17:47:36 +0100 | [diff] [blame] | 209 | # Set a general port to be in a specified VLAN (tag) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 210 | def port_set_general_vlan(self, port, tag): |
Steve McIntyre | 16f0216 | 2014-08-14 17:47:36 +0100 | [diff] [blame] | 211 | logging.debug("Setting general port %s to VLAN %d" % (port, tag)) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 212 | if not self._is_port_name_valid(port): |
| 213 | raise IndexError("Port name %s not recognised" % port) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 214 | if not (self.port_get_mode(port) == "general"): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 215 | raise IndexError("Port %s not in general mode" % port) |
| 216 | |
Steve McIntyre | 16f0216 | 2014-08-14 17:47:36 +0100 | [diff] [blame] | 217 | # More complicated than just a "set" on the hardware, so let's |
| 218 | # split it up into separate helper calls. |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 219 | |
Steve McIntyre | 16f0216 | 2014-08-14 17:47:36 +0100 | [diff] [blame] | 220 | # First, get the current VLAN |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 221 | current_vlan = self.port_get_general_vlan(port) |
Steve McIntyre | 16f0216 | 2014-08-14 17:47:36 +0100 | [diff] [blame] | 222 | |
| 223 | # Next, drop off that current VLAN |
| 224 | # VLAN 1 is handled specially :-( |
| 225 | if current_vlan == 1: |
| 226 | self._port_general_block_default_vlan(port) |
| 227 | else: |
| 228 | self._port_remove_general_from_vlan(port, current_vlan) |
| 229 | |
| 230 | # Next, add the desired VLAN |
| 231 | # VLAN 1 is handled specially again :-( |
| 232 | if tag == 1: |
| 233 | self._port_general_allow_default_vlan(port) |
| 234 | else: |
| 235 | self._port_add_general_to_vlan(port, tag) |
| 236 | |
| 237 | # Finally, validate things worked |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 238 | read_vlan = int(self.port_get_general_vlan(port)) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 239 | if read_vlan != tag: |
Steve McIntyre | 5b3ce21 | 2014-08-15 13:10:41 +0100 | [diff] [blame] | 240 | raise IOError("Failed to move general port %s to VLAN %d - got VLAN %d instead" |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 241 | % (port, tag, read_vlan)) |
| 242 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 243 | # Add a trunk port to a specified VLAN (tag) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 244 | def port_add_trunk_to_vlan(self, port, tag): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 245 | logging.debug("Adding trunk port %s to VLAN %d" % (port, tag)) |
| 246 | if not self._is_port_name_valid(port): |
| 247 | raise IndexError("Port name %s not recognised" % port) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 248 | if not (self.port_get_mode(port) == "trunk"): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 249 | raise IndexError("Port %s not in trunk mode" % port) |
| 250 | self._configure() |
| 251 | self._cli("interface %s" % port) |
| 252 | self._cli("switchport trunk allowed vlan add %d" % tag) |
| 253 | self._end_configure() |
| 254 | |
| 255 | # Validate it happened |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 256 | read_vlans = self.port_get_trunk_vlan_list(port) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 257 | for vlan in read_vlans: |
| 258 | if vlan == tag: |
| 259 | return |
Steve McIntyre | 5b3ce21 | 2014-08-15 13:10:41 +0100 | [diff] [blame] | 260 | raise IOError("Failed to add trunk port %s to VLAN %d" % (port, tag)) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 261 | |
| 262 | # Remove a trunk port from a specified VLAN (tag) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 263 | def port_remove_trunk_from_vlan(self, port, tag): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 264 | logging.debug("Removing trunk port %s from VLAN %d" % (port, tag)) |
| 265 | if not self._is_port_name_valid(port): |
| 266 | raise IndexError("Port name %s not recognised" % port) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 267 | if not (self.port_get_mode(port) == "trunk"): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 268 | raise IndexError("Port %s not in trunk mode" % port) |
| 269 | self._configure() |
| 270 | self._cli("interface %s" % port) |
| 271 | self._cli("switchport trunk allowed vlan remove %d" % tag) |
| 272 | self._end_configure() |
| 273 | |
| 274 | # Validate it happened |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 275 | read_vlans = self.port_get_trunk_vlan_list(port) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 276 | for vlan in read_vlans: |
| 277 | if vlan == tag: |
Steve McIntyre | 5b3ce21 | 2014-08-15 13:10:41 +0100 | [diff] [blame] | 278 | raise IOError("Failed to remove trunk port %s from VLAN %d" % (port, tag)) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 279 | |
| 280 | # Get the configured VLAN tag for an general port (tag) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 281 | def port_get_general_vlan(self, port): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 282 | logging.debug("Getting VLAN for general port %s" % port) |
| 283 | vlan = 1 |
| 284 | if not self._is_port_name_valid(port): |
| 285 | raise IndexError("Port name %s not recognised" % port) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 286 | if not (self.port_get_mode(port) == "general"): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 287 | raise IndexError("Port %s not in general mode" % port) |
| 288 | regex = re.compile('(\d+)\s+\S+\s+Untagged\s+Static') |
| 289 | self._cli("show interfaces switchport %s" % port) |
| 290 | for line in self._read_paged_output(): |
| 291 | match = regex.match(line) |
| 292 | if match: |
| 293 | vlan = match.group(1) |
| 294 | return int(vlan) |
| 295 | |
| 296 | # Get the list of configured VLAN tags for a trunk port |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 297 | def port_get_trunk_vlan_list(self, port): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 298 | logging.debug("Getting VLANs for trunk port %s" % port) |
| 299 | vlans = [ ] |
| 300 | if not self._is_port_name_valid(port): |
| 301 | raise IndexError("Port name %s not recognised" % port) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 302 | if not (self.port_get_mode(port) == "trunk"): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 303 | raise IndexError("Port %s not in general mode" % port) |
| 304 | regex = re.compile('(\d+)\s+\S+\s+(Tagged|Untagged)\s+Static') |
| 305 | self._cli("show interfaces switchport %s" % port) |
| 306 | for line in self._read_paged_output(): |
| 307 | match = regex.match(line) |
| 308 | if match: |
| 309 | vlans.append (int(match.group(1))) |
| 310 | return vlans |
| 311 | |
| 312 | ################################ |
| 313 | ### Internal functions |
| 314 | ################################ |
| 315 | |
| 316 | def _login(self, username, password): |
| 317 | logging.debug("attempting login with username %s, password %s" % (username, password)) |
| 318 | self._cli("") |
| 319 | self.connection.expect("User Name:") |
| 320 | self._cli("%s" % username) |
| 321 | self.connection.expect("Password:") |
| 322 | self._cli("%s" % password, False) |
| 323 | while True: |
| 324 | index = self.connection.expect(['User Name:', 'authentication failed', r'(.*)#', '.*']) |
| 325 | if index == 0 or index == 1: # Failed to log in! |
| 326 | logging.error("Login failure: %s\n" % self.connection.match) |
| 327 | raise IOError |
| 328 | elif index == 2: |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 329 | self._prompt_name = self.connection.match.group(1).strip() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 330 | return 0 |
| 331 | |
| 332 | def _logout(self): |
| 333 | logging.debug("Logging out") |
| 334 | self._cli("exit", False) |
| 335 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 336 | def _configure(self): |
| 337 | self._cli("configure terminal") |
| 338 | |
| 339 | def _end_configure(self): |
| 340 | self._cli("end") |
| 341 | |
| 342 | def _read_paged_output(self): |
| 343 | buf = [] |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 344 | prompt = self._prompt_name + '#' |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 345 | while True: |
| 346 | index = self.connection.expect(['\x1b\[0mMore:.*<return>.*$', prompt]) |
| 347 | if index == 0: # More: <space> |
| 348 | for line in self.connection.before.split('\r\n'): |
| 349 | buf.append(line.strip()) |
| 350 | self._cli(' ', False) |
| 351 | elif index == 1: # Back to a prompt, says output is finished |
| 352 | break |
| 353 | |
| 354 | for line in self.connection.before.split('\r\n'): |
| 355 | buf.append(line.strip()) |
| 356 | |
| 357 | return buf |
| 358 | |
| 359 | def _get_port_names(self): |
| 360 | logging.debug("Grabbing list of ports") |
| 361 | interfaces = [] |
| 362 | |
| 363 | # Use "Up" or "Down" to only identify lines in the output that |
| 364 | # match interfaces that exist |
| 365 | regex = re.compile('^(\w+).*(Up|Down)') |
| 366 | |
| 367 | self._cli("show interfaces status detailed") |
| 368 | for line in self._read_paged_output(): |
| 369 | match = regex.match(line) |
| 370 | if match: |
| 371 | interfaces.append(match.group(1)) |
| 372 | return interfaces |
| 373 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 374 | def _show_config(self): |
| 375 | logging.debug("Grabbing config") |
| 376 | self._cli("show running-config") |
| 377 | return self._read_paged_output() |
| 378 | |
| 379 | def _show_clock(self): |
| 380 | logging.debug("Grabbing time") |
| 381 | self._cli("show clock") |
| 382 | return self._read_paged_output() |
| 383 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 384 | def _get_systemdata(self): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 385 | |
Steve McIntyre | 28d34ca | 2014-08-12 15:40:24 +0100 | [diff] [blame] | 386 | logging.debug("Grabbing system data") |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 387 | self._cli("show system") |
| 388 | for line in self._read_paged_output(): |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 389 | self._systemdata.append(line) |
Steve McIntyre | 28d34ca | 2014-08-12 15:40:24 +0100 | [diff] [blame] | 390 | |
| 391 | logging.debug("Grabbing system sw and hw versions") |
| 392 | self._cli("show version") |
| 393 | for line in self._read_paged_output(): |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 394 | self._systemdata.append(line) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 395 | |
Steve McIntyre | 16f0216 | 2014-08-14 17:47:36 +0100 | [diff] [blame] | 396 | ###################################### |
| 397 | # Internal port access helper methods |
| 398 | ###################################### |
| 399 | # N.B. No parameter checking here, for speed reasons - if you're |
| 400 | # calling this internal API then you should already have validated |
| 401 | # things yourself! Equally, no post-set checks in here - do that |
| 402 | # at the higher level. |
| 403 | ###################################### |
| 404 | |
| 405 | # Allow the default VLAN on a port |
| 406 | def _port_general_allow_default_vlan(self, port): |
| 407 | logging.debug("Allowing default VLAN (1) for general port %s" % port) |
| 408 | self._configure() |
| 409 | self._cli("interface %s" % port) |
| 410 | self._cli("no switchport forbidden default-vlan") |
| 411 | self._end_configure() |
| 412 | # Difficult to validate |
| 413 | |
| 414 | # Block the default VLAN on a port |
| 415 | def _port_general_block_default_vlan(self, port): |
| 416 | logging.debug("Blocking default VLAN (1) for general port %s" % port) |
| 417 | self._configure() |
| 418 | self._cli("interface %s" % port) |
| 419 | self._cli("switchport forbidden default-vlan") |
| 420 | self._end_configure() |
| 421 | # Difficult to validate |
| 422 | |
| 423 | # Add a general port to a specified VLAN (tag) |
| 424 | def _port_add_general_to_vlan(self, port, tag): |
| 425 | logging.debug("Adding general port %s to VLAN %d" % (port, tag)) |
| 426 | self._configure() |
| 427 | self._cli("interface %s" % port) |
| 428 | self._cli("switchport general pvid %d" % tag) |
| 429 | self._cli("switchport general allowed vlan add %d untagged" % tag) |
| 430 | self._end_configure() |
| 431 | |
| 432 | # Remove a general port from a specified VLAN (tag) |
| 433 | def _port_remove_general_from_vlan(self, port, tag): |
| 434 | logging.debug("Removing general port %s from VLAN %d" % (port, tag)) |
| 435 | self._configure() |
| 436 | self._cli("interface %s" % port) |
| 437 | self._cli("no switchport general pvid") |
| 438 | self._cli("switchport general allowed vlan remove %d" % tag) |
| 439 | self._end_configure() |
| 440 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 441 | # Wrapper around connection.send - by default, expect() the same |
| 442 | # text we've sent, to remove it from the output from the |
| 443 | # switch. For the few cases where we don't need that, override |
| 444 | # this using echo=False. |
| 445 | # Horrible, but seems to work. |
| 446 | def _cli(self, text, echo=True): |
| 447 | self.connection.send(text + '\r') |
| 448 | if echo: |
| 449 | self.connection.expect(text) |
| 450 | |
| 451 | if __name__ == "__main__": |
Steve McIntyre | f169932 | 2014-08-19 22:49:43 +0100 | [diff] [blame] | 452 | # p = CiscoSX300('10.172.2.52', 23) |
| 453 | p = CiscoSX300('10.0.3.15', 23) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 454 | p.switch_connect('cisco', 'cisco') |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 455 | #buf = p._show_clock() |
| 456 | #print "%s" % buf |
| 457 | #buf = p._show_config() |
| 458 | #p._dump_list(buf) |
| 459 | |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 460 | print "System data:" |
| 461 | p._dump_list(p._systemdata) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 462 | |
| 463 | print "Creating VLANs for testing:" |
| 464 | for i in [ 2, 3, 4, 5, 20 ]: |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 465 | p.vlan_create(i) |
| 466 | p.vlan_set_name(i, "test%d" % i) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 467 | print " %d (test%d)" % (i, i) |
| 468 | |
| 469 | #print "And dump config\n" |
| 470 | #buf = p._show_config() |
| 471 | #print "%s" % buf |
| 472 | |
| 473 | #print "Destroying VLAN 2\n" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 474 | #p.vlan_destroy(2) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 475 | |
| 476 | #print "And dump config\n" |
| 477 | #buf = p._show_config() |
| 478 | #print "%s" % buf |
| 479 | |
| 480 | #print "Port names are:" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 481 | #buf = p.switch_get_port_names() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 482 | #p._dump_list(buf) |
| 483 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 484 | #buf = p.vlan_get_name(25) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 485 | #print "VLAN with tag 25 is called \"%s\"" % buf |
| 486 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 487 | #p.vlan_set_name(35, "foo") |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 488 | #print "VLAN with tag 35 is called \"foo\"" |
| 489 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 490 | #buf = p.port_get_mode("fa12") |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 491 | #print "Port fa12 is in %s mode" % buf |
| 492 | |
| 493 | # Test general stuff |
| 494 | print "Set fa6 to general mode" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 495 | p.port_set_mode("fa6", "general") |
Steve McIntyre | 16f0216 | 2014-08-14 17:47:36 +0100 | [diff] [blame] | 496 | print "Move fa6 to VLAN 2" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 497 | p.port_set_general_vlan("fa6", 2) |
| 498 | buf = p.port_get_general_vlan("fa6") |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 499 | print "Read from switch: fa6 is on VLAN %s" % buf |
Steve McIntyre | 16f0216 | 2014-08-14 17:47:36 +0100 | [diff] [blame] | 500 | print "Move fa6 back to default VLAN 1" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 501 | p.port_set_general_vlan("fa6", 1) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 502 | #print "And move fa6 back to a trunk port" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 503 | #p.port_set_mode("fa6", "trunk") |
| 504 | #buf = p.port_get_mode("fa6") |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 505 | #print "Port fa6 is in %s mode" % buf |
| 506 | |
| 507 | # Test trunk stuff |
| 508 | print "Set gi2 to trunk mode" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 509 | p.port_set_mode("gi2", "trunk") |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 510 | print "Add gi2 to VLAN 2" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 511 | p.port_add_trunk_to_vlan("gi2", 2) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 512 | print "Add gi2 to VLAN 3" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 513 | p.port_add_trunk_to_vlan("gi2", 3) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 514 | print "Add gi2 to VLAN 4" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 515 | p.port_add_trunk_to_vlan("gi2", 4) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 516 | print "Read from switch: which VLANs is gi2 on?" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 517 | buf = p.port_get_trunk_vlan_list("gi2") |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 518 | p._dump_list(buf) |
| 519 | |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 520 | print "Remove gi2 from VLANs 3,3,4" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 521 | p.port_remove_trunk_from_vlan("gi2", 3) |
| 522 | p.port_remove_trunk_from_vlan("gi2", 3) |
| 523 | p.port_remove_trunk_from_vlan("gi2", 4) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 524 | print "Read from switch: which VLANs is gi2 on?" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 525 | buf = p.port_get_trunk_vlan_list("gi2") |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 526 | p._dump_list(buf) |
| 527 | |
| 528 | # print "Adding lots of ports to VLANs" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 529 | # p.port_add_trunk_to_vlan("fa1", 2) |
| 530 | # p.port_add_trunk_to_vlan("fa3", 2) |
| 531 | # p.port_add_trunk_to_vlan("fa5", 2) |
| 532 | # p.port_add_trunk_to_vlan("fa7", 2) |
| 533 | # p.port_add_trunk_to_vlan("fa9", 2) |
| 534 | # p.port_add_trunk_to_vlan("fa11", 2) |
| 535 | # p.port_add_trunk_to_vlan("fa13", 2) |
| 536 | # p.port_add_trunk_to_vlan("fa15", 2) |
| 537 | # p.port_add_trunk_to_vlan("fa17", 2) |
| 538 | # p.port_add_trunk_to_vlan("fa19", 2) |
| 539 | # p.port_add_trunk_to_vlan("fa21", 2) |
| 540 | # p.port_add_trunk_to_vlan("fa23", 2) |
| 541 | # p.port_add_trunk_to_vlan("gi4", 2) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 542 | |
| 543 | print "VLANs are:" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 544 | buf = p.vlan_get_list() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 545 | p._dump_list(buf) |
| 546 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 547 | # p.switch_save_running_config() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 548 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame^] | 549 | p.switch_disconnect() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 550 | # p._show_config() |
| 551 | |