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