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 | 06c644a | 2015-07-17 17:07:41 +0100 | [diff] [blame^] | 410 | self.connection = pexpect.spawn(self.exec_string, logfile=self.logger) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 411 | self._login() |
| 412 | |
| 413 | # Avoid paged output |
| 414 | self._cli("terminal length 0") |
| 415 | |
| 416 | # And grab details about the switch. in case we need it |
| 417 | self._get_systemdata() |
| 418 | |
| 419 | # And also validate them - make sure we're driving a switch of |
| 420 | # the correct model! Also store the serial number |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 421 | descr_regex = re.compile(r'^cisco\s+(\S+)') |
| 422 | sn_regex = re.compile(r'System serial number\s+:\s+(\S+)') |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 423 | descr = "" |
| 424 | |
| 425 | for line in self._systemdata: |
| 426 | match = descr_regex.match(line) |
| 427 | if match: |
| 428 | descr = match.group(1) |
| 429 | match = sn_regex.match(line) |
| 430 | if match: |
| 431 | self.serial_number = match.group(1) |
| 432 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 433 | logging.debug("serial number is %s", self.serial_number) |
| 434 | logging.debug("system description is %s", descr) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 435 | |
| 436 | if not self._expected_descr_re.match(descr): |
| 437 | raise IOError("Switch %s not recognised by this driver: abort" % descr) |
| 438 | |
| 439 | # Now build a list of our ports, for later sanity checking |
| 440 | self._ports = self._get_port_names() |
| 441 | if len(self._ports) < 4: |
| 442 | raise IOError("Not enough ports detected - problem!") |
| 443 | |
| 444 | def _login(self): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 445 | 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] | 446 | self.connection.expect('User Access Verification') |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 447 | if self._username is not None: |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 448 | self.connection.expect("User Name:") |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 449 | self._cli("%s" % self._username) |
| 450 | if self._password is not None: |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 451 | self.connection.expect("Password:") |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 452 | self._cli("%s" % self._password, False) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 453 | while True: |
| 454 | index = self.connection.expect(['User Name:', 'Password:', 'Bad passwords', 'authentication failed', r'(.*)(#|>)']) |
| 455 | if index != 4: # Any other means: failed to log in! |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 456 | logging.error("Login failure: index %d\n", index) |
| 457 | logging.error("Login failure: %s\n", self.connection.match.before) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 458 | raise IOError |
| 459 | |
| 460 | # else |
Steve McIntyre | 65dfe7f | 2015-06-09 15:57:02 +0100 | [diff] [blame] | 461 | self._prompt_name = re.escape(self.connection.match.group(1).strip()) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 462 | if self.connection.match.group(2) == ">": |
| 463 | # Need to enter "enable" mode too |
| 464 | self._cli("enable") |
Steve McIntyre | ad12cd7 | 2015-02-12 06:41:29 +0000 | [diff] [blame] | 465 | if self._enable_password is not None: |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 466 | self.connection.expect("Password:") |
Steve McIntyre | ad12cd7 | 2015-02-12 06:41:29 +0000 | [diff] [blame] | 467 | self._cli("%s" % self._enable_password, False) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 468 | index = self.connection.expect(['Password:', 'Bad passwords', 'authentication failed', r'(.*)(#|>)']) |
| 469 | if index != 3: # Any other means: failed to log in! |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 470 | logging.error("Enable password failure: %s\n", self.connection.match) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 471 | raise IOError |
| 472 | return 0 |
| 473 | |
| 474 | def _logout(self): |
| 475 | logging.debug("Logging out") |
| 476 | self._cli("exit", False) |
| 477 | |
| 478 | def _configure(self): |
| 479 | self._cli("configure terminal") |
| 480 | |
| 481 | def _end_configure(self): |
| 482 | self._cli("end") |
| 483 | |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 484 | def _read_long_output(self, text): |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 485 | prompt = self._prompt_name + '#' |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 486 | try: |
| 487 | self.connection.expect(prompt) |
| 488 | except (pexpect.EOF, pexpect.TIMEOUT): |
| 489 | # Something went wrong; logout, log in and try again! |
Steve McIntyre | 7a9c819 | 2015-02-12 07:15:52 +0000 | [diff] [blame] | 490 | logging.error("PEXPECT FAILURE, RECONNECT") |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 491 | self.errors.log_error_in(text) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 492 | raise PExpectError("_read_long_output failed") |
| 493 | except: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 494 | logging.error("prompt is \"%s\"", prompt) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 495 | raise |
| 496 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 497 | longbuf = [] |
Steve McIntyre | a7cdefc | 2014-12-24 00:48:19 +0000 | [diff] [blame] | 498 | for line in self.connection.before.split('\r\n'): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 499 | longbuf.append(line.strip()) |
| 500 | return longbuf |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 501 | |
| 502 | def _get_port_names(self): |
| 503 | logging.debug("Grabbing list of ports") |
| 504 | interfaces = [] |
| 505 | |
| 506 | # Use "Up" or "Down" to only identify lines in the output that |
| 507 | # match interfaces that exist |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 508 | regex = re.compile(r'^\s*([a-zA-Z0-9_/]*).*(connect)(.*)') |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 509 | regex1 = re.compile('.*Not Present.*') |
Steve McIntyre | 6c279b4 | 2014-12-23 22:09:04 +0000 | [diff] [blame] | 510 | regex2 = re.compile('.*routed.*') |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 511 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 512 | try: |
| 513 | self._cli("show interfaces status") |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 514 | for line in self._read_long_output("show interfaces status"): |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 515 | match = regex.match(line) |
| 516 | if match: |
| 517 | interface = match.group(1) |
| 518 | junk = match.group(3) |
| 519 | match1 = regex1.match(junk) # Deliberately drop things |
| 520 | # marked as "Not Present" |
| 521 | match2 = regex2.match(junk) # Deliberately drop things |
| 522 | # marked as "routed" |
| 523 | if not match1 and not match2: |
| 524 | interfaces.append(interface) |
Steve McIntyre | d601ab8 | 2015-07-09 17:42:36 +0100 | [diff] [blame] | 525 | logging.debug(" found %d ports on the switch", len(interfaces)) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 526 | return interfaces |
| 527 | |
| 528 | except PExpectError: |
| 529 | # recurse on error |
| 530 | self._switch_connect() |
| 531 | return self._get_port_names() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 532 | |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 533 | def _show_config(self): |
| 534 | logging.debug("Grabbing config") |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 535 | try: |
| 536 | self._cli("show running-config") |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 537 | return self._read_long_output("show running-config") |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 538 | except PExpectError: |
| 539 | # recurse on error |
| 540 | self._switch_connect() |
| 541 | return self._show_config() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 542 | |
| 543 | def _show_clock(self): |
| 544 | logging.debug("Grabbing time") |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 545 | try: |
| 546 | self._cli("show clock") |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 547 | return self._read_long_output("show clock") |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 548 | except PExpectError: |
| 549 | # recurse on error |
| 550 | self._switch_connect() |
| 551 | return self._show_clock() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 552 | |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 553 | def _get_systemdata(self): |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 554 | logging.debug("Grabbing system sw and hw versions") |
Steve McIntyre | ffb9b5a | 2014-10-10 16:31:58 +0100 | [diff] [blame] | 555 | |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 556 | try: |
| 557 | self._systemdata = [] |
| 558 | self._cli("show version") |
Steve McIntyre | f5fb22b | 2015-04-01 18:19:54 +0100 | [diff] [blame] | 559 | for line in self._read_long_output("show version"): |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 560 | self._systemdata.append(line) |
| 561 | |
| 562 | except PExpectError: |
| 563 | # recurse on error |
| 564 | self._switch_connect() |
| 565 | return self._get_systemdata() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 566 | |
Steve McIntyre | 2b4c07b | 2014-12-22 16:10:04 +0000 | [diff] [blame] | 567 | def _parse_vlan_list(self, inputdata): |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 568 | vlans = [] |
| 569 | |
Steve McIntyre | 2b4c07b | 2014-12-22 16:10:04 +0000 | [diff] [blame] | 570 | if inputdata == "ALL": |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 571 | return ["ALL"] |
Steve McIntyre | 2b4c07b | 2014-12-22 16:10:04 +0000 | [diff] [blame] | 572 | elif inputdata == "NONE": |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 573 | return [] |
| 574 | else: |
| 575 | # Parse the complex list |
Steve McIntyre | 2b4c07b | 2014-12-22 16:10:04 +0000 | [diff] [blame] | 576 | groups = inputdata.split(',') |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 577 | for group in groups: |
| 578 | subgroups = group.split('-') |
| 579 | if len(subgroups) == 1: |
| 580 | vlans.append(int(subgroups[0])) |
| 581 | elif len(subgroups) == 2: |
| 582 | for i in range (int(subgroups[0]), int(subgroups[1]) + 1): |
| 583 | vlans.append(i) |
| 584 | else: |
Steve McIntyre | 6d5594f | 2014-12-23 14:28:47 +0000 | [diff] [blame] | 585 | logging.debug("Can't parse group \"" + group + "\"") |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 586 | |
| 587 | return vlans |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 588 | |
| 589 | # Wrapper around connection.send - by default, expect() the same |
| 590 | # text we've sent, to remove it from the output from the |
| 591 | # switch. For the few cases where we don't need that, override |
| 592 | # this using echo=False. |
| 593 | # Horrible, but seems to work. |
| 594 | def _cli(self, text, echo=True): |
| 595 | self.connection.send(text + '\r') |
| 596 | if echo: |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 597 | try: |
| 598 | self.connection.expect(text) |
| 599 | except (pexpect.EOF, pexpect.TIMEOUT): |
| 600 | # Something went wrong; logout, log in and try again! |
Steve McIntyre | 7a9c819 | 2015-02-12 07:15:52 +0000 | [diff] [blame] | 601 | logging.error("PEXPECT FAILURE, RECONNECT") |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 602 | self.errors.log_error_out(text) |
| 603 | raise PExpectError("_cli failed on %s" % text) |
| 604 | except: |
Steve McIntyre | a67473a | 2015-02-12 08:26:33 +0000 | [diff] [blame] | 605 | logging.error("Unexpected error: %s", sys.exc_info()[0]) |
Steve McIntyre | 3dfd36f | 2015-02-12 06:37:56 +0000 | [diff] [blame] | 606 | raise |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 607 | |
| 608 | if __name__ == "__main__": |
Steve McIntyre | 48dc6ae | 2014-12-23 16:08:19 +0000 | [diff] [blame] | 609 | |
| 610 | import optparse |
| 611 | |
| 612 | switch = 'vlandswitch01' |
| 613 | parser = optparse.OptionParser() |
| 614 | parser.add_option("--switch", |
| 615 | dest = "switch", |
| 616 | action = "store", |
| 617 | nargs = 1, |
| 618 | type = "string", |
| 619 | help = "specify switch to connect to for testing", |
| 620 | metavar = "<switch>") |
| 621 | (opts, args) = parser.parse_args() |
| 622 | if opts.switch: |
| 623 | switch = opts.switch |
| 624 | |
Steve McIntyre | 144d51c | 2015-07-07 17:56:30 +0100 | [diff] [blame] | 625 | logging.basicConfig(level = logging.DEBUG, |
| 626 | format = '%(asctime)s %(levelname)-8s %(message)s') |
Steve McIntyre | 48dc6ae | 2014-12-23 16:08:19 +0000 | [diff] [blame] | 627 | p = CiscoCatalyst(switch, 23, debug=True) |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 628 | p.switch_connect(None, 'lngvirtual', 'lngenable') |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 629 | |
| 630 | print "VLANs are:" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 631 | buf = p.vlan_get_list() |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 632 | p.dump_list(buf) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 633 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 634 | buf = p.vlan_get_name(2) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 635 | print "VLAN 2 is named \"%s\"" % buf |
| 636 | |
| 637 | print "Create VLAN 3" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 638 | p.vlan_create(3) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 639 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 640 | buf = p.vlan_get_name(3) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 641 | print "VLAN 3 is named \"%s\"" % buf |
| 642 | |
| 643 | print "Set name of VLAN 3 to test333" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 644 | p.vlan_set_name(3, "test333") |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 645 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 646 | buf = p.vlan_get_name(3) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 647 | print "VLAN 3 is named \"%s\"" % buf |
| 648 | |
| 649 | print "VLANs are:" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 650 | buf = p.vlan_get_list() |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 651 | p.dump_list(buf) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 652 | |
| 653 | print "Destroy VLAN 3" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 654 | p.vlan_destroy(3) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 655 | |
| 656 | print "VLANs are:" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 657 | buf = p.vlan_get_list() |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 658 | p.dump_list(buf) |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 659 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 660 | buf = p.port_get_mode("Gi1/0/10") |
Steve McIntyre | b7adc78 | 2014-08-13 00:22:21 +0100 | [diff] [blame] | 661 | print "Port Gi1/0/10 is in %s mode" % buf |
| 662 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 663 | buf = p.port_get_mode("Gi1/0/11") |
Steve McIntyre | b7adc78 | 2014-08-13 00:22:21 +0100 | [diff] [blame] | 664 | print "Port Gi1/0/11 is in %s mode" % buf |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 665 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 666 | # Test access stuff |
| 667 | print "Set Gi1/0/9 to access mode" |
| 668 | p.port_set_mode("Gi1/0/9", "access") |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 669 | |
| 670 | print "Move Gi1/0/9 to VLAN 4" |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 671 | p.port_set_access_vlan("Gi1/0/9", 4) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 672 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 673 | buf = p.port_get_access_vlan("Gi1/0/9") |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 674 | print "Read from switch: Gi1/0/9 is on VLAN %s" % buf |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 675 | |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 676 | print "Move Gi1/0/9 back to VLAN 1" |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 677 | p.port_set_access_vlan("Gi1/0/9", 1) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 678 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 679 | # Test access stuff |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 680 | print "Set Gi1/0/9 to trunk mode" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 681 | p.port_set_mode("Gi1/0/9", "trunk") |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 682 | print "Read from switch: which VLANs is Gi1/0/9 on?" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 683 | buf = p.port_get_trunk_vlan_list("Gi1/0/9") |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 684 | p.dump_list(buf) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 685 | print "Add Gi1/0/9 to VLAN 2" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 686 | p.port_add_trunk_to_vlan("Gi1/0/9", 2) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 687 | print "Add Gi1/0/9 to VLAN 3" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 688 | p.port_add_trunk_to_vlan("Gi1/0/9", 3) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 689 | print "Add Gi1/0/9 to VLAN 4" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 690 | p.port_add_trunk_to_vlan("Gi1/0/9", 4) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 691 | print "Read from switch: which VLANs is Gi1/0/9 on?" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 692 | buf = p.port_get_trunk_vlan_list("Gi1/0/9") |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 693 | p.dump_list(buf) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 694 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 695 | p.port_remove_trunk_from_vlan("Gi1/0/9", 3) |
| 696 | p.port_remove_trunk_from_vlan("Gi1/0/9", 3) |
| 697 | p.port_remove_trunk_from_vlan("Gi1/0/9", 4) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 698 | print "Read from switch: which VLANs is Gi1/0/9 on?" |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 699 | buf = p.port_get_trunk_vlan_list("Gi1/0/9") |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 700 | p.dump_list(buf) |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 701 | |
Steve McIntyre | 7460d97 | 2014-12-23 14:45:30 +0000 | [diff] [blame] | 702 | # print 'Restarting switch, to explicitly reset config' |
| 703 | # p.switch_restart() |
Steve McIntyre | 3f28788 | 2014-08-18 19:02:15 +0100 | [diff] [blame] | 704 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 705 | # p.switch_save_running_config() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 706 | |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 707 | # p.switch_disconnect() |
Steve McIntyre | d6759dd | 2014-08-12 18:10:00 +0100 | [diff] [blame] | 708 | # p._show_config() |