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