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