Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +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 |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 23 | import re |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 24 | from drivers.common import SwitchDriver, SwitchErrors |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 25 | |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 26 | if __name__ == '__main__': |
Steve McIntyre | a67473a | 2015-02-12 08:26:33 +0000 | [diff] [blame] | 27 | import os |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 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 | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 32 | from errors import InputError, PExpectError |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 33 | |
| 34 | class CiscoCatalyst(SwitchDriver): |
| 35 | |
| 36 | connection = None |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 37 | _username = None |
| 38 | _password = None |
| 39 | _enable_password = None |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 40 | |
| 41 | _capabilities = [ |
| 42 | 'TrunkWildCardVlans' # Trunk ports are on all VLANs by |
| 43 | # default, so we shouldn't need to |
| 44 | # bugger with them |
| 45 | ] |
| 46 | |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 47 | # Regexp of expected hardware information - fail if we don't see |
| 48 | # this |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 49 | _expected_descr_re = re.compile('WS-C\S+-\d+P') |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 50 | |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 51 | logfile = None |
| 52 | |
Steve McIntyre | 48dc6ae | 2014-12-23 16:08:19 +0000 | [diff] [blame] | 53 | def __init__(self, switch_hostname, switch_telnetport=23, debug = False): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 54 | SwitchDriver.__init__(self) |
| 55 | self._systemdata = [] |
Steve McIntyre | 48dc6ae | 2014-12-23 16:08:19 +0000 | [diff] [blame] | 56 | if debug: |
Steve McIntyre | 9f5a023 | 2014-12-23 16:14:28 +0000 | [diff] [blame] | 57 | self.logfile = sys.stderr |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 58 | self.exec_string = "/usr/bin/telnet %s %d" % (switch_hostname, switch_telnetport) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 59 | self.errors = SwitchErrors() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 60 | |
| 61 | ################################ |
| 62 | ### Switch-level API functions |
| 63 | ################################ |
| 64 | |
| 65 | # Connect to the switch and log in |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 66 | def switch_connect(self, username, password, enablepassword): |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 67 | self._username = username |
| 68 | self._password = password |
| 69 | self._enable_password = enablepassword |
| 70 | self._switch_connect() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 71 | |
| 72 | # Log out of the switch and drop the connection and all state |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 73 | def switch_disconnect(self): |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 74 | self._logout() |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 75 | logging.debug("Closing connection: %s", self.connection) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 76 | self.connection.close(True) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 77 | self._ports = [] |
| 78 | self._prompt_name = '' |
| 79 | self._systemdata = [] |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 80 | del(self) |
| 81 | |
| 82 | # Save the current running config into flash - we want config to |
| 83 | # remain across reboots |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 84 | def switch_save_running_config(self): |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 85 | try: |
| 86 | self._cli("copy running-config startup-config") |
| 87 | self.connection.expect("startup-config") |
| 88 | self._cli("startup-config") |
| 89 | self.connection.expect("OK") |
Steve McIntyre | 2d84e52 | 2015-02-12 06:44:42 +0000 | [diff] [blame] | 90 | except (PExpectError, pexpect.EOF): |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 91 | # recurse on error |
| 92 | self._switch_connect() |
| 93 | self.switch_save_running_config() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 94 | |
Steve McIntyre | 095b445 | 2014-12-19 17:53:43 +0000 | [diff] [blame] | 95 | # Restart the switch - we need to reload config to do a |
| 96 | # roll-back. Do NOT save running-config first if the switch asks - |
| 97 | # we're trying to dump recent changes, not save them. |
| 98 | # |
| 99 | # This will also implicitly cause a connection to be closed |
| 100 | def switch_restart(self): |
| 101 | self._cli("reload") |
| 102 | index = self.connection.expect(['has been modified', 'Proceed']) |
| 103 | if index == 0: |
| 104 | self._cli("n") # No, don't save |
| 105 | self.connection.expect("Proceed") |
| 106 | |
| 107 | # Fall through |
| 108 | self._cli("y") # Yes, continue to reset |
| 109 | self.connection.close(True) |
| 110 | |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 111 | # List the capabilities of the switch (and driver) - some things |
| 112 | # make no sense to abstract. Returns a dict of strings, each one |
| 113 | # describing an extra feature that that higher levels may care |
| 114 | # about |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 115 | def switch_get_capabilities(self): |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 116 | return self._capabilities |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 117 | |
| 118 | ################################ |
| 119 | ### VLAN API functions |
| 120 | ################################ |
| 121 | |
| 122 | # Create a VLAN with the specified tag |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 123 | def vlan_create(self, tag): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 124 | logging.debug("Creating VLAN %d", tag) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 125 | try: |
| 126 | self._configure() |
| 127 | self._cli("vlan %d" % tag) |
| 128 | self._end_configure() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 129 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 130 | # Validate it happened |
| 131 | vlans = self.vlan_get_list() |
| 132 | for vlan in vlans: |
| 133 | if vlan == tag: |
| 134 | return |
| 135 | raise IOError("Failed to create VLAN %d" % tag) |
| 136 | |
| 137 | except PExpectError: |
| 138 | # recurse on error |
| 139 | self._switch_connect() |
| 140 | self.vlan_create(tag) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 141 | |
| 142 | # Destroy a VLAN with the specified tag |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 143 | def vlan_destroy(self, tag): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 144 | logging.debug("Destroying VLAN %d", tag) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 145 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 146 | try: |
| 147 | self._configure() |
| 148 | self._cli("no vlan %d" % tag) |
| 149 | self._end_configure() |
| 150 | |
| 151 | # Validate it happened |
| 152 | vlans = self.vlan_get_list() |
| 153 | for vlan in vlans: |
| 154 | if vlan == tag: |
| 155 | raise IOError("Failed to destroy VLAN %d" % tag) |
| 156 | |
| 157 | except PExpectError: |
| 158 | # recurse on error |
| 159 | self._switch_connect() |
| 160 | self.vlan_destroy(tag) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 161 | |
| 162 | # Set the name of a VLAN |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 163 | def vlan_set_name(self, tag, name): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 164 | logging.debug("Setting name of VLAN %d to %s", tag, name) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 165 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 166 | try: |
| 167 | self._configure() |
| 168 | self._cli("vlan %d" % tag) |
| 169 | self._cli("name %s" % name) |
| 170 | self._end_configure() |
| 171 | |
| 172 | # Validate it happened |
| 173 | read_name = self.vlan_get_name(tag) |
| 174 | if read_name != name: |
| 175 | raise IOError("Failed to set name for VLAN %d (name found is \"%s\", not \"%s\")" |
| 176 | % (tag, read_name, name)) |
| 177 | except PExpectError: |
| 178 | # recurse on error |
| 179 | self._switch_connect() |
| 180 | self.vlan_set_name(tag, name) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 181 | |
| 182 | # Get a list of the VLAN tags currently registered on the switch |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 183 | def vlan_get_list(self): |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 184 | logging.debug("Grabbing list of VLANs") |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 185 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 186 | try: |
| 187 | vlans = [] |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 188 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 189 | regex = re.compile('^ *(\d+).*(active)') |
| 190 | |
| 191 | self._cli("show vlan brief") |
| 192 | for line in self._read_long_output(): |
| 193 | match = regex.match(line) |
| 194 | if match: |
| 195 | vlans.append(int(match.group(1))) |
| 196 | return vlans |
| 197 | |
| 198 | except PExpectError: |
| 199 | # recurse on error |
| 200 | self._switch_connect() |
| 201 | return self.vlan_get_list() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 202 | |
| 203 | # 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] | 204 | def vlan_get_name(self, tag): |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 205 | |
| 206 | try: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 207 | logging.debug("Grabbing the name of VLAN %d", tag) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 208 | name = None |
| 209 | regex = re.compile('^ *\d+\s+(\S+).*(active)') |
| 210 | self._cli("show vlan id %d" % tag) |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 211 | for line in self._read_long_output("show vlan id"): |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 212 | match = regex.match(line) |
| 213 | if match: |
| 214 | name = match.group(1) |
| 215 | name.strip() |
| 216 | return name |
| 217 | |
| 218 | except PExpectError: |
| 219 | # recurse on error |
| 220 | self._switch_connect() |
| 221 | return self.vlan_get_name(tag) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 222 | |
| 223 | ################################ |
| 224 | ### Port API functions |
Steve McIntyre | e1bf11a | 2014-08-14 17:56:25 +0100 | [diff] [blame] | 225 | ################################ |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 226 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 227 | # Set the mode of a port: access or trunk |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 228 | def port_set_mode(self, port, mode): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 229 | logging.debug("Setting port %s to %s", port, mode) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 230 | if not self._is_port_mode_valid(mode): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 231 | raise InputError("Port mode %s is not allowed" % mode) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 232 | if not self._is_port_name_valid(port): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 233 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 234 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 235 | try: |
| 236 | self._configure() |
| 237 | self._cli("interface %s" % port) |
| 238 | self._cli("switchport mode %s" % mode) |
| 239 | if mode == "trunk": |
| 240 | self._cli("switchport trunk encapsulation dot1q") |
| 241 | self._end_configure() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 242 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 243 | # Validate it happened |
| 244 | read_mode = self.port_get_mode(port) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 245 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 246 | if read_mode != mode: |
| 247 | raise IOError("Failed to set mode for port %s" % port) |
| 248 | |
| 249 | except PExpectError: |
| 250 | # recurse on error |
| 251 | self._switch_connect() |
| 252 | self.port_set_mode(port, mode) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 253 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 254 | # Get the mode of a port: access or trunk |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 255 | def port_get_mode(self, port): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 256 | logging.debug("Getting mode of port %s", port) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 257 | mode = '' |
| 258 | if not self._is_port_name_valid(port): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 259 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 260 | regex = re.compile('Administrative Mode: (.*)') |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 261 | |
| 262 | try: |
| 263 | self._cli("show interfaces %s switchport" % port) |
| 264 | for line in self._read_long_output(): |
| 265 | match = regex.match(line) |
| 266 | if match: |
| 267 | mode = match.group(1) |
| 268 | if mode == 'static access': |
| 269 | return 'access' |
| 270 | if mode == 'dynamic auto': |
| 271 | return 'trunk' |
| 272 | return mode |
| 273 | |
| 274 | except PExpectError: |
| 275 | # recurse on error |
| 276 | self._switch_connect() |
| 277 | return self.port_get_mode(port) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 278 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 279 | # Set an access port to be in a specified VLAN (tag) |
| 280 | def port_set_access_vlan(self, port, tag): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 281 | logging.debug("Setting access port %s to VLAN %d", port, tag) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 282 | if not self._is_port_name_valid(port): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 283 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 284 | if not (self.port_get_mode(port) == "access"): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 285 | raise InputError("Port %s not in access mode" % port) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 286 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 287 | try: |
| 288 | self._configure() |
| 289 | self._cli("interface %s" % port) |
| 290 | self._cli("switchport access vlan %d" % tag) |
| 291 | self._cli("no shutdown") |
| 292 | self._end_configure() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 293 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 294 | # Finally, validate things worked |
| 295 | read_vlan = int(self.port_get_access_vlan(port)) |
| 296 | if read_vlan != tag: |
| 297 | raise IOError("Failed to move access port %d to VLAN %d - got VLAN %d instead" |
| 298 | % (port, tag, read_vlan)) |
| 299 | |
| 300 | except PExpectError: |
| 301 | # recurse on error |
| 302 | self._switch_connect() |
| 303 | self.port_set_access_vlan(port, tag) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 304 | |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 305 | # Add a trunk port to a specified VLAN (tag) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 306 | def port_add_trunk_to_vlan(self, port, tag): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 307 | logging.debug("Adding trunk port %s to VLAN %d", port, tag) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 308 | if not self._is_port_name_valid(port): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 309 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 310 | if not (self.port_get_mode(port) == "trunk"): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 311 | raise InputError("Port %s not in trunk mode" % port) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 312 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 313 | try: |
| 314 | self._configure() |
| 315 | self._cli("interface %s" % port) |
| 316 | self._cli("switchport trunk allowed vlan add %d" % tag) |
| 317 | self._end_configure() |
| 318 | |
| 319 | # Validate it happened |
| 320 | read_vlans = self.port_get_trunk_vlan_list(port) |
| 321 | for vlan in read_vlans: |
| 322 | if vlan == tag or vlan == "ALL": |
| 323 | return |
| 324 | raise IOError("Failed to add trunk port %s to VLAN %d" % (port, tag)) |
| 325 | |
| 326 | except PExpectError: |
| 327 | # recurse on error |
| 328 | self._switch_connect() |
| 329 | self.port_add_trunk_to_vlan(port, tag) |
| 330 | |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 331 | # Remove a trunk port from a specified VLAN (tag) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 332 | def port_remove_trunk_from_vlan(self, port, tag): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 333 | logging.debug("Removing trunk port %s from VLAN %d", port, tag) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 334 | if not self._is_port_name_valid(port): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 335 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 336 | if not (self.port_get_mode(port) == "trunk"): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 337 | raise InputError("Port %s not in trunk mode" % port) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 338 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 339 | try: |
| 340 | self._configure() |
| 341 | self._cli("interface %s" % port) |
| 342 | self._cli("switchport trunk allowed vlan remove %d" % tag) |
| 343 | self._end_configure() |
| 344 | |
| 345 | # Validate it happened |
| 346 | read_vlans = self.port_get_trunk_vlan_list(port) |
| 347 | for vlan in read_vlans: |
| 348 | if vlan == tag: |
| 349 | raise IOError("Failed to remove trunk port %s from VLAN %d" % (port, tag)) |
| 350 | |
| 351 | except PExpectError: |
| 352 | # recurse on error |
| 353 | self._switch_connect() |
| 354 | self.port_remove_trunk_from_vlan(port, tag) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 355 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 356 | # Get the configured VLAN tag for an access port (tag) |
| 357 | def port_get_access_vlan(self, port): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 358 | logging.debug("Getting VLAN for access port %s", port) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 359 | vlan = 1 |
| 360 | if not self._is_port_name_valid(port): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 361 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 362 | if not (self.port_get_mode(port) == "access"): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 363 | raise InputError("Port %s not in access mode" % port) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 364 | regex = re.compile('Access Mode VLAN: (\d+)') |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 365 | |
| 366 | try: |
| 367 | self._cli("show interfaces %s switchport" % port) |
| 368 | for line in self._read_long_output(): |
| 369 | match = regex.match(line) |
| 370 | if match: |
| 371 | vlan = match.group(1) |
| 372 | return int(vlan) |
| 373 | |
| 374 | except PExpectError: |
| 375 | # recurse on error |
| 376 | self._switch_connect() |
| 377 | return self.port_get_access_vlan(port) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 378 | |
| 379 | # Get the list of configured VLAN tags for a trunk port |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 380 | def port_get_trunk_vlan_list(self, port): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 381 | logging.debug("Getting VLANs for trunk port %s", port) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 382 | vlans = [ ] |
| 383 | if not self._is_port_name_valid(port): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 384 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 385 | if not (self.port_get_mode(port) == "trunk"): |
Steve McIntyre | 72a8bce | 2015-01-23 18:02:19 +0000 | [diff] [blame] | 386 | raise InputError("Port %s not in trunk mode" % port) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 387 | regex_start = re.compile('Trunking VLANs Enabled: (.*)') |
| 388 | regex_continue = re.compile('\s*(\d.*)') |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 389 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 390 | try: |
| 391 | self._cli("show interfaces %s switchport" % port) |
| 392 | |
| 393 | # Horrible parsing work - VLAN list may extend over several lines |
| 394 | in_match = False |
| 395 | vlan_text = '' |
| 396 | |
| 397 | for line in self._read_long_output(): |
| 398 | if in_match: |
| 399 | match = regex_continue.match(line) |
| 400 | if match: |
| 401 | vlan_text += match.group(1) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 402 | else: |
| 403 | in_match = False |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 404 | else: |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 405 | match = regex_start.match(line) |
| 406 | if match: |
| 407 | vlan_text += match.group(1) |
| 408 | in_match = True |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 409 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 410 | vlans = self._parse_vlan_list(vlan_text) |
| 411 | return vlans |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 412 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 413 | except PExpectError: |
| 414 | # recurse on error |
| 415 | self._switch_connect() |
| 416 | return self.port_get_trunk_vlan_list(port) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 417 | |
| 418 | ################################ |
| 419 | ### Internal functions |
| 420 | ################################ |
| 421 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 422 | # Connect to the switch and log in |
| 423 | def _switch_connect(self): |
| 424 | |
| 425 | if not self.connection is None: |
| 426 | self.connection.close(True) |
| 427 | self.connection = None |
| 428 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 429 | logging.debug("Connecting to Switch with: %s", self.exec_string) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 430 | self.connection = pexpect.spawn(self.exec_string, logfile = self.logfile) |
| 431 | |
| 432 | self._login() |
| 433 | |
| 434 | # Avoid paged output |
| 435 | self._cli("terminal length 0") |
| 436 | |
| 437 | # And grab details about the switch. in case we need it |
| 438 | self._get_systemdata() |
| 439 | |
| 440 | # And also validate them - make sure we're driving a switch of |
| 441 | # the correct model! Also store the serial number |
| 442 | descr_regex = re.compile('^cisco\s+(\S+)') |
| 443 | sn_regex = re.compile('System serial number\s+:\s+(\S+)') |
| 444 | descr = "" |
| 445 | |
| 446 | for line in self._systemdata: |
| 447 | match = descr_regex.match(line) |
| 448 | if match: |
| 449 | descr = match.group(1) |
| 450 | match = sn_regex.match(line) |
| 451 | if match: |
| 452 | self.serial_number = match.group(1) |
| 453 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 454 | logging.debug("serial number is %s", self.serial_number) |
| 455 | logging.debug("system description is %s", descr) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 456 | |
| 457 | if not self._expected_descr_re.match(descr): |
| 458 | raise IOError("Switch %s not recognised by this driver: abort" % descr) |
| 459 | |
| 460 | # Now build a list of our ports, for later sanity checking |
| 461 | self._ports = self._get_port_names() |
| 462 | if len(self._ports) < 4: |
| 463 | raise IOError("Not enough ports detected - problem!") |
| 464 | |
| 465 | def _login(self): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 466 | logging.debug("attempting login with username %s, password %s", self._username, self._password) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 467 | self.connection.expect('User Access Verification') |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 468 | if self._username is not None: |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 469 | self.connection.expect("User Name:") |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 470 | self._cli("%s" % self._username) |
| 471 | if self._password is not None: |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 472 | self.connection.expect("Password:") |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 473 | self._cli("%s" % self._password, False) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 474 | while True: |
| 475 | index = self.connection.expect(['User Name:', 'Password:', 'Bad passwords', 'authentication failed', r'(.*)(#|>)']) |
| 476 | if index != 4: # Any other means: failed to log in! |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 477 | logging.error("Login failure: index %d\n", index) |
| 478 | logging.error("Login failure: %s\n", self.connection.match.before) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 479 | raise IOError |
| 480 | |
| 481 | # else |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 482 | self._prompt_name = self.connection.match.group(1).strip() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 483 | if self.connection.match.group(2) == ">": |
| 484 | # Need to enter "enable" mode too |
| 485 | self._cli("enable") |
Steve McIntyre | ad12cd7 | 2015-02-12 06:41:29 +0000 | [diff] [blame] | 486 | if self._enable_password is not None: |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 487 | self.connection.expect("Password:") |
Steve McIntyre | ad12cd7 | 2015-02-12 06:41:29 +0000 | [diff] [blame] | 488 | self._cli("%s" % self._enable_password, False) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 489 | index = self.connection.expect(['Password:', 'Bad passwords', 'authentication failed', r'(.*)(#|>)']) |
| 490 | if index != 3: # Any other means: failed to log in! |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 491 | logging.error("Enable password failure: %s\n", self.connection.match) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 492 | raise IOError |
| 493 | return 0 |
| 494 | |
| 495 | def _logout(self): |
| 496 | logging.debug("Logging out") |
| 497 | self._cli("exit", False) |
| 498 | |
| 499 | def _configure(self): |
| 500 | self._cli("configure terminal") |
| 501 | |
| 502 | def _end_configure(self): |
| 503 | self._cli("end") |
| 504 | |
Steve McIntyre | c68d6d7 | 2014-12-24 00:23:36 +0000 | [diff] [blame] | 505 | def _read_long_output(self): |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 506 | prompt = self._prompt_name + '#' |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 507 | try: |
| 508 | self.connection.expect(prompt) |
| 509 | except (pexpect.EOF, pexpect.TIMEOUT): |
| 510 | # Something went wrong; logout, log in and try again! |
Steve McIntyre | 7a9c819 | 2015-02-12 07:15:52 +0000 | [diff] [blame] | 511 | logging.error("PEXPECT FAILURE, RECONNECT") |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 512 | self.errors.log_error_in("") |
| 513 | raise PExpectError("_read_long_output failed") |
| 514 | except: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 515 | logging.error("prompt is \"%s\"", prompt) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 516 | raise |
| 517 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 518 | longbuf = [] |
Steve McIntyre | a7cdefc | 2014-12-24 00:48:19 +0000 | [diff] [blame] | 519 | for line in self.connection.before.split('\r\n'): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 520 | longbuf.append(line.strip()) |
| 521 | return longbuf |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 522 | |
| 523 | def _get_port_names(self): |
| 524 | logging.debug("Grabbing list of ports") |
| 525 | interfaces = [] |
| 526 | |
| 527 | # Use "Up" or "Down" to only identify lines in the output that |
| 528 | # match interfaces that exist |
| 529 | regex = re.compile('^\s*([a-zA-Z0-9_/]*).*(connect)(.*)') |
| 530 | regex1 = re.compile('.*Not Present.*') |
Steve McIntyre | 6c279b4 | 2014-12-23 22:09:04 +0000 | [diff] [blame] | 531 | regex2 = re.compile('.*routed.*') |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 532 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 533 | try: |
| 534 | self._cli("show interfaces status") |
| 535 | for line in self._read_long_output(): |
| 536 | match = regex.match(line) |
| 537 | if match: |
| 538 | interface = match.group(1) |
| 539 | junk = match.group(3) |
| 540 | match1 = regex1.match(junk) # Deliberately drop things |
| 541 | # marked as "Not Present" |
| 542 | match2 = regex2.match(junk) # Deliberately drop things |
| 543 | # marked as "routed" |
| 544 | if not match1 and not match2: |
| 545 | interfaces.append(interface) |
| 546 | return interfaces |
| 547 | |
| 548 | except PExpectError: |
| 549 | # recurse on error |
| 550 | self._switch_connect() |
| 551 | return self._get_port_names() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 552 | |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 553 | def _show_config(self): |
| 554 | logging.debug("Grabbing config") |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 555 | try: |
| 556 | self._cli("show running-config") |
| 557 | return self._read_long_output() |
| 558 | except PExpectError: |
| 559 | # recurse on error |
| 560 | self._switch_connect() |
| 561 | return self._show_config() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 562 | |
| 563 | def _show_clock(self): |
| 564 | logging.debug("Grabbing time") |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 565 | try: |
| 566 | self._cli("show clock") |
| 567 | return self._read_long_output() |
| 568 | except PExpectError: |
| 569 | # recurse on error |
| 570 | self._switch_connect() |
| 571 | return self._show_clock() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 572 | |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 573 | def _get_systemdata(self): |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 574 | logging.debug("Grabbing system sw and hw versions") |
Steve McIntyre | ffb9b5a | 2014-10-10 16:31:58 +0100 | [diff] [blame] | 575 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 576 | try: |
| 577 | self._systemdata = [] |
| 578 | self._cli("show version") |
| 579 | for line in self._read_long_output(): |
| 580 | self._systemdata.append(line) |
| 581 | |
| 582 | except PExpectError: |
| 583 | # recurse on error |
| 584 | self._switch_connect() |
| 585 | return self._get_systemdata() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 586 | |
Steve McIntyre | 2b4c07b | 2014-12-22 16:10:04 +0000 | [diff] [blame] | 587 | def _parse_vlan_list(self, inputdata): |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 588 | vlans = [] |
| 589 | |
Steve McIntyre | 2b4c07b | 2014-12-22 16:10:04 +0000 | [diff] [blame] | 590 | if inputdata == "ALL": |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 591 | return ["ALL"] |
Steve McIntyre | 2b4c07b | 2014-12-22 16:10:04 +0000 | [diff] [blame] | 592 | elif inputdata == "NONE": |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 593 | return [] |
| 594 | else: |
| 595 | # Parse the complex list |
Steve McIntyre | 2b4c07b | 2014-12-22 16:10:04 +0000 | [diff] [blame] | 596 | groups = inputdata.split(',') |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 597 | for group in groups: |
| 598 | subgroups = group.split('-') |
| 599 | if len(subgroups) == 1: |
| 600 | vlans.append(int(subgroups[0])) |
| 601 | elif len(subgroups) == 2: |
| 602 | for i in range (int(subgroups[0]), int(subgroups[1]) + 1): |
| 603 | vlans.append(i) |
| 604 | else: |
Steve McIntyre | 6d5594f | 2014-12-23 14:28:47 +0000 | [diff] [blame] | 605 | logging.debug("Can't parse group \"" + group + "\"") |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 606 | |
| 607 | return vlans |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 608 | |
| 609 | # Wrapper around connection.send - by default, expect() the same |
| 610 | # text we've sent, to remove it from the output from the |
| 611 | # switch. For the few cases where we don't need that, override |
| 612 | # this using echo=False. |
| 613 | # Horrible, but seems to work. |
| 614 | def _cli(self, text, echo=True): |
| 615 | self.connection.send(text + '\r') |
| 616 | if echo: |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 617 | try: |
| 618 | self.connection.expect(text) |
| 619 | except (pexpect.EOF, pexpect.TIMEOUT): |
| 620 | # Something went wrong; logout, log in and try again! |
Steve McIntyre | 7a9c819 | 2015-02-12 07:15:52 +0000 | [diff] [blame] | 621 | logging.error("PEXPECT FAILURE, RECONNECT") |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 622 | self.errors.log_error_out(text) |
| 623 | raise PExpectError("_cli failed on %s" % text) |
| 624 | except: |
Steve McIntyre | a67473a | 2015-02-12 08:26:33 +0000 | [diff] [blame] | 625 | logging.error("Unexpected error: %s", sys.exc_info()[0]) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 626 | raise |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 627 | |
| 628 | if __name__ == "__main__": |
Steve McIntyre | 48dc6ae | 2014-12-23 16:08:19 +0000 | [diff] [blame] | 629 | |
| 630 | import optparse |
| 631 | |
| 632 | switch = 'vlandswitch01' |
| 633 | parser = optparse.OptionParser() |
| 634 | parser.add_option("--switch", |
| 635 | dest = "switch", |
| 636 | action = "store", |
| 637 | nargs = 1, |
| 638 | type = "string", |
| 639 | help = "specify switch to connect to for testing", |
| 640 | metavar = "<switch>") |
| 641 | (opts, args) = parser.parse_args() |
| 642 | if opts.switch: |
| 643 | switch = opts.switch |
| 644 | |
| 645 | p = CiscoCatalyst(switch, 23, debug=True) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 646 | p.switch_connect(None, 'lngvirtual', 'lngenable') |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 647 | |
| 648 | print "VLANs are:" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 649 | buf = p.vlan_get_list() |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 650 | p.dump_list(buf) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 651 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 652 | buf = p.vlan_get_name(2) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 653 | print "VLAN 2 is named \"%s\"" % buf |
| 654 | |
| 655 | print "Create VLAN 3" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 656 | p.vlan_create(3) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 657 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 658 | buf = p.vlan_get_name(3) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 659 | print "VLAN 3 is named \"%s\"" % buf |
| 660 | |
| 661 | print "Set name of VLAN 3 to test333" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 662 | p.vlan_set_name(3, "test333") |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 663 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 664 | buf = p.vlan_get_name(3) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 665 | print "VLAN 3 is named \"%s\"" % buf |
| 666 | |
| 667 | print "VLANs are:" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 668 | buf = p.vlan_get_list() |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 669 | p.dump_list(buf) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 670 | |
| 671 | print "Destroy VLAN 3" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 672 | p.vlan_destroy(3) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 673 | |
| 674 | print "VLANs are:" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 675 | buf = p.vlan_get_list() |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 676 | p.dump_list(buf) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 677 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 678 | buf = p.port_get_mode("Gi1/0/10") |
Steve McIntyre | b7adc78 | 2014-08-13 00:22:21 +0100 | [diff] [blame] | 679 | print "Port Gi1/0/10 is in %s mode" % buf |
| 680 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 681 | buf = p.port_get_mode("Gi1/0/11") |
Steve McIntyre | b7adc78 | 2014-08-13 00:22:21 +0100 | [diff] [blame] | 682 | print "Port Gi1/0/11 is in %s mode" % buf |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 683 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 684 | # Test access stuff |
| 685 | print "Set Gi1/0/9 to access mode" |
| 686 | p.port_set_mode("Gi1/0/9", "access") |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 687 | |
| 688 | print "Move Gi1/0/9 to VLAN 4" |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 689 | p.port_set_access_vlan("Gi1/0/9", 4) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 690 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 691 | buf = p.port_get_access_vlan("Gi1/0/9") |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 692 | print "Read from switch: Gi1/0/9 is on VLAN %s" % buf |
| 693 | |
| 694 | print "Move Gi1/0/9 back to VLAN 1" |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 695 | p.port_set_access_vlan("Gi1/0/9", 1) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 696 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 697 | # Test access stuff |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 698 | print "Set Gi1/0/9 to trunk mode" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 699 | p.port_set_mode("Gi1/0/9", "trunk") |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 700 | print "Read from switch: which VLANs is Gi1/0/9 on?" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 701 | buf = p.port_get_trunk_vlan_list("Gi1/0/9") |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 702 | p.dump_list(buf) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 703 | print "Add Gi1/0/9 to VLAN 2" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 704 | p.port_add_trunk_to_vlan("Gi1/0/9", 2) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 705 | print "Add Gi1/0/9 to VLAN 3" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 706 | p.port_add_trunk_to_vlan("Gi1/0/9", 3) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 707 | print "Add Gi1/0/9 to VLAN 4" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 708 | p.port_add_trunk_to_vlan("Gi1/0/9", 4) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 709 | print "Read from switch: which VLANs is Gi1/0/9 on?" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 710 | buf = p.port_get_trunk_vlan_list("Gi1/0/9") |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 711 | p.dump_list(buf) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 712 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 713 | p.port_remove_trunk_from_vlan("Gi1/0/9", 3) |
| 714 | p.port_remove_trunk_from_vlan("Gi1/0/9", 3) |
| 715 | p.port_remove_trunk_from_vlan("Gi1/0/9", 4) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 716 | print "Read from switch: which VLANs is Gi1/0/9 on?" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 717 | buf = p.port_get_trunk_vlan_list("Gi1/0/9") |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame^] | 718 | p.dump_list(buf) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 719 | |
Steve McIntyre | 7460d97 | 2014-12-23 14:45:30 +0000 | [diff] [blame] | 720 | # print 'Restarting switch, to explicitly reset config' |
| 721 | # p.switch_restart() |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 722 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 723 | # p.switch_save_running_config() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 724 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 725 | # p.switch_disconnect() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 726 | # p._show_config() |