Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2015 Linaro Limited |
| 4 | # |
| 5 | # This program is free software; you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation; either version 2 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program; if not, write to the Free Software |
| 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | # MA 02110-1301, USA. |
| 19 | |
| 20 | import logging |
| 21 | import pexpect |
| 22 | import sys |
| 23 | import re |
| 24 | |
| 25 | # Netgear XSM family driver |
| 26 | # Developed and tested against the XSM7224S in the Linaro LAVA lab |
| 27 | |
| 28 | if __name__ == '__main__': |
| 29 | import os |
| 30 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 31 | sys.path.insert(0, vlandpath) |
| 32 | sys.path.insert(0, "%s/.." % vlandpath) |
| 33 | |
| 34 | from errors import InputError, PExpectError |
| 35 | from drivers.common import SwitchDriver, SwitchErrors |
| 36 | |
| 37 | class NetgearXSM(SwitchDriver): |
| 38 | |
| 39 | connection = None |
| 40 | _username = None |
| 41 | _password = None |
| 42 | _enable_password = None |
| 43 | |
| 44 | _capabilities = [ |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 45 | ] |
| 46 | |
| 47 | # Regexps of expected hardware information - fail if we don't see |
| 48 | # this |
| 49 | _expected_manuf = re.compile('^Netgear') |
| 50 | _expected_model = re.compile('^XSM') |
| 51 | |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 52 | def __init__(self, switch_hostname, switch_telnetport=23, debug = False): |
Steve McIntyre | bb58a27 | 2015-07-14 15:39:54 +0100 | [diff] [blame] | 53 | SwitchDriver.__init__(self, switch_hostname, debug) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 54 | self._systemdata = [] |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 55 | self.exec_string = "/usr/bin/telnet %s %d" % (switch_hostname, switch_telnetport) |
| 56 | self.errors = SwitchErrors() |
| 57 | |
| 58 | ################################ |
| 59 | ### Switch-level API functions |
| 60 | ################################ |
| 61 | |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 62 | # Save the current running config into flash - we want config to |
| 63 | # remain across reboots |
| 64 | def switch_save_running_config(self): |
| 65 | try: |
| 66 | self._cli("save") |
| 67 | self.connection.expect("Are you sure") |
| 68 | self._cli("y") |
| 69 | self.connection.expect("Configuration Saved!") |
| 70 | except (PExpectError, pexpect.EOF): |
| 71 | # recurse on error |
| 72 | self._switch_connect() |
| 73 | self.switch_save_running_config() |
| 74 | |
| 75 | # Restart the switch - we need to reload config to do a |
| 76 | # roll-back. Do NOT save running-config first if the switch asks - |
| 77 | # we're trying to dump recent changes, not save them. |
| 78 | # |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 79 | # This will also implicitly cause a connection to be closed |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 80 | def switch_restart(self): |
| 81 | self._cli("reload") |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 82 | self.connection.expect('Are you sure') |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 83 | self._cli("y") # Yes, continue to reset |
| 84 | self.connection.close(True) |
| 85 | |
| 86 | # List the capabilities of the switch (and driver) - some things |
| 87 | # make no sense to abstract. Returns a dict of strings, each one |
| 88 | # describing an extra feature that that higher levels may care |
| 89 | # about |
| 90 | def switch_get_capabilities(self): |
| 91 | return self._capabilities |
| 92 | |
| 93 | ################################ |
| 94 | ### VLAN API functions |
| 95 | ################################ |
| 96 | |
| 97 | # Create a VLAN with the specified tag |
| 98 | def vlan_create(self, tag): |
| 99 | logging.debug("Creating VLAN %d", tag) |
| 100 | try: |
| 101 | self._cli("vlan database") |
| 102 | self._cli("vlan %d" % tag) |
| 103 | self._end_configure() |
| 104 | |
| 105 | # Validate it happened |
| 106 | vlans = self.vlan_get_list() |
| 107 | for vlan in vlans: |
| 108 | if vlan == tag: |
| 109 | return |
| 110 | raise IOError("Failed to create VLAN %d" % tag) |
| 111 | |
| 112 | except PExpectError: |
| 113 | # recurse on error |
| 114 | self._switch_connect() |
| 115 | self.vlan_create(tag) |
| 116 | |
| 117 | # Destroy a VLAN with the specified tag |
| 118 | def vlan_destroy(self, tag): |
| 119 | logging.debug("Destroying VLAN %d", tag) |
| 120 | |
| 121 | try: |
| 122 | self._cli("vlan database") |
| 123 | self._cli("no vlan %d" % tag) |
| 124 | self._end_configure() |
| 125 | |
| 126 | # Validate it happened |
| 127 | vlans = self.vlan_get_list() |
| 128 | for vlan in vlans: |
| 129 | if vlan == tag: |
| 130 | raise IOError("Failed to destroy VLAN %d" % tag) |
| 131 | |
| 132 | except PExpectError: |
| 133 | # recurse on error |
| 134 | self._switch_connect() |
| 135 | self.vlan_destroy(tag) |
| 136 | |
| 137 | # Set the name of a VLAN |
| 138 | def vlan_set_name(self, tag, name): |
| 139 | logging.debug("Setting name of VLAN %d to %s", tag, name) |
| 140 | |
| 141 | try: |
| 142 | self._cli("vlan database") |
| 143 | self._cli("vlan name %d %s" % (tag, name)) |
| 144 | self._end_configure() |
| 145 | |
| 146 | # Validate it happened |
| 147 | read_name = self.vlan_get_name(tag) |
| 148 | if read_name != name: |
| 149 | raise IOError("Failed to set name for VLAN %d (name found is \"%s\", not \"%s\")" |
| 150 | % (tag, read_name, name)) |
| 151 | except PExpectError: |
| 152 | # recurse on error |
| 153 | self._switch_connect() |
| 154 | self.vlan_set_name(tag, name) |
| 155 | |
| 156 | # Get a list of the VLAN tags currently registered on the switch |
| 157 | def vlan_get_list(self): |
| 158 | logging.debug("Grabbing list of VLANs") |
| 159 | |
| 160 | try: |
| 161 | vlans = [] |
| 162 | |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 163 | regex = re.compile(r'^ *(\d+).*(Static)') |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 164 | |
| 165 | self._cli("show vlan brief") |
| 166 | for line in self._read_long_output("show vlan brief"): |
| 167 | match = regex.match(line) |
| 168 | if match: |
| 169 | vlans.append(int(match.group(1))) |
| 170 | return vlans |
| 171 | |
| 172 | except PExpectError: |
| 173 | # recurse on error |
| 174 | self._switch_connect() |
| 175 | return self.vlan_get_list() |
| 176 | |
| 177 | # For a given VLAN tag, ask the switch what the associated name is |
| 178 | def vlan_get_name(self, tag): |
| 179 | |
| 180 | try: |
| 181 | logging.debug("Grabbing the name of VLAN %d", tag) |
| 182 | name = None |
| 183 | regex = re.compile('VLAN Name: (.*)') |
| 184 | self._cli("show vlan %d" % tag) |
| 185 | for line in self._read_long_output("show vlan"): |
| 186 | match = regex.match(line) |
| 187 | if match: |
| 188 | name = match.group(1) |
| 189 | name.strip() |
| 190 | return name |
| 191 | |
| 192 | except PExpectError: |
| 193 | # recurse on error |
| 194 | self._switch_connect() |
| 195 | return self.vlan_get_name(tag) |
| 196 | |
| 197 | ################################ |
| 198 | ### Port API functions |
| 199 | ################################ |
| 200 | |
| 201 | # Set the mode of a port: access or trunk |
| 202 | def port_set_mode(self, port, mode): |
Steve McIntyre | e4c97ec | 2015-07-16 18:27:59 +0100 | [diff] [blame] | 203 | logging.debug("Setting port %s to %s mode", port, mode) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 204 | if not self._is_port_mode_valid(mode): |
| 205 | raise InputError("Port mode %s is not allowed" % mode) |
| 206 | if not self._is_port_name_valid(port): |
| 207 | raise InputError("Port name %s not recognised" % port) |
| 208 | |
| 209 | # This switch does not support specific modes, so we can't |
| 210 | # actually change the mode directly. However, we can and |
| 211 | # should deal with the PVID and memberships of existing VLANs |
| 212 | # etc. |
| 213 | |
| 214 | try: |
| 215 | if mode == "trunk": |
| 216 | # We define a trunk port thus: |
Steve McIntyre | 4f94529 | 2015-07-16 18:31:21 +0100 | [diff] [blame] | 217 | # * accept all frames on ingress |
| 218 | # * accept packets for all VLANs (no ingress filter) |
| 219 | # * tags frames on transmission (do that later when |
| 220 | # * adding VLANs to the port) |
| 221 | # * PVID should match the default VLAN (1). |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 222 | self._configure() |
| 223 | self._cli("interface %s" % port) |
Steve McIntyre | d97ab29 | 2015-07-07 14:23:32 +0100 | [diff] [blame] | 224 | self._cli("vlan acceptframe all") |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 225 | self._cli("no vlan ingressfilter") |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 226 | self._cli("vlan pvid 1") |
Steve McIntyre | fc7a627 | 2015-07-07 16:22:32 +0100 | [diff] [blame] | 227 | self._end_interface() |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 228 | self._end_configure() |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 229 | |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 230 | # We define an access port thus: |
Steve McIntyre | 4f94529 | 2015-07-16 18:31:21 +0100 | [diff] [blame] | 231 | # * accept only untagged frames on ingress |
| 232 | # * accept packets for only desired VLANs (ingress filter) |
| 233 | # * exists on one VLAN only (1 by default) |
| 234 | # * do not tag frames on transmission (the devices |
| 235 | # we're talking to are expecting untagged frames) |
| 236 | # * PVID should match the VLAN it's on (1 by default, |
| 237 | # but don't do that here) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 238 | if mode == "access": |
| 239 | self._configure() |
| 240 | self._cli("interface %s" % port) |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 241 | self._cli("vlan acceptframe admituntaggedonly") |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 242 | self._cli("vlan ingressfilter") |
Steve McIntyre | 53d4719 | 2015-07-07 16:24:04 +0100 | [diff] [blame] | 243 | self._cli("no vlan tagging 1-1023") |
| 244 | self._cli("no vlan tagging 1024-2047") |
Steve McIntyre | 533519d | 2015-07-07 16:17:34 +0100 | [diff] [blame] | 245 | self._cli("no vlan tagging 2048-3071") |
| 246 | self._cli("no vlan tagging 3072-4093") |
Steve McIntyre | fc7a627 | 2015-07-07 16:22:32 +0100 | [diff] [blame] | 247 | self._end_interface() |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 248 | self._end_configure() |
| 249 | |
| 250 | # Validate it happened |
Steve McIntyre | b79cea5 | 2015-07-24 17:03:31 +0100 | [diff] [blame^] | 251 | read_mode = self._port_get_mode(port) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 252 | |
| 253 | if read_mode != mode: |
| 254 | raise IOError("Failed to set mode for port %s" % port) |
| 255 | |
Steve McIntyre | 1a7bad5 | 2015-07-20 15:21:42 +0100 | [diff] [blame] | 256 | # And cache the result |
| 257 | self._port_modes[port] = mode |
| 258 | |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 259 | except PExpectError: |
| 260 | # recurse on error |
| 261 | self._switch_connect() |
| 262 | self.port_set_mode(port, mode) |
| 263 | |
| 264 | # Get the mode of a port: access or trunk |
| 265 | def port_get_mode(self, port): |
| 266 | logging.debug("Getting mode of port %s", port) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 267 | |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 268 | if not self._is_port_name_valid(port): |
| 269 | raise InputError("Port name %s not recognised" % port) |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 270 | acceptframe_re = re.compile('vlan acceptframe (.*)') |
| 271 | ingress_re = re.compile('vlan ingressfilter') |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 272 | |
| 273 | acceptframe = None |
| 274 | ingressfilter = True |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 275 | |
| 276 | try: |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 277 | self._cli("show running-config interface %s" % port) |
| 278 | for line in self._read_long_output("show running-config interface"): |
| 279 | |
| 280 | match = acceptframe_re.match(line) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 281 | if match: |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 282 | acceptframe = match.group(1) |
| 283 | |
| 284 | match = ingress_re.match(line) |
| 285 | if match: |
| 286 | ingressfilter = True |
| 287 | |
Steve McIntyre | bcbb422 | 2015-07-09 18:28:02 +0100 | [diff] [blame] | 288 | # Simple classifier for now; may need to revisit later... |
Steve McIntyre | 4f94529 | 2015-07-16 18:31:21 +0100 | [diff] [blame] | 289 | if (ingressfilter and acceptframe == "admituntaggedonly"): |
Steve McIntyre | bcbb422 | 2015-07-09 18:28:02 +0100 | [diff] [blame] | 290 | return "access" |
| 291 | else: |
| 292 | return "trunk" |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 293 | |
| 294 | except PExpectError: |
| 295 | # recurse on error |
| 296 | self._switch_connect() |
| 297 | return self.port_get_mode(port) |
| 298 | |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 299 | |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 300 | # Set an access port to be in a specified VLAN (tag) |
| 301 | def port_set_access_vlan(self, port, tag): |
| 302 | logging.debug("Setting access port %s to VLAN %d", port, tag) |
| 303 | if not self._is_port_name_valid(port): |
| 304 | raise InputError("Port name %s not recognised" % port) |
| 305 | if not (self.port_get_mode(port) == "access"): |
| 306 | raise InputError("Port %s not in access mode" % port) |
| 307 | |
| 308 | try: |
Steve McIntyre | e6172b0 | 2015-07-07 17:59:46 +0100 | [diff] [blame] | 309 | current_vlans = self._get_port_vlans(port) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 310 | self._configure() |
| 311 | self._cli("interface %s" % port) |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 312 | self._cli("vlan pvid %s" % tag) |
| 313 | # Find the list of VLANs we're currently on, and drop them |
| 314 | # all. "auto" mode is fine here, we won't be included |
| 315 | # unless we have GVRP configured, and we don't do |
| 316 | # that. |
Steve McIntyre | e6172b0 | 2015-07-07 17:59:46 +0100 | [diff] [blame] | 317 | for current_vlan in current_vlans: |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 318 | self._cli("vlan participation auto %s" % current_vlan) |
| 319 | # Now specifically include the VLAN we want |
| 320 | self._cli("vlan participation include %s" % tag) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 321 | self._cli("no shutdown") |
Steve McIntyre | fc7a627 | 2015-07-07 16:22:32 +0100 | [diff] [blame] | 322 | self._end_interface() |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 323 | self._end_configure() |
| 324 | |
| 325 | # Finally, validate things worked |
| 326 | read_vlan = int(self.port_get_access_vlan(port)) |
| 327 | if read_vlan != tag: |
| 328 | raise IOError("Failed to move access port %d to VLAN %d - got VLAN %d instead" |
| 329 | % (port, tag, read_vlan)) |
| 330 | |
| 331 | except PExpectError: |
| 332 | # recurse on error |
| 333 | self._switch_connect() |
| 334 | self.port_set_access_vlan(port, tag) |
| 335 | |
Steve McIntyre | 4f94529 | 2015-07-16 18:31:21 +0100 | [diff] [blame] | 336 | # Add a trunk port to a specified VLAN (tag) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 337 | def port_add_trunk_to_vlan(self, port, tag): |
| 338 | logging.debug("Adding trunk port %s to VLAN %d", port, tag) |
| 339 | if not self._is_port_name_valid(port): |
| 340 | raise InputError("Port name %s not recognised" % port) |
| 341 | if not (self.port_get_mode(port) == "trunk"): |
| 342 | raise InputError("Port %s not in trunk mode" % port) |
| 343 | |
| 344 | try: |
| 345 | self._configure() |
| 346 | self._cli("interface %s" % port) |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 347 | self._cli("vlan participation include %d" % tag) |
Steve McIntyre | 4f94529 | 2015-07-16 18:31:21 +0100 | [diff] [blame] | 348 | self._cli("vlan tagging %d" % tag) |
Steve McIntyre | fc7a627 | 2015-07-07 16:22:32 +0100 | [diff] [blame] | 349 | self._end_interface() |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 350 | self._end_configure() |
| 351 | |
| 352 | # Validate it happened |
| 353 | read_vlans = self.port_get_trunk_vlan_list(port) |
| 354 | for vlan in read_vlans: |
| 355 | if vlan == tag or vlan == "ALL": |
| 356 | return |
| 357 | raise IOError("Failed to add trunk port %s to VLAN %d" % (port, tag)) |
| 358 | |
| 359 | except PExpectError: |
| 360 | # recurse on error |
| 361 | self._switch_connect() |
| 362 | self.port_add_trunk_to_vlan(port, tag) |
| 363 | |
Steve McIntyre | 4f94529 | 2015-07-16 18:31:21 +0100 | [diff] [blame] | 364 | # Remove a trunk port from a specified VLAN (tag) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 365 | def port_remove_trunk_from_vlan(self, port, tag): |
| 366 | logging.debug("Removing trunk port %s from VLAN %d", port, tag) |
| 367 | if not self._is_port_name_valid(port): |
| 368 | raise InputError("Port name %s not recognised" % port) |
| 369 | if not (self.port_get_mode(port) == "trunk"): |
| 370 | raise InputError("Port %s not in trunk mode" % port) |
| 371 | |
| 372 | try: |
| 373 | self._configure() |
| 374 | self._cli("interface %s" % port) |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 375 | self._cli("vlan participation auto %d" % tag) |
Steve McIntyre | 4f94529 | 2015-07-16 18:31:21 +0100 | [diff] [blame] | 376 | self._cli("no vlan tagging %d" % tag) |
Steve McIntyre | fc7a627 | 2015-07-07 16:22:32 +0100 | [diff] [blame] | 377 | self._end_interface() |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 378 | self._end_configure() |
| 379 | |
| 380 | # Validate it happened |
| 381 | read_vlans = self.port_get_trunk_vlan_list(port) |
| 382 | for vlan in read_vlans: |
| 383 | if vlan == tag: |
| 384 | raise IOError("Failed to remove trunk port %s from VLAN %d" % (port, tag)) |
| 385 | |
| 386 | except PExpectError: |
| 387 | # recurse on error |
| 388 | self._switch_connect() |
| 389 | self.port_remove_trunk_from_vlan(port, tag) |
| 390 | |
| 391 | # Get the configured VLAN tag for an access port (tag) |
| 392 | def port_get_access_vlan(self, port): |
| 393 | logging.debug("Getting VLAN for access port %s", port) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 394 | if not self._is_port_name_valid(port): |
| 395 | raise InputError("Port name %s not recognised" % port) |
| 396 | if not (self.port_get_mode(port) == "access"): |
| 397 | raise InputError("Port %s not in access mode" % port) |
Steve McIntyre | c4834c7 | 2015-07-07 18:00:17 +0100 | [diff] [blame] | 398 | vlans = self._get_port_vlans(port) |
| 399 | if (len(vlans) > 1): |
| 400 | raise IOError("More than one VLAN on access port %s" % port) |
| 401 | return vlans[0] |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 402 | |
| 403 | # Get the list of configured VLAN tags for a trunk port |
| 404 | def port_get_trunk_vlan_list(self, port): |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 405 | logging.debug("Getting VLAN(s) for trunk port %s", port) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 406 | if not self._is_port_name_valid(port): |
| 407 | raise InputError("Port name %s not recognised" % port) |
| 408 | if not (self.port_get_mode(port) == "trunk"): |
| 409 | raise InputError("Port %s not in trunk mode" % port) |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 410 | return self._get_port_vlans(port) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 411 | |
| 412 | ################################ |
| 413 | ### Internal functions |
| 414 | ################################ |
| 415 | |
| 416 | # Connect to the switch and log in |
| 417 | def _switch_connect(self): |
| 418 | |
| 419 | if not self.connection is None: |
| 420 | self.connection.close(True) |
| 421 | self.connection = None |
| 422 | |
| 423 | logging.debug("Connecting to Switch with: %s", self.exec_string) |
Steve McIntyre | 06c644a | 2015-07-17 17:07:41 +0100 | [diff] [blame] | 424 | self.connection = pexpect.spawn(self.exec_string, logfile=self.logger) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 425 | self._login() |
| 426 | |
| 427 | # Avoid paged output |
| 428 | self._cli("terminal length 0") |
| 429 | |
| 430 | # And grab details about the switch. in case we need it |
| 431 | self._get_systemdata() |
| 432 | |
| 433 | # And also validate them - make sure we're driving a switch of |
| 434 | # the correct model! Also store the serial number |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 435 | manuf_regex = re.compile(r'^Manufacturer([\.\s])+(\S+)') |
| 436 | model_regex = re.compile(r'^Machine Model([\.\s])+(\S+)') |
| 437 | sn_regex = re.compile(r'^Serial Number([\.\s])+(\S+)') |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 438 | |
| 439 | for line in self._systemdata: |
| 440 | match1 = manuf_regex.match(line) |
| 441 | if match1: |
| 442 | manuf = match1.group(2) |
| 443 | |
| 444 | match2 = model_regex.match(line) |
| 445 | if match2: |
| 446 | model = match2.group(2) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 447 | |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 448 | match3 = sn_regex.match(line) |
| 449 | if match3: |
| 450 | self.serial_number = match3.group(2) |
| 451 | |
| 452 | logging.debug("manufacturer is %s", manuf) |
| 453 | logging.debug("model is %s", model) |
| 454 | logging.debug("serial number is %s", self.serial_number) |
| 455 | |
| 456 | if not (self._expected_manuf.match(manuf) and self._expected_model.match(model)): |
| 457 | raise IOError("Switch %s %s not recognised by this driver: abort" % (manuf, model)) |
| 458 | |
| 459 | # Now build a list of our ports, for later sanity checking |
| 460 | self._ports = self._get_port_names() |
| 461 | if len(self._ports) < 4: |
| 462 | raise IOError("Not enough ports detected - problem!") |
| 463 | |
| 464 | def _login(self): |
| 465 | logging.debug("attempting login with username %s, password %s", self._username, self._password) |
| 466 | if self._username is not None: |
| 467 | self.connection.expect("User:") |
| 468 | self._cli("%s" % self._username) |
| 469 | if self._password is not None: |
| 470 | self.connection.expect("Password:") |
| 471 | self._cli("%s" % self._password, False) |
| 472 | while True: |
| 473 | index = self.connection.expect(['User:', 'Password:', 'Bad passwords', 'authentication failed', r'(.*)(#|>)']) |
| 474 | if index != 4: # Any other means: failed to log in! |
| 475 | logging.error("Login failure: index %d\n", index) |
| 476 | logging.error("Login failure: %s\n", self.connection.match.before) |
| 477 | raise IOError |
| 478 | |
| 479 | # else |
| 480 | self._prompt_name = re.escape(self.connection.match.group(1).strip()) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 481 | if self.connection.match.group(2) == ">": |
| 482 | # Need to enter "enable" mode too |
| 483 | self._cli("enable") |
| 484 | if self._enable_password is not None: |
| 485 | self.connection.expect("Password:") |
| 486 | self._cli("%s" % self._enable_password, False) |
| 487 | index = self.connection.expect(['Password:', 'Bad passwords', 'authentication failed', r'(.*) *(#|>)']) |
| 488 | if index != 3: # Any other means: failed to log in! |
| 489 | logging.error("Enable password failure: %s\n", self.connection.match) |
| 490 | raise IOError |
| 491 | return 0 |
| 492 | |
| 493 | def _logout(self): |
| 494 | logging.debug("Logging out") |
| 495 | self._cli("quit", False) |
Steve McIntyre | dfce73f | 2015-07-09 17:41:55 +0100 | [diff] [blame] | 496 | try: |
| 497 | self.connection.expect("Would you like to save them now") |
| 498 | self._cli("n") |
| 499 | except (pexpect.EOF): |
| 500 | pass |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 501 | |
| 502 | def _configure(self): |
| 503 | self._cli("configure") |
| 504 | |
| 505 | def _end_configure(self): |
| 506 | self._cli("exit") |
| 507 | |
Steve McIntyre | fc7a627 | 2015-07-07 16:22:32 +0100 | [diff] [blame] | 508 | def _end_interface(self): |
| 509 | self._end_configure() |
| 510 | |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 511 | def _read_long_output(self, text): |
| 512 | longbuf = [] |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 513 | prompt = self._prompt_name + r'\s*#' |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 514 | while True: |
| 515 | try: |
| 516 | index = self.connection.expect(['--More--', prompt]) |
| 517 | if index == 0: # "--More-- or (q)uit" |
| 518 | for line in self.connection.before.split('\r\n'): |
| 519 | line1 = re.sub('(\x08|\x0D)*', '', line.strip()) |
| 520 | longbuf.append(line1) |
| 521 | self._cli(' ', False) |
| 522 | elif index == 1: # Back to a prompt, says output is finished |
| 523 | break |
| 524 | except (pexpect.EOF, pexpect.TIMEOUT): |
| 525 | # Something went wrong; logout, log in and try again! |
| 526 | logging.error("PEXPECT FAILURE, RECONNECT") |
| 527 | self.errors.log_error_in(text) |
| 528 | raise PExpectError("_read_long_output failed") |
| 529 | except: |
| 530 | logging.error("prompt is \"%s\"", prompt) |
| 531 | raise |
| 532 | |
| 533 | for line in self.connection.before.split('\r\n'): |
| 534 | longbuf.append(line.strip()) |
| 535 | return longbuf |
| 536 | |
| 537 | def _get_port_names(self): |
| 538 | logging.debug("Grabbing list of ports") |
| 539 | interfaces = [] |
| 540 | |
| 541 | # Use "Up" or "Down" to only identify lines in the output that |
| 542 | # match interfaces that exist |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 543 | regex = re.compile(r'^(1\S+)') |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 544 | |
| 545 | try: |
| 546 | self._cli("show port all") |
| 547 | for line in self._read_long_output("show port all"): |
| 548 | match = regex.match(line) |
| 549 | if match: |
| 550 | interface = match.group(1) |
| 551 | interfaces.append(interface) |
Steve McIntyre | d601ab8 | 2015-07-09 17:42:36 +0100 | [diff] [blame] | 552 | logging.debug(" found %d ports on the switch", len(interfaces)) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 553 | return interfaces |
| 554 | |
| 555 | except PExpectError: |
| 556 | # recurse on error |
| 557 | self._switch_connect() |
| 558 | return self._get_port_names() |
| 559 | |
Steve McIntyre | 1a7bad5 | 2015-07-20 15:21:42 +0100 | [diff] [blame] | 560 | # Get the mode of a port: access or trunk |
| 561 | def _port_get_mode(self, port): |
| 562 | logging.debug("Getting mode of port %s", port) |
| 563 | |
| 564 | if not self._is_port_name_valid(port): |
| 565 | raise InputError("Port name %s not recognised" % port) |
| 566 | acceptframe_re = re.compile('vlan acceptframe (.*)') |
| 567 | ingress_re = re.compile('vlan ingressfilter') |
| 568 | |
| 569 | acceptframe = None |
| 570 | ingressfilter = True |
| 571 | |
| 572 | try: |
| 573 | self._cli("show running-config interface %s" % port) |
| 574 | for line in self._read_long_output("show running-config interface"): |
| 575 | |
| 576 | match = acceptframe_re.match(line) |
| 577 | if match: |
| 578 | acceptframe = match.group(1) |
| 579 | |
| 580 | match = ingress_re.match(line) |
| 581 | if match: |
| 582 | ingressfilter = True |
| 583 | |
| 584 | # Simple classifier for now; may need to revisit later... |
| 585 | if (ingressfilter and acceptframe == "admituntaggedonly"): |
| 586 | return "access" |
| 587 | else: |
| 588 | return "trunk" |
| 589 | |
| 590 | except PExpectError: |
| 591 | # recurse on error |
| 592 | self._switch_connect() |
| 593 | return self.port_get_mode(port) |
| 594 | |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 595 | def _show_config(self): |
| 596 | logging.debug("Grabbing config") |
| 597 | try: |
| 598 | self._cli("show running-config") |
| 599 | return self._read_long_output("show running-config") |
| 600 | except PExpectError: |
| 601 | # recurse on error |
| 602 | self._switch_connect() |
| 603 | return self._show_config() |
| 604 | |
| 605 | def _show_clock(self): |
| 606 | logging.debug("Grabbing time") |
| 607 | try: |
| 608 | self._cli("show clock") |
| 609 | return self._read_long_output("show clock") |
| 610 | except PExpectError: |
| 611 | # recurse on error |
| 612 | self._switch_connect() |
| 613 | return self._show_clock() |
| 614 | |
| 615 | def _get_systemdata(self): |
| 616 | logging.debug("Grabbing system sw and hw versions") |
| 617 | |
| 618 | try: |
| 619 | self._systemdata = [] |
| 620 | self._cli("show version") |
| 621 | for line in self._read_long_output("show version"): |
| 622 | self._systemdata.append(line) |
| 623 | |
| 624 | except PExpectError: |
| 625 | # recurse on error |
| 626 | self._switch_connect() |
| 627 | return self._get_systemdata() |
| 628 | |
| 629 | def _parse_vlan_list(self, inputdata): |
| 630 | vlans = [] |
| 631 | |
| 632 | if inputdata == "ALL": |
| 633 | return ["ALL"] |
| 634 | elif inputdata == "NONE": |
| 635 | return [] |
| 636 | else: |
| 637 | # Parse the complex list |
| 638 | groups = inputdata.split(',') |
| 639 | for group in groups: |
| 640 | subgroups = group.split('-') |
| 641 | if len(subgroups) == 1: |
| 642 | vlans.append(int(subgroups[0])) |
| 643 | elif len(subgroups) == 2: |
| 644 | for i in range (int(subgroups[0]), int(subgroups[1]) + 1): |
| 645 | vlans.append(i) |
| 646 | else: |
| 647 | logging.debug("Can't parse group \"" + group + "\"") |
| 648 | |
| 649 | return vlans |
| 650 | |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 651 | def _get_port_vlans(self, port): |
| 652 | vlan_text = None |
| 653 | |
Steve McIntyre | c08cca2 | 2015-07-07 18:00:33 +0100 | [diff] [blame] | 654 | vlan_part_re = re.compile('vlan participation include (.*)') |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 655 | |
| 656 | try: |
| 657 | self._cli("show running-config interface %s" % port) |
| 658 | for line in self._read_long_output("show running-config interface"): |
| 659 | match = vlan_part_re.match(line) |
| 660 | if match: |
| 661 | if vlan_text != None: |
| 662 | vlan_text += "," |
| 663 | vlan_text += (match.group(1)) |
| 664 | else: |
| 665 | vlan_text = match.group(1) |
Steve McIntyre | 8009ffe | 2015-07-07 18:02:21 +0100 | [diff] [blame] | 666 | |
| 667 | if vlan_text is None: |
| 668 | return [1] |
| 669 | else: |
| 670 | vlans = self._parse_vlan_list(vlan_text) |
| 671 | return vlans |
Steve McIntyre | ef73fca | 2015-07-07 14:57:59 +0100 | [diff] [blame] | 672 | |
| 673 | except PExpectError: |
| 674 | # recurse on error |
| 675 | self._switch_connect() |
| 676 | return self._get_port_vlans(port) |
| 677 | |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 678 | # Wrapper around connection.send - by default, expect() the same |
| 679 | # text we've sent, to remove it from the output from the |
| 680 | # switch. For the few cases where we don't need that, override |
| 681 | # this using echo=False. |
| 682 | # Horrible, but seems to work. |
| 683 | def _cli(self, text, echo=True): |
| 684 | self.connection.send(text + '\r') |
| 685 | if echo: |
| 686 | try: |
| 687 | self.connection.expect(text) |
| 688 | except (pexpect.EOF, pexpect.TIMEOUT): |
| 689 | # Something went wrong; logout, log in and try again! |
| 690 | logging.error("PEXPECT FAILURE, RECONNECT") |
| 691 | self.errors.log_error_out(text) |
| 692 | raise PExpectError("_cli failed on %s" % text) |
| 693 | except: |
| 694 | logging.error("Unexpected error: %s", sys.exc_info()[0]) |
| 695 | raise |
| 696 | |
| 697 | if __name__ == "__main__": |
| 698 | |
| 699 | import optparse |
| 700 | |
| 701 | switch = 'vlandswitch05' |
| 702 | parser = optparse.OptionParser() |
| 703 | parser.add_option("--switch", |
| 704 | dest = "switch", |
| 705 | action = "store", |
| 706 | nargs = 1, |
| 707 | type = "string", |
| 708 | help = "specify switch to connect to for testing", |
| 709 | metavar = "<switch>") |
| 710 | (opts, args) = parser.parse_args() |
| 711 | if opts.switch: |
| 712 | switch = opts.switch |
| 713 | |
Steve McIntyre | 144d51c | 2015-07-07 17:56:30 +0100 | [diff] [blame] | 714 | logging.basicConfig(level = logging.DEBUG, |
| 715 | format = '%(asctime)s %(levelname)-8s %(message)s') |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 716 | p = NetgearXSM(switch, 23, debug=True) |
| 717 | p.switch_connect('admin', '', None) |
| 718 | |
| 719 | print "VLANs are:" |
| 720 | buf = p.vlan_get_list() |
| 721 | p.dump_list(buf) |
| 722 | |
| 723 | buf = p.vlan_get_name(2) |
| 724 | print "VLAN 2 is named \"%s\"" % buf |
| 725 | |
| 726 | print "Create VLAN 3" |
| 727 | p.vlan_create(3) |
| 728 | |
| 729 | buf = p.vlan_get_name(3) |
| 730 | print "VLAN 3 is named \"%s\"" % buf |
| 731 | |
| 732 | print "Set name of VLAN 3 to test333" |
| 733 | p.vlan_set_name(3, "test333") |
| 734 | |
| 735 | buf = p.vlan_get_name(3) |
| 736 | print "VLAN 3 is named \"%s\"" % buf |
| 737 | |
| 738 | print "VLANs are:" |
| 739 | buf = p.vlan_get_list() |
| 740 | p.dump_list(buf) |
| 741 | |
| 742 | print "Destroy VLAN 3" |
| 743 | p.vlan_destroy(3) |
| 744 | |
| 745 | print "VLANs are:" |
| 746 | buf = p.vlan_get_list() |
| 747 | p.dump_list(buf) |
| 748 | |
| 749 | buf = p.port_get_mode("1/0/10") |
| 750 | print "Port 1/0/10 is in %s mode" % buf |
| 751 | |
| 752 | buf = p.port_get_mode("1/0/11") |
| 753 | print "Port 1/0/11 is in %s mode" % buf |
| 754 | |
| 755 | # Test access stuff |
| 756 | print "Set 1/0/9 to access mode" |
| 757 | p.port_set_mode("1/0/9", "access") |
| 758 | |
| 759 | print "Move 1/0/9 to VLAN 4" |
| 760 | p.port_set_access_vlan("1/0/9", 4) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 761 | |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 762 | buf = p.port_get_access_vlan("1/0/9") |
| 763 | print "Read from switch: 1/0/9 is on VLAN %s" % buf |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 764 | |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 765 | print "Move 1/0/9 back to VLAN 1" |
| 766 | p.port_set_access_vlan("1/0/9", 1) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 767 | |
Steve McIntyre | 40484f1 | 2015-07-07 18:02:50 +0100 | [diff] [blame] | 768 | print "Create VLAN 2" |
Steve McIntyre | 3a1b483 | 2015-07-08 15:42:09 +0100 | [diff] [blame] | 769 | p.vlan_create(2) |
Steve McIntyre | 40484f1 | 2015-07-07 18:02:50 +0100 | [diff] [blame] | 770 | |
| 771 | print "Create VLAN 3" |
| 772 | p.vlan_create(3) |
| 773 | |
| 774 | print "Create VLAN 4" |
| 775 | p.vlan_create(4) |
| 776 | |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 777 | # Test access stuff |
| 778 | print "Set 1/0/9 to trunk mode" |
| 779 | p.port_set_mode("1/0/9", "trunk") |
| 780 | print "Read from switch: which VLANs is 1/0/9 on?" |
| 781 | buf = p.port_get_trunk_vlan_list("1/0/9") |
| 782 | p.dump_list(buf) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 783 | |
Steve McIntyre | 12b9ef9 | 2015-07-08 15:48:44 +0100 | [diff] [blame] | 784 | # The adds below are NOOPs in effect on this switch - no filtering |
| 785 | # for "trunk" ports |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 786 | print "Add 1/0/9 to VLAN 2" |
| 787 | p.port_add_trunk_to_vlan("1/0/9", 2) |
| 788 | print "Add 1/0/9 to VLAN 3" |
| 789 | p.port_add_trunk_to_vlan("1/0/9", 3) |
| 790 | print "Add 1/0/9 to VLAN 4" |
| 791 | p.port_add_trunk_to_vlan("1/0/9", 4) |
| 792 | print "Read from switch: which VLANs is 1/0/9 on?" |
| 793 | buf = p.port_get_trunk_vlan_list("1/0/9") |
| 794 | p.dump_list(buf) |
| 795 | |
Steve McIntyre | 12b9ef9 | 2015-07-08 15:48:44 +0100 | [diff] [blame] | 796 | # And the same for removals here |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 797 | p.port_remove_trunk_from_vlan("1/0/9", 3) |
Steve McIntyre | 6067138 | 2015-07-08 15:49:03 +0100 | [diff] [blame] | 798 | p.port_remove_trunk_from_vlan("1/0/9", 2) |
Steve McIntyre | fdcb378 | 2015-07-06 16:34:56 +0100 | [diff] [blame] | 799 | p.port_remove_trunk_from_vlan("1/0/9", 4) |
| 800 | print "Read from switch: which VLANs is 1/0/9 on?" |
| 801 | buf = p.port_get_trunk_vlan_list("1/0/9") |
| 802 | p.dump_list(buf) |
| 803 | |
| 804 | # print 'Restarting switch, to explicitly reset config' |
| 805 | # p.switch_restart() |
| 806 | |
| 807 | # p.switch_save_running_config() |
| 808 | |
| 809 | # p.switch_disconnect() |
| 810 | # p._show_config() |