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