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