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