Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
Steve McIntyre | 94ef65e | 2015-09-25 01:08:14 +0100 | [diff] [blame] | 3 | # Copyright 2014-2015 Linaro Limited |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 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 |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 21 | import sys |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 22 | import re |
Steve McIntyre | 4267c6a | 2018-01-24 16:57:28 +0000 | [diff] [blame] | 23 | import pexpect |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 24 | |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 25 | if __name__ == '__main__': |
Steve McIntyre | a67473a | 2015-02-12 08:26:33 +0000 | [diff] [blame] | 26 | import os |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 27 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 28 | sys.path.insert(0, vlandpath) |
| 29 | sys.path.insert(0, "%s/.." % vlandpath) |
| 30 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 31 | from errors import InputError, PExpectError |
Steve McIntyre | 17c421c | 2015-04-29 14:37:36 +0100 | [diff] [blame] | 32 | from drivers.common import SwitchDriver, SwitchErrors |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 33 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 34 | class CiscoSX300(SwitchDriver): |
| 35 | |
| 36 | connection = None |
Steve McIntyre | bbd2ac8 | 2015-02-12 03:08:48 +0000 | [diff] [blame] | 37 | _username = None |
| 38 | _password = None |
| 39 | _enable_password = None |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 40 | |
| 41 | # No extra capabilities for this switch/driver yet |
| 42 | _capabilities = [ |
| 43 | ] |
| 44 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 45 | # Regexp of expected hardware information - fail if we don't see |
| 46 | # this |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 47 | _expected_descr_re = re.compile(r'.*\d+-Port.*Managed Switch.*') |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 48 | |
Steve McIntyre | 48dc6ae | 2014-12-23 16:08:19 +0000 | [diff] [blame] | 49 | def __init__(self, switch_hostname, switch_telnetport=23, debug = False): |
Steve McIntyre | bb58a27 | 2015-07-14 15:39:54 +0100 | [diff] [blame] | 50 | SwitchDriver.__init__(self, switch_hostname, debug) |
| 51 | self._systemdata = [] |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 52 | self.exec_string = "/usr/bin/telnet %s %d" % (switch_hostname, switch_telnetport) |
Steve McIntyre | 3c3f4fc | 2015-02-12 04:26:27 +0000 | [diff] [blame] | 53 | self.errors = SwitchErrors() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 54 | |
| 55 | ################################ |
| 56 | ### Switch-level API functions |
| 57 | ################################ |
| 58 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 59 | # Save the current running config into flash - we want config to |
| 60 | # remain across reboots |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 61 | def switch_save_running_config(self): |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 62 | try: |
| 63 | self._cli("copy running-config startup-config") |
| 64 | self.connection.expect("Y/N") |
| 65 | self._cli("y") |
| 66 | self.connection.expect("succeeded") |
Steve McIntyre | 2d84e52 | 2015-02-12 06:44:42 +0000 | [diff] [blame] | 67 | except (PExpectError, pexpect.EOF, pexpect.TIMEOUT): |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 68 | # recurse on error |
| 69 | self._switch_connect() |
| 70 | self.switch_save_running_config() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 71 | |
Steve McIntyre | 095b445 | 2014-12-19 17:53:43 +0000 | [diff] [blame] | 72 | # Restart the switch - we need to reload config to do a |
| 73 | # roll-back. Do NOT save running-config first if the switch asks - |
| 74 | # we're trying to dump recent changes, not save them. |
| 75 | # |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 76 | # This will also implicitly cause a connection to be closed |
Steve McIntyre | 095b445 | 2014-12-19 17:53:43 +0000 | [diff] [blame] | 77 | def switch_restart(self): |
| 78 | self._cli("reload") |
| 79 | index = self.connection.expect(['Are you sure', 'will reset']) |
| 80 | if index == 0: |
| 81 | self._cli("y") # Yes, continue without saving |
| 82 | self.connection.expect("reset the whole") |
| 83 | |
| 84 | # Fall through |
| 85 | self._cli("y") # Yes, continue to reset |
| 86 | self.connection.close(True) |
| 87 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 88 | ################################ |
| 89 | ### VLAN API functions |
| 90 | ################################ |
| 91 | |
| 92 | # Create a VLAN with the specified tag |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 93 | def vlan_create(self, tag): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 94 | logging.debug("Creating VLAN %d", tag) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 95 | |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 96 | try: |
| 97 | self._configure() |
| 98 | self._cli("vlan database") |
| 99 | self._cli("vlan %d" % tag) |
| 100 | self._end_configure() |
| 101 | |
| 102 | # Validate it happened |
| 103 | vlans = self.vlan_get_list() |
| 104 | for vlan in vlans: |
| 105 | if vlan == tag: |
| 106 | return |
Steve McIntyre | 661af76 | 2015-02-12 06:18:07 +0000 | [diff] [blame] | 107 | raise IOError("Failed to create VLAN %d" % tag) |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 108 | |
| 109 | except PExpectError: |
| 110 | # recurse on error |
| 111 | self._switch_connect() |
| 112 | self.vlan_create(tag) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 113 | |
| 114 | # Destroy a VLAN with the specified tag |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 115 | def vlan_destroy(self, tag): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 116 | logging.debug("Destroying VLAN %d", tag) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 117 | |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 118 | try: |
| 119 | self._configure() |
| 120 | self._cli("no vlan %d" % tag) |
| 121 | self._end_configure() |
| 122 | |
| 123 | # Validate it happened |
| 124 | vlans = self.vlan_get_list() |
| 125 | for vlan in vlans: |
| 126 | if vlan == tag: |
| 127 | raise IOError("Failed to destroy VLAN %d" % tag) |
| 128 | |
| 129 | except PExpectError: |
| 130 | # recurse on error |
| 131 | self._switch_connect() |
| 132 | self.vlan_destroy(tag) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 133 | |
| 134 | # Set the name of a VLAN |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 135 | def vlan_set_name(self, tag, name): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 136 | logging.debug("Setting name of VLAN %d to %s", tag, name) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 137 | |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 138 | try: |
| 139 | self._configure() |
| 140 | self._cli("vlan %d" % tag) |
| 141 | self._cli("interface vlan %d" % tag) |
| 142 | self._cli("name %s" % name) |
| 143 | self._end_configure() |
| 144 | |
| 145 | # Validate it happened |
| 146 | read_name = self.vlan_get_name(tag) |
| 147 | if read_name != name: |
| 148 | raise IOError("Failed to set name for VLAN %d (name found is \"%s\", not \"%s\")" |
| 149 | % (tag, read_name, name)) |
| 150 | |
| 151 | except PExpectError: |
| 152 | # recurse on error |
| 153 | self._switch_connect() |
| 154 | self.vlan_set_name(tag, name) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 155 | |
| 156 | # Get a list of the VLAN tags currently registered on the switch |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 157 | def vlan_get_list(self): |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 158 | logging.debug("Grabbing list of VLANs") |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 159 | |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 160 | try: |
| 161 | vlans = [] |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 162 | regex = re.compile(r'^ *(\d+).*(D|S|G|R)') |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 163 | |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 164 | self._cli("show vlan") |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 165 | for line in self._read_long_output("show vlan"): |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 166 | match = regex.match(line) |
| 167 | if match: |
| 168 | vlans.append(int(match.group(1))) |
| 169 | return vlans |
| 170 | |
| 171 | except PExpectError: |
| 172 | # recurse on error |
| 173 | self._switch_connect() |
| 174 | return self.vlan_get_list() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 175 | |
| 176 | # 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] | 177 | def vlan_get_name(self, tag): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 178 | logging.debug("Grabbing the name of VLAN %d", tag) |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 179 | |
| 180 | try: |
| 181 | name = None |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 182 | regex = re.compile(r'^ *\d+\s+(\S+).*(D|S|G|R)') |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 183 | self._cli("show vlan tag %d" % tag) |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 184 | for line in self._read_long_output("show vlan tag"): |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 185 | match = regex.match(line) |
| 186 | if match: |
| 187 | name = match.group(1) |
| 188 | name.strip() |
| 189 | return name |
| 190 | |
| 191 | except PExpectError: |
| 192 | # recurse on error |
| 193 | self._switch_connect() |
| 194 | return self.vlan_get_name(tag) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 195 | |
| 196 | ################################ |
| 197 | ### Port API functions |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 198 | ################################ |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 199 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 200 | # Set the mode of a port: access or trunk |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 201 | def port_set_mode(self, port, mode): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 202 | logging.debug("Setting port %s to %s", port, mode) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 203 | if not self._is_port_mode_valid(mode): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 204 | raise InputError("Port mode %s is not allowed" % mode) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 205 | if not self._is_port_name_valid(port): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 206 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 207 | |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 208 | try: |
| 209 | self._configure() |
| 210 | self._cli("interface %s" % port) |
| 211 | self._cli("switchport mode %s" % mode) |
| 212 | self._end_configure() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 213 | |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 214 | # Validate it happened |
Steve McIntyre | b79cea5 | 2015-07-24 17:03:31 +0100 | [diff] [blame] | 215 | read_mode = self._port_get_mode(port) |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 216 | if read_mode != mode: |
| 217 | raise IOError("Failed to set mode for port %s" % port) |
| 218 | |
Steve McIntyre | 1a7bad5 | 2015-07-20 15:21:42 +0100 | [diff] [blame] | 219 | # And cache the result |
| 220 | self._port_modes[port] = mode |
| 221 | |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 222 | except PExpectError: |
| 223 | # recurse on error |
| 224 | self._switch_connect() |
| 225 | self.port_set_mode(port, mode) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 226 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 227 | # Set an access port to be in a specified VLAN (tag) |
| 228 | def port_set_access_vlan(self, port, tag): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 229 | logging.debug("Setting access port %s to VLAN %d", port, tag) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 230 | if not self._is_port_name_valid(port): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 231 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 232 | if not (self.port_get_mode(port) == "access"): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 233 | raise InputError("Port %s not in access mode" % port) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 234 | |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 235 | try: |
| 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 | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 241 | # Validate things worked |
| 242 | read_vlan = int(self.port_get_access_vlan(port)) |
| 243 | if read_vlan != tag: |
| 244 | raise IOError("Failed to move access port %s to VLAN %d - got VLAN %d instead" |
| 245 | % (port, tag, read_vlan)) |
| 246 | |
| 247 | except PExpectError: |
| 248 | # recurse on error |
| 249 | self._switch_connect() |
| 250 | self.port_set_access_vlan(port, tag) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 251 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 252 | # Add a trunk port to a specified VLAN (tag) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 253 | def port_add_trunk_to_vlan(self, port, tag): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 254 | logging.debug("Adding trunk port %s to VLAN %d", port, tag) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 255 | if not self._is_port_name_valid(port): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 256 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 257 | if not (self.port_get_mode(port) == "trunk"): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 258 | raise InputError("Port %s not in trunk mode" % port) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 259 | |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 260 | try: |
| 261 | self._configure() |
| 262 | self._cli("interface %s" % port) |
| 263 | self._cli("switchport trunk allowed vlan add %d" % tag) |
| 264 | self._end_configure() |
| 265 | |
| 266 | # Validate it happened |
| 267 | read_vlans = self.port_get_trunk_vlan_list(port) |
| 268 | for vlan in read_vlans: |
| 269 | if vlan == tag: |
| 270 | return |
Steve McIntyre | f177128 | 2015-04-29 18:08:34 +0100 | [diff] [blame] | 271 | raise IOError("Failed to add trunk port %s to VLAN %d" % (port, tag)) |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 272 | |
| 273 | except PExpectError: |
| 274 | # recurse on error |
| 275 | self._switch_connect() |
| 276 | self.port_add_trunk_to_vlan(port, tag) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 277 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 278 | # Remove a trunk port from a specified VLAN (tag) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 279 | def port_remove_trunk_from_vlan(self, port, tag): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 280 | logging.debug("Removing trunk port %s from VLAN %d", port, tag) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 281 | if not self._is_port_name_valid(port): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 282 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 283 | if not (self.port_get_mode(port) == "trunk"): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 284 | raise InputError("Port %s not in trunk mode" % port) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 285 | |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 286 | try: |
| 287 | self._configure() |
| 288 | self._cli("interface %s" % port) |
| 289 | self._cli("switchport trunk allowed vlan remove %d" % tag) |
| 290 | self._end_configure() |
| 291 | |
| 292 | # Validate it happened |
| 293 | read_vlans = self.port_get_trunk_vlan_list(port) |
| 294 | for vlan in read_vlans: |
| 295 | if vlan == tag: |
| 296 | raise IOError("Failed to remove trunk port %s from VLAN %d" % (port, tag)) |
| 297 | |
| 298 | except PExpectError: |
| 299 | # recurse on error |
| 300 | self._switch_connect() |
| 301 | self.port_remove_trunk_from_vlan(port, tag) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 302 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 303 | # Get the configured VLAN tag for an access port (tag) |
| 304 | def port_get_access_vlan(self, port): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 305 | logging.debug("Getting VLAN for access port %s", port) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 306 | vlan = 1 |
| 307 | if not self._is_port_name_valid(port): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 308 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 309 | if not (self.port_get_mode(port) == "access"): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 310 | raise InputError("Port %s not in access mode" % port) |
Steve McIntyre | 6b4ff2e | 2015-09-04 14:36:00 +0100 | [diff] [blame] | 311 | regex = re.compile(r'(\d+)\s+\S+\s+Untagged\s+(D|S|G|R)') |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 312 | |
| 313 | try: |
| 314 | self._cli("show interfaces switchport %s" % port) |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 315 | for line in self._read_long_output("show interfaces switchport"): |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 316 | match = regex.match(line) |
| 317 | if match: |
| 318 | vlan = match.group(1) |
| 319 | return int(vlan) |
| 320 | |
| 321 | except PExpectError: |
| 322 | # recurse on error |
| 323 | self._switch_connect() |
| 324 | return self.port_get_access_vlan(port) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 325 | |
| 326 | # Get the list of configured VLAN tags for a trunk port |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 327 | def port_get_trunk_vlan_list(self, port): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 328 | logging.debug("Getting VLANs for trunk port %s", port) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 329 | vlans = [ ] |
| 330 | if not self._is_port_name_valid(port): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 331 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 332 | if not (self.port_get_mode(port) == "trunk"): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 333 | raise InputError("Port %s not in trunk mode" % port) |
Steve McIntyre | 6b4ff2e | 2015-09-04 14:36:00 +0100 | [diff] [blame] | 334 | regex = re.compile(r'(\d+)\s+\S+\s+(Tagged|Untagged)\s+(D|S|G|R)') |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 335 | |
| 336 | try: |
| 337 | self._cli("show interfaces switchport %s" % port) |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 338 | for line in self._read_long_output("show interfaces switchport"): |
Steve McIntyre | c5359e6 | 2015-02-12 04:54:07 +0000 | [diff] [blame] | 339 | match = regex.match(line) |
| 340 | if match: |
| 341 | vlans.append (int(match.group(1))) |
| 342 | return vlans |
| 343 | |
| 344 | except PExpectError: |
| 345 | # recurse on error |
| 346 | self._switch_connect() |
| 347 | return self.port_get_trunk_vlan_list(port) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 348 | |
| 349 | ################################ |
| 350 | ### Internal functions |
| 351 | ################################ |
| 352 | |
Steve McIntyre | b345fc5 | 2015-02-12 06:37:05 +0000 | [diff] [blame] | 353 | # Connect to the switch and log in |
| 354 | def _switch_connect(self): |
| 355 | |
| 356 | if not self.connection is None: |
| 357 | self.connection.close(True) |
| 358 | self.connection = None |
| 359 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 360 | logging.debug("Connecting to Switch with: %s", self.exec_string) |
Steve McIntyre | 06c644a | 2015-07-17 17:07:41 +0100 | [diff] [blame] | 361 | self.connection = pexpect.spawn(self.exec_string, logfile=self.logger) |
Steve McIntyre | b345fc5 | 2015-02-12 06:37:05 +0000 | [diff] [blame] | 362 | self._login() |
| 363 | |
| 364 | # Avoid paged output |
| 365 | self._cli("terminal datadump") |
| 366 | |
| 367 | # And grab details about the switch. in case we need it |
| 368 | self._get_systemdata() |
| 369 | |
| 370 | # And also validate them - make sure we're driving a switch of |
| 371 | # the correct model! Also store the serial number |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 372 | descr_regex = re.compile(r'System Description:.\s+(.*)') |
| 373 | sn_regex = re.compile(r'SN:\s+(\S_)') |
Steve McIntyre | b345fc5 | 2015-02-12 06:37:05 +0000 | [diff] [blame] | 374 | descr = "" |
| 375 | |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 376 | for line in self._systemdata: |
Steve McIntyre | b345fc5 | 2015-02-12 06:37:05 +0000 | [diff] [blame] | 377 | match = descr_regex.match(line) |
| 378 | if match: |
| 379 | descr = match.group(1) |
| 380 | match = sn_regex.match(line) |
| 381 | if match: |
| 382 | self.serial_number = match.group(1) |
| 383 | |
| 384 | if not self._expected_descr_re.match(descr): |
| 385 | raise IOError("Switch %s not recognised by this driver: abort" % descr) |
| 386 | |
| 387 | # Now build a list of our ports, for later sanity checking |
| 388 | self._ports = self._get_port_names() |
| 389 | if len(self._ports) < 4: |
| 390 | raise IOError("Not enough ports detected - problem!") |
| 391 | |
Steve McIntyre | bbd2ac8 | 2015-02-12 03:08:48 +0000 | [diff] [blame] | 392 | def _login(self): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 393 | logging.debug("attempting login with username %s, password %s", self._username, self._password) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 394 | self._cli("") |
| 395 | self.connection.expect("User Name:") |
Steve McIntyre | bbd2ac8 | 2015-02-12 03:08:48 +0000 | [diff] [blame] | 396 | self._cli("%s" % self._username) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 397 | self.connection.expect("Password:") |
Steve McIntyre | bbd2ac8 | 2015-02-12 03:08:48 +0000 | [diff] [blame] | 398 | self._cli("%s" % self._password, False) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 399 | self.connection.expect(r"\*\*") |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 400 | while True: |
Steve McIntyre | 2651aca | 2015-02-12 05:10:58 +0000 | [diff] [blame] | 401 | index = self.connection.expect(['User Name:', 'authentication failed', r'([^#]+)#', 'Password:', '.+']) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 402 | if index == 0 or index == 1: # Failed to log in! |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 403 | logging.error("Login failure: %s\n", self.connection.match) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 404 | raise IOError |
| 405 | elif index == 2: |
Steve McIntyre | 65dfe7f | 2015-06-09 15:57:02 +0100 | [diff] [blame] | 406 | self._prompt_name = re.escape(self.connection.match.group(1).strip()) |
Steve McIntyre | 5c51bc5 | 2015-02-12 05:34:09 +0000 | [diff] [blame] | 407 | # Horrible output from the switch at login time may |
| 408 | # confuse our pexpect stuff here. If we've somehow got |
| 409 | # multiple lines of output, clean up and just take the |
| 410 | # *last* line here. Anything before that is going to |
| 411 | # just be noise from the "***" password input, etc. |
| 412 | prompt_lines = self._prompt_name.split('\r\n') |
| 413 | if len(prompt_lines) > 1: |
| 414 | self._prompt_name = prompt_lines[-1] |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 415 | logging.debug("Got prompt name %s", self._prompt_name) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 416 | return 0 |
Steve McIntyre | ca6c5a3 | 2014-12-23 15:34:29 +0000 | [diff] [blame] | 417 | elif index == 3 or index == 4: |
| 418 | self._cli("", False) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 419 | |
| 420 | def _logout(self): |
| 421 | logging.debug("Logging out") |
| 422 | self._cli("exit", False) |
Steve McIntyre | 26c8956 | 2015-10-09 15:02:37 +0100 | [diff] [blame] | 423 | self.connection.close(True) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 424 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 425 | def _configure(self): |
| 426 | self._cli("configure terminal") |
| 427 | |
| 428 | def _end_configure(self): |
| 429 | self._cli("end") |
| 430 | |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 431 | def _read_long_output(self, text): |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 432 | prompt = self._prompt_name + '#' |
Steve McIntyre | 2eececb | 2015-02-12 03:09:17 +0000 | [diff] [blame] | 433 | try: |
| 434 | self.connection.expect(prompt) |
Steve McIntyre | 1d23b49 | 2015-02-12 03:47:24 +0000 | [diff] [blame] | 435 | except (pexpect.EOF, pexpect.TIMEOUT): |
Steve McIntyre | d0614fb | 2015-02-12 03:44:42 +0000 | [diff] [blame] | 436 | # Something went wrong; logout, log in and try again! |
Steve McIntyre | 7a9c819 | 2015-02-12 07:15:52 +0000 | [diff] [blame] | 437 | logging.error("PEXPECT FAILURE, RECONNECT") |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 438 | self.errors.log_error_in(text) |
Steve McIntyre | 1a3e2f7 | 2015-02-12 03:49:05 +0000 | [diff] [blame] | 439 | raise PExpectError("_read_long_output failed") |
Steve McIntyre | 2eececb | 2015-02-12 03:09:17 +0000 | [diff] [blame] | 440 | except: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 441 | logging.error("prompt is \"%s\"", prompt) |
Steve McIntyre | 2eececb | 2015-02-12 03:09:17 +0000 | [diff] [blame] | 442 | raise |
Steve McIntyre | df6aabf | 2015-02-12 06:33:19 +0000 | [diff] [blame] | 443 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 444 | longbuf = [] |
Steve McIntyre | a7cdefc | 2014-12-24 00:48:19 +0000 | [diff] [blame] | 445 | for line in self.connection.before.split('\r\n'): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 446 | longbuf.append(line.strip()) |
| 447 | return longbuf |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 448 | |
| 449 | def _get_port_names(self): |
| 450 | logging.debug("Grabbing list of ports") |
| 451 | interfaces = [] |
| 452 | |
| 453 | # Use "Up" or "Down" to only identify lines in the output that |
| 454 | # match interfaces that exist |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 455 | regex = re.compile(r'^(\w+).*(Up|Down)') |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 456 | |
Steve McIntyre | 48ab82c | 2015-02-12 04:59:10 +0000 | [diff] [blame] | 457 | try: |
| 458 | self._cli("show interfaces status detailed") |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 459 | for line in self._read_long_output("show interfaces status detailed"): |
Steve McIntyre | 48ab82c | 2015-02-12 04:59:10 +0000 | [diff] [blame] | 460 | match = regex.match(line) |
| 461 | if match: |
Steve McIntyre | d5cfde1 | 2015-08-05 13:49:42 +0100 | [diff] [blame] | 462 | interface = match.group(1) |
| 463 | interfaces.append(interface) |
| 464 | self._port_numbers[interface] = len(interfaces) |
Steve McIntyre | d601ab8 | 2015-07-09 17:42:36 +0100 | [diff] [blame] | 465 | logging.debug(" found %d ports on the switch", len(interfaces)) |
Steve McIntyre | 48ab82c | 2015-02-12 04:59:10 +0000 | [diff] [blame] | 466 | return interfaces |
| 467 | except PExpectError: |
| 468 | # recurse on error |
| 469 | self._switch_connect() |
| 470 | return self._get_port_names() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 471 | |
Steve McIntyre | 1a7bad5 | 2015-07-20 15:21:42 +0100 | [diff] [blame] | 472 | # Get the mode of a port: access or trunk |
| 473 | def _port_get_mode(self, port): |
| 474 | logging.debug("Getting mode of port %s", port) |
| 475 | mode = '' |
| 476 | if not self._is_port_name_valid(port): |
| 477 | raise InputError("Port name %s not recognised" % port) |
| 478 | regex = re.compile(r'Port Mode: (\S+)') |
| 479 | |
| 480 | try: |
| 481 | self._cli("show interfaces switchport %s" % port) |
| 482 | for line in self._read_long_output("show interfaces switchport"): |
| 483 | match = regex.match(line) |
| 484 | if match: |
| 485 | mode = match.group(1) |
| 486 | return mode.lower() |
| 487 | |
| 488 | except PExpectError: |
| 489 | # recurse on error |
| 490 | self._switch_connect() |
| 491 | return self.port_get_mode(port) |
| 492 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 493 | def _show_config(self): |
| 494 | logging.debug("Grabbing config") |
Steve McIntyre | 0fbb2dc | 2015-02-12 05:11:27 +0000 | [diff] [blame] | 495 | try: |
| 496 | self._cli("show running-config") |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 497 | return self._read_long_output("show running-config") |
Steve McIntyre | 0fbb2dc | 2015-02-12 05:11:27 +0000 | [diff] [blame] | 498 | except PExpectError: |
| 499 | # recurse on error |
| 500 | self._switch_connect() |
| 501 | return self._show_config() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 502 | |
| 503 | def _show_clock(self): |
| 504 | logging.debug("Grabbing time") |
Steve McIntyre | 0fbb2dc | 2015-02-12 05:11:27 +0000 | [diff] [blame] | 505 | try: |
| 506 | self._cli("show clock") |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 507 | return self._read_long_output("show clock") |
Steve McIntyre | 0fbb2dc | 2015-02-12 05:11:27 +0000 | [diff] [blame] | 508 | except PExpectError: |
| 509 | # recurse on error |
| 510 | self._switch_connect() |
| 511 | return self._show_clock() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 512 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 513 | def _get_systemdata(self): |
Steve McIntyre | df6aabf | 2015-02-12 06:33:19 +0000 | [diff] [blame] | 514 | logging.debug("Grabbing system data") |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 515 | |
Steve McIntyre | 0fbb2dc | 2015-02-12 05:11:27 +0000 | [diff] [blame] | 516 | try: |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 517 | self._systemdata = [] |
Steve McIntyre | 0fbb2dc | 2015-02-12 05:11:27 +0000 | [diff] [blame] | 518 | self._cli("show system") |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 519 | for line in self._read_long_output("show system"): |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 520 | self._systemdata.append(line) |
Steve McIntyre | 28d34ca | 2014-08-12 15:40:24 +0100 | [diff] [blame] | 521 | |
Steve McIntyre | 0fbb2dc | 2015-02-12 05:11:27 +0000 | [diff] [blame] | 522 | logging.debug("Grabbing system sw and hw versions") |
| 523 | self._cli("show version") |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 524 | for line in self._read_long_output("show version"): |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 525 | self._systemdata.append(line) |
Steve McIntyre | 0fbb2dc | 2015-02-12 05:11:27 +0000 | [diff] [blame] | 526 | |
| 527 | except PExpectError: |
| 528 | # recurse on error |
| 529 | self._switch_connect() |
| 530 | return self._get_systemdata() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 531 | |
Steve McIntyre | 16f0216 | 2014-08-14 17:47:36 +0100 | [diff] [blame] | 532 | ###################################### |
| 533 | # Internal port access helper methods |
| 534 | ###################################### |
| 535 | # N.B. No parameter checking here, for speed reasons - if you're |
| 536 | # calling this internal API then you should already have validated |
| 537 | # things yourself! Equally, no post-set checks in here - do that |
| 538 | # at the higher level. |
| 539 | ###################################### |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 540 | |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 541 | # Wrapper around connection.send - by default, expect() the same |
| 542 | # text we've sent, to remove it from the output from the |
| 543 | # switch. For the few cases where we don't need that, override |
| 544 | # this using echo=False. |
| 545 | # Horrible, but seems to work. |
| 546 | def _cli(self, text, echo=True): |
| 547 | self.connection.send(text + '\r') |
| 548 | if echo: |
Steve McIntyre | 9f7f03b | 2015-02-12 03:10:13 +0000 | [diff] [blame] | 549 | try: |
| 550 | self.connection.expect(text) |
Steve McIntyre | 1d23b49 | 2015-02-12 03:47:24 +0000 | [diff] [blame] | 551 | except (pexpect.EOF, pexpect.TIMEOUT): |
Steve McIntyre | 9f7f03b | 2015-02-12 03:10:13 +0000 | [diff] [blame] | 552 | # Something went wrong; logout, log in and try again! |
Steve McIntyre | 7a9c819 | 2015-02-12 07:15:52 +0000 | [diff] [blame] | 553 | logging.error("PEXPECT FAILURE, RECONNECT") |
Steve McIntyre | 3c3f4fc | 2015-02-12 04:26:27 +0000 | [diff] [blame] | 554 | self.errors.log_error_out(text) |
Steve McIntyre | 729d950 | 2015-02-12 03:14:10 +0000 | [diff] [blame] | 555 | raise PExpectError("_cli failed on %s" % text) |
Steve McIntyre | 9f7f03b | 2015-02-12 03:10:13 +0000 | [diff] [blame] | 556 | except: |
Steve McIntyre | a67473a | 2015-02-12 08:26:33 +0000 | [diff] [blame] | 557 | logging.error("Unexpected error: %s", sys.exc_info()[0]) |
Steve McIntyre | 9f7f03b | 2015-02-12 03:10:13 +0000 | [diff] [blame] | 558 | raise |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 559 | |
| 560 | if __name__ == "__main__": |
Steve McIntyre | f169932 | 2014-08-19 22:49:43 +0100 | [diff] [blame] | 561 | # p = CiscoSX300('10.172.2.52', 23) |
Steve McIntyre | 48dc6ae | 2014-12-23 16:08:19 +0000 | [diff] [blame] | 562 | |
Steve McIntyre | da9e9c1 | 2018-01-24 16:58:13 +0000 | [diff] [blame] | 563 | # Simple test harness - exercise the main working functions above to verify |
| 564 | # they work. This does *NOT* test really disruptive things like "save |
| 565 | # running-config" and "reload" - test those by hand. |
| 566 | |
Steve McIntyre | 48dc6ae | 2014-12-23 16:08:19 +0000 | [diff] [blame] | 567 | import optparse |
| 568 | |
| 569 | switch = 'vlandswitch02' |
| 570 | parser = optparse.OptionParser() |
| 571 | parser.add_option("--switch", |
| 572 | dest = "switch", |
| 573 | action = "store", |
| 574 | nargs = 1, |
| 575 | type = "string", |
| 576 | help = "specify switch to connect to for testing", |
| 577 | metavar = "<switch>") |
| 578 | (opts, args) = parser.parse_args() |
| 579 | if opts.switch: |
| 580 | switch = opts.switch |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 581 | |
Steve McIntyre | 3d92ef7 | 2015-07-08 20:02:18 +0100 | [diff] [blame] | 582 | portname_base = "fa" |
| 583 | # Text to match if we're on a SG-series switch, ports all called gi<number> |
| 584 | sys_descr_re = re.compile('System Description.*Gigabit') |
Steve McIntyre | 48dc6ae | 2014-12-23 16:08:19 +0000 | [diff] [blame] | 585 | |
Steve McIntyre | 3d92ef7 | 2015-07-08 20:02:18 +0100 | [diff] [blame] | 586 | def _port_name(number): |
| 587 | return "%s%d" % (portname_base, number) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 588 | |
Steve McIntyre | 144d51c | 2015-07-07 17:56:30 +0100 | [diff] [blame] | 589 | logging.basicConfig(level = logging.DEBUG, |
| 590 | format = '%(asctime)s %(levelname)-8s %(message)s') |
Steve McIntyre | 48dc6ae | 2014-12-23 16:08:19 +0000 | [diff] [blame] | 591 | p = CiscoSX300(switch, 23, debug = True) |
Steve McIntyre | aea995c | 2014-12-22 17:17:38 +0000 | [diff] [blame] | 592 | p.switch_connect('cisco', 'cisco', None) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 593 | #buf = p._show_clock() |
| 594 | #print "%s" % buf |
| 595 | #buf = p._show_config() |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 596 | #p.dump_list(buf) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 597 | |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 598 | print "System data:" |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 599 | p.dump_list(p._systemdata) |
| 600 | for l in p._systemdata: |
| 601 | m = sys_descr_re.match(l) |
| 602 | if m: |
Steve McIntyre | 3d92ef7 | 2015-07-08 20:02:18 +0100 | [diff] [blame] | 603 | print 'Found an SG switch, using "gi" as port name prefix for testing' |
| 604 | portname_base = "gi" |
| 605 | |
| 606 | if portname_base == "fa": |
| 607 | print 'Found an SF switch, using "fa" as port name prefix for testing' |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 608 | |
| 609 | print "Creating VLANs for testing:" |
| 610 | for i in [ 2, 3, 4, 5, 20 ]: |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 611 | p.vlan_create(i) |
| 612 | p.vlan_set_name(i, "test%d" % i) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 613 | print " %d (test%d)" % (i, i) |
| 614 | |
| 615 | #print "And dump config\n" |
| 616 | #buf = p._show_config() |
| 617 | #print "%s" % buf |
| 618 | |
| 619 | #print "Destroying VLAN 2\n" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 620 | #p.vlan_destroy(2) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 621 | |
| 622 | #print "And dump config\n" |
| 623 | #buf = p._show_config() |
| 624 | #print "%s" % buf |
| 625 | |
| 626 | #print "Port names are:" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 627 | #buf = p.switch_get_port_names() |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 628 | #p.dump_list(buf) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 629 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 630 | #buf = p.vlan_get_name(25) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 631 | #print "VLAN with tag 25 is called \"%s\"" % buf |
| 632 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 633 | #p.vlan_set_name(35, "foo") |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 634 | #print "VLAN with tag 35 is called \"foo\"" |
| 635 | |
Steve McIntyre | 3d92ef7 | 2015-07-08 20:02:18 +0100 | [diff] [blame] | 636 | #buf = p.port_get_mode(_port_name(12)) |
| 637 | #print "Port %s is in %s mode" % (_port_name(12), buf) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 638 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 639 | # Test access stuff |
Steve McIntyre | 3d92ef7 | 2015-07-08 20:02:18 +0100 | [diff] [blame] | 640 | print "Set %s to access mode" % _port_name(6) |
| 641 | p.port_set_mode(_port_name(6), "access") |
| 642 | print "Move %s to VLAN 2" % _port_name(6) |
| 643 | p.port_set_access_vlan(_port_name(6), 2) |
| 644 | buf = p.port_get_access_vlan(_port_name(6)) |
| 645 | print "Read from switch: %s is on VLAN %s" % (_port_name(6), buf) |
| 646 | print "Move %s back to default VLAN 1" % _port_name(6) |
| 647 | p.port_set_access_vlan(_port_name(6), 1) |
| 648 | #print "And move %s back to a trunk port" % _port_name(6) |
| 649 | #p.port_set_mode(_port_name(6), "trunk") |
| 650 | #buf = p.port_get_mode(_port_name(6)) |
| 651 | #print "Port %s is in %s mode" % (_port_name(6), buf) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 652 | |
| 653 | # Test trunk stuff |
Steve McIntyre | 3d92ef7 | 2015-07-08 20:02:18 +0100 | [diff] [blame] | 654 | print "Set %s to trunk mode" % _port_name(2) |
| 655 | p.port_set_mode(_port_name(2), "trunk") |
| 656 | print "Add %s to VLAN 2" % _port_name(2) |
| 657 | p.port_add_trunk_to_vlan(_port_name(2), 2) |
| 658 | print "Add %s to VLAN 3" % _port_name(2) |
| 659 | p.port_add_trunk_to_vlan(_port_name(2), 3) |
| 660 | print "Add %s to VLAN 4" % _port_name(2) |
| 661 | p.port_add_trunk_to_vlan(_port_name(2), 4) |
| 662 | print "Read from switch: which VLANs is %s on?" % _port_name(2) |
| 663 | buf = p.port_get_trunk_vlan_list(_port_name(2)) |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 664 | p.dump_list(buf) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 665 | |
Steve McIntyre | 3d92ef7 | 2015-07-08 20:02:18 +0100 | [diff] [blame] | 666 | print "Remove %s from VLANs 3,3,4" % _port_name(2) |
| 667 | p.port_remove_trunk_from_vlan(_port_name(2), 3) |
| 668 | p.port_remove_trunk_from_vlan(_port_name(2), 3) |
| 669 | p.port_remove_trunk_from_vlan(_port_name(2), 4) |
| 670 | print "Read from switch: which VLANs is %s on?" % _port_name(2) |
| 671 | buf = p.port_get_trunk_vlan_list(_port_name(2)) |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 672 | p.dump_list(buf) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 673 | |
| 674 | # print "Adding lots of ports to VLANs" |
Steve McIntyre | 3d92ef7 | 2015-07-08 20:02:18 +0100 | [diff] [blame] | 675 | # p.port_add_trunk_to_vlan(_port_name(1), 2) |
| 676 | # p.port_add_trunk_to_vlan(_port_name(3), 2) |
| 677 | # p.port_add_trunk_to_vlan(_port_name(5), 2) |
| 678 | # p.port_add_trunk_to_vlan(_port_name(7), 2) |
| 679 | # p.port_add_trunk_to_vlan(_port_name(9), 2) |
| 680 | # p.port_add_trunk_to_vlan(_port_name(11), 2) |
| 681 | # p.port_add_trunk_to_vlan(_port_name(13), 2) |
| 682 | # p.port_add_trunk_to_vlan(_port_name(15), 2) |
| 683 | # p.port_add_trunk_to_vlan(_port_name(17), 2) |
| 684 | # p.port_add_trunk_to_vlan(_port_name(19), 2) |
| 685 | # p.port_add_trunk_to_vlan(_port_name(21), 2) |
| 686 | # p.port_add_trunk_to_vlan(_port_name(23), 2) |
| 687 | # p.port_add_trunk_to_vlan(_port_name(4, 2) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 688 | |
| 689 | print "VLANs are:" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 690 | buf = p.vlan_get_list() |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 691 | p.dump_list(buf) |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 692 | |
Steve McIntyre | 7460d97 | 2014-12-23 14:45:30 +0000 | [diff] [blame] | 693 | # print 'Restarting switch, to explicitly reset config' |
| 694 | # p.switch_restart() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 695 | |
Steve McIntyre | bf5dc88 | 2014-12-19 17:54:58 +0000 | [diff] [blame] | 696 | # p.switch_save_running_config() |
Steve McIntyre | cc29711 | 2014-08-11 18:46:58 +0100 | [diff] [blame] | 697 | # p._show_config() |