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