Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
Steve McIntyre | 94ef65e | 2015-09-25 01:08:14 +0100 | [diff] [blame] | 3 | # Copyright 2014-2015 Linaro Limited |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 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 |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 21 | import sys |
| 22 | import re |
Steve McIntyre | 4267c6a | 2018-01-24 16:57:28 +0000 | [diff] [blame] | 23 | import pexpect |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 24 | |
| 25 | if __name__ == '__main__': |
| 26 | import os |
| 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 | |
| 31 | from errors import InputError, PExpectError |
| 32 | from drivers.common import SwitchDriver, SwitchErrors |
| 33 | |
| 34 | class TPLinkTLSG2XXX(SwitchDriver): |
| 35 | |
| 36 | connection = None |
| 37 | _username = None |
| 38 | _password = None |
| 39 | _enable_password = None |
| 40 | |
| 41 | _capabilities = [ |
| 42 | ] |
| 43 | |
| 44 | # Regexp of expected hardware information - fail if we don't see |
| 45 | # this |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 46 | _expected_descr_re = re.compile(r'TL-SG2\d\d\d') |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 47 | |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 48 | def __init__(self, switch_hostname, switch_telnetport=23, debug=False): |
Steve McIntyre | bb58a27 | 2015-07-14 15:39:54 +0100 | [diff] [blame] | 49 | SwitchDriver.__init__(self, switch_hostname, debug) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 50 | self._systemdata = [] |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 51 | self.exec_string = "/usr/bin/telnet %s %d" % (switch_hostname, switch_telnetport) |
| 52 | self.errors = SwitchErrors() |
| 53 | |
| 54 | ################################ |
| 55 | ### Switch-level API functions |
| 56 | ################################ |
| 57 | |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 58 | # Save the current running config into flash - we want config to |
| 59 | # remain across reboots |
| 60 | def switch_save_running_config(self): |
| 61 | try: |
| 62 | self._cli("copy running-config startup-config") |
| 63 | self.connection.expect("OK") |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 64 | except (PExpectError, pexpect.EOF): |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 65 | # recurse on error |
| 66 | self._switch_connect() |
| 67 | self.switch_save_running_config() |
| 68 | |
| 69 | # Restart the switch - we need to reload config to do a |
| 70 | # roll-back. Do NOT save running-config first if the switch asks - |
| 71 | # we're trying to dump recent changes, not save them. |
| 72 | # |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 73 | # This will also implicitly cause a connection to be closed |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 74 | def switch_restart(self): |
| 75 | self._cli("reboot") |
| 76 | index = self.connection.expect(['Daving current', 'Continue?']) |
| 77 | if index == 0: |
| 78 | self._cli("n") # No, don't save |
| 79 | self.connection.expect("Continue?") |
| 80 | |
| 81 | # Fall through |
| 82 | self._cli("y") # Yes, continue to reset |
| 83 | self.connection.close(True) |
| 84 | |
| 85 | # List the capabilities of the switch (and driver) - some things |
| 86 | # make no sense to abstract. Returns a dict of strings, each one |
| 87 | # describing an extra feature that that higher levels may care |
| 88 | # about |
| 89 | def switch_get_capabilities(self): |
| 90 | return self._capabilities |
| 91 | |
| 92 | ################################ |
| 93 | ### VLAN API functions |
| 94 | ################################ |
| 95 | |
| 96 | # Create a VLAN with the specified tag |
| 97 | def vlan_create(self, tag): |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 98 | logging.debug("Creating VLAN %d", tag) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 99 | |
| 100 | try: |
| 101 | self._configure() |
| 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): |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 119 | logging.debug("Destroying VLAN %d", tag) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 120 | |
| 121 | try: |
| 122 | self._configure() |
| 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): |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 139 | logging.debug("Setting name of VLAN %d to %s", tag, name) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 140 | |
| 141 | try: |
| 142 | self._configure() |
| 143 | self._cli("vlan %d" % tag) |
| 144 | self._cli("name %s" % name) |
| 145 | self._end_configure() |
| 146 | |
| 147 | # Validate it happened |
| 148 | read_name = self.vlan_get_name(tag) |
| 149 | if read_name != name: |
| 150 | raise IOError("Failed to set name for VLAN %d (name found is \"%s\", not \"%s\")" |
| 151 | % (tag, read_name, name)) |
| 152 | |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 153 | except (PExpectError, pexpect.EOF): |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 154 | # recurse on error |
| 155 | self._switch_connect() |
| 156 | self.vlan_set_name(tag, name) |
| 157 | |
| 158 | # Get a list of the VLAN tags currently registered on the switch |
| 159 | def vlan_get_list(self): |
| 160 | logging.debug("Grabbing list of VLANs") |
| 161 | |
| 162 | try: |
| 163 | vlans = [] |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 164 | regex = re.compile(r'^ *(\d+).*active') |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 165 | |
| 166 | self._cli("show vlan brief") |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 167 | for line in self._read_long_output("show vlan brief"): |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 168 | match = regex.match(line) |
| 169 | if match: |
| 170 | vlans.append(int(match.group(1))) |
| 171 | return vlans |
| 172 | |
| 173 | except PExpectError: |
| 174 | # recurse on error |
| 175 | self._switch_connect() |
| 176 | return self.vlan_get_list() |
| 177 | |
| 178 | # For a given VLAN tag, ask the switch what the associated name is |
| 179 | def vlan_get_name(self, tag): |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 180 | logging.debug("Grabbing the name of VLAN %d", tag) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 181 | |
| 182 | try: |
| 183 | name = None |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 184 | regex = re.compile(r'^ *\d+\s+(\S+).*(active)') |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 185 | self._cli("show vlan id %d" % tag) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 186 | for line in self._read_long_output("show vlan id"): |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 187 | match = regex.match(line) |
| 188 | if match: |
| 189 | name = match.group(1) |
| 190 | name.strip() |
| 191 | return name |
| 192 | |
| 193 | except PExpectError: |
| 194 | # recurse on error |
| 195 | self._switch_connect() |
| 196 | return self.vlan_get_name(tag) |
| 197 | |
| 198 | ################################ |
| 199 | ### Port API functions |
| 200 | ################################ |
| 201 | |
| 202 | # Set the mode of a port: access or trunk |
| 203 | def port_set_mode(self, port, mode): |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 204 | logging.debug("Setting port %s to %s", port, mode) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 205 | if not self._is_port_mode_valid(mode): |
| 206 | raise IndexError("Port mode %s is not allowed" % mode) |
| 207 | if not self._is_port_name_valid(port): |
| 208 | raise IndexError("Port name %s not recognised" % port) |
| 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 | |
| 213 | try: |
| 214 | # We define a trunk to be on *all* VLANs on the switch in |
| 215 | # tagged mode, and PVID should match the default VLAN (1). |
| 216 | if mode == "trunk": |
| 217 | # Disconnect all the untagged ports |
| 218 | read_vlans = self._port_get_all_vlans(port, 'Untagged') |
| 219 | for vlan in read_vlans: |
| 220 | self._port_remove_general_vlan(port, vlan) |
| 221 | |
| 222 | # And move to VLAN 1 |
| 223 | self.port_add_trunk_to_vlan(port, 1) |
| 224 | self._set_pvid(port, 1) |
| 225 | |
| 226 | # And an access port should only be on one VLAN. Move to |
| 227 | # VLAN 1, untagged, and set PVID there. |
| 228 | if mode == "access": |
| 229 | # Disconnect all the ports |
| 230 | read_vlans = self._port_get_all_vlans(port, 'Untagged') |
| 231 | for vlan in read_vlans: |
| 232 | self._port_remove_general_vlan(port, vlan) |
| 233 | read_vlans = self._port_get_all_vlans(port, 'Tagged') |
| 234 | for vlan in read_vlans: |
| 235 | self._port_remove_general_vlan(port, vlan) |
| 236 | |
| 237 | # And move to VLAN 1 |
| 238 | self.port_set_access_vlan(port, 1) |
| 239 | self._set_pvid(port, 1) |
| 240 | |
Steve McIntyre | b79cea5 | 2015-07-24 17:03:31 +0100 | [diff] [blame] | 241 | # Validate it happened |
| 242 | read_mode = self._port_get_mode(port) |
| 243 | if read_mode != mode: |
| 244 | raise IOError("Failed to set mode for port %s" % port) |
| 245 | |
Steve McIntyre | 1a7bad5 | 2015-07-20 15:21:42 +0100 | [diff] [blame] | 246 | # And cache the result |
| 247 | self._port_modes[port] = mode |
| 248 | |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 249 | except (PExpectError, pexpect.EOF): |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 250 | # recurse on error |
| 251 | self._switch_connect() |
| 252 | self.port_set_mode(port, mode) |
| 253 | |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 254 | # Set an access port to be in a specified VLAN (tag) |
| 255 | def port_set_access_vlan(self, port, tag): |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 256 | logging.debug("Setting access port %s to VLAN %d", port, tag) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 257 | if not self._is_port_name_valid(port): |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 258 | raise IndexError("Port name %s not recognised" % port) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 259 | # Does the VLAN already exist? |
| 260 | vlan_list = self.vlan_get_list() |
| 261 | if not tag in vlan_list: |
| 262 | raise IndexError("VLAN tag %d not recognised" % tag) |
| 263 | |
| 264 | try: |
| 265 | # Add the new VLAN |
| 266 | self._configure() |
| 267 | self._cli("interface %s" % self._long_port_name(port)) |
| 268 | self._cli("switchport general allowed vlan %d untagged" % tag) |
| 269 | self._cli("no shutdown") |
| 270 | self._end_configure() |
| 271 | |
| 272 | self._set_pvid(port, tag) |
| 273 | |
| 274 | # Now drop all the other VLANs |
| 275 | read_vlans = self._port_get_all_vlans(port, 'Untagged') |
| 276 | for vlan in read_vlans: |
| 277 | if vlan != tag: |
| 278 | self._port_remove_general_vlan(port, vlan) |
| 279 | |
| 280 | # Finally, validate things worked |
| 281 | read_vlan = int(self.port_get_access_vlan(port)) |
| 282 | if read_vlan != tag: |
| 283 | raise IOError("Failed to move access port %s to VLAN %d - got VLAN %d instead" |
| 284 | % (port, tag, read_vlan)) |
| 285 | |
| 286 | except PExpectError: |
| 287 | # recurse on error |
| 288 | self._switch_connect() |
| 289 | self.port_set_access_vlan(port, tag) |
| 290 | |
| 291 | # Add a trunk port to a specified VLAN (tag) |
| 292 | def port_add_trunk_to_vlan(self, port, tag): |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 293 | logging.debug("Adding trunk port %s to VLAN %d", port, tag) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 294 | if not self._is_port_name_valid(port): |
| 295 | raise IndexError("Port name %s not recognised" % port) |
| 296 | try: |
| 297 | self._configure() |
| 298 | self._cli("interface %s" % self._long_port_name(port)) |
| 299 | self._cli("switchport general allowed vlan %d tagged" % tag) |
| 300 | self._end_configure() |
| 301 | |
| 302 | # Validate it happened |
| 303 | read_vlans = self.port_get_trunk_vlan_list(port) |
| 304 | for vlan in read_vlans: |
| 305 | if vlan == tag or vlan == "ALL": |
| 306 | return |
| 307 | raise IOError("Failed to add trunk port %s to VLAN %d" % (port, tag)) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 308 | |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 309 | except PExpectError: |
| 310 | # recurse on error |
| 311 | self._switch_connect() |
| 312 | self.port_add_trunk_to_vlan(port, tag) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 313 | |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 314 | # Remove a trunk port from a specified VLAN (tag) |
| 315 | def port_remove_trunk_from_vlan(self, port, tag): |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 316 | logging.debug("Removing trunk port %s from VLAN %d", port, tag) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 317 | if not self._is_port_name_valid(port): |
| 318 | raise IndexError("Port name %s not recognised" % port) |
| 319 | |
| 320 | try: |
| 321 | self._configure() |
| 322 | self._cli("interface %s" % self._long_port_name(port)) |
| 323 | self._cli("no switchport general allowed vlan %d" % tag) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 324 | self._end_configure() |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 325 | |
| 326 | # Validate it happened |
| 327 | read_vlans = self.port_get_trunk_vlan_list(port) |
| 328 | for vlan in read_vlans: |
| 329 | if vlan == tag: |
| 330 | raise IOError("Failed to remove trunk port %s from VLAN %d" % (port, tag)) |
| 331 | |
| 332 | except PExpectError: |
| 333 | # recurse on error |
| 334 | self._switch_connect() |
| 335 | self.port_remove_trunk_from_vlan(port, tag) |
| 336 | |
| 337 | # Get the configured VLAN tag for an access port (i.e. a port not |
| 338 | # configured for tagged egress) |
| 339 | def port_get_access_vlan(self, port): |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 340 | logging.debug("Getting VLAN for access port %s", port) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 341 | vlan = 1 |
| 342 | if not self._is_port_name_valid(port): |
| 343 | raise IndexError("Port name %s not recognised" % port) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 344 | regex = re.compile(r'(\d+)\s+.*Untagged') |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 345 | |
| 346 | try: |
| 347 | self._cli("show interface switchport %s" % self._long_port_name(port)) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 348 | for line in self._read_long_output("show interface switchport"): |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [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) |
| 358 | |
| 359 | # Get the list of configured VLAN tags for a trunk port |
| 360 | def port_get_trunk_vlan_list(self, port): |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 361 | logging.debug("Getting VLANs for trunk port %s", port) |
| 362 | |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 363 | if not self._is_port_name_valid(port): |
| 364 | raise IndexError("Port name %s not recognised" % port) |
| 365 | |
| 366 | return self._port_get_all_vlans(port, 'Tagged') |
| 367 | |
| 368 | ################################ |
| 369 | ### Internal functions |
| 370 | ################################ |
| 371 | |
| 372 | # Connect to the switch and log in |
| 373 | def _switch_connect(self): |
| 374 | |
| 375 | if not self.connection is None: |
| 376 | self.connection.close(True) |
| 377 | self.connection = None |
| 378 | |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 379 | logging.debug("Connecting to Switch with: %s", self.exec_string) |
Steve McIntyre | 06c644a | 2015-07-17 17:07:41 +0100 | [diff] [blame] | 380 | self.connection = pexpect.spawn(self.exec_string, logfile=self.logger) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 381 | self._login() |
| 382 | |
| 383 | # No way to avoid paged output on this switch AFAICS |
| 384 | |
| 385 | # And grab details about the switch. in case we need it |
| 386 | self._get_systemdata() |
| 387 | |
| 388 | # And also validate them - make sure we're driving a switch of |
| 389 | # the correct model! Also store the serial number |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 390 | descr_regex = re.compile(r'Hardware Version\s+ - (.*)') |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 391 | descr = "" |
| 392 | |
| 393 | for line in self._systemdata: |
| 394 | match = descr_regex.match(line) |
| 395 | if match: |
| 396 | descr = match.group(1) |
| 397 | |
| 398 | logging.debug("system description is %s", descr) |
| 399 | |
| 400 | if not self._expected_descr_re.match(descr): |
| 401 | raise IOError("Switch %s not recognised by this driver: abort" % descr) |
| 402 | |
| 403 | # Now build a list of our ports, for later sanity checking |
| 404 | self._ports = self._get_port_names() |
| 405 | if len(self._ports) < 4: |
| 406 | raise IOError("Not enough ports detected - problem!") |
| 407 | |
| 408 | def _login(self): |
| 409 | logging.debug("attempting login with username \"%s\", password \"%s\", enable_password \"%s\"", self._username, self._password, self._enable_password) |
| 410 | self.connection.expect('User Access Login') |
| 411 | if self._username is not None: |
| 412 | self.connection.expect("User:") |
| 413 | self._cli("%s" % self._username) |
| 414 | if self._password is not None: |
| 415 | self.connection.expect("Password:") |
| 416 | self._cli("%s" % self._password, False) |
| 417 | while True: |
| 418 | index = self.connection.expect(['User Name:', 'Password:', 'Bad passwords', 'authentication failed', r'(.*)(#|>)']) |
| 419 | if index != 4: # Any other means: failed to log in! |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 420 | logging.error("Login failure: index %d\n", index) |
| 421 | logging.error("Login failure: %s\n", self.connection.match.before) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 422 | raise IOError |
| 423 | |
| 424 | # else |
Steve McIntyre | 65dfe7f | 2015-06-09 15:57:02 +0100 | [diff] [blame] | 425 | self._prompt_name = re.escape(self.connection.match.group(1).strip()) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 426 | if self.connection.match.group(2) == ">": |
| 427 | # Need to enter "enable" mode too |
| 428 | self._cli("") |
| 429 | self._cli("enable") |
| 430 | if self._enable_password is not None and len(self._enable_password) > 0: |
| 431 | self.connection.expect("Password:") |
| 432 | self._cli("%s" % self._enable_password, False) |
| 433 | index = self.connection.expect(['Password:', 'Bad passwords', 'authentication failed', r'(.*)(#|>)']) |
| 434 | if index != 3: # Any other means: failed to log in! |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 435 | logging.error("Enable password failure: %s\n", self.connection.match) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 436 | raise IOError |
| 437 | return 0 |
| 438 | |
| 439 | def _logout(self): |
| 440 | logging.debug("Logging out") |
| 441 | self._cli("exit", False) |
Steve McIntyre | 26c8956 | 2015-10-09 15:02:37 +0100 | [diff] [blame] | 442 | self.connection.close(True) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 443 | |
| 444 | def _configure(self): |
| 445 | self._cli("configure") |
| 446 | |
| 447 | def _end_configure(self): |
| 448 | self._cli("end") |
| 449 | |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 450 | def _read_long_output(self, text): |
| 451 | longbuf = [] |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 452 | prompt = self._prompt_name + '#' |
| 453 | while True: |
| 454 | try: |
| 455 | index = self.connection.expect(['^Press any key to continue', prompt]) |
| 456 | if index == 0: # "Press any key to continue (Q to quit)" |
| 457 | for line in self.connection.before.split('\r\n'): |
| 458 | line1 = re.sub('(\x08|\x0D)*', '', line.strip()) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 459 | longbuf.append(line1) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 460 | self._cli(' ', False) |
| 461 | elif index == 1: # Back to a prompt, says output is finished |
| 462 | break |
| 463 | except (pexpect.EOF, pexpect.TIMEOUT): |
| 464 | # Something went wrong; logout, log in and try again! |
| 465 | logging.error("PEXPECT FAILURE, RECONNECT") |
| 466 | self.errors.log_error_in(text) |
| 467 | raise PExpectError("_read_long_output failed") |
| 468 | except: |
| 469 | logging.error("prompt is \"%s\"", prompt) |
| 470 | raise |
| 471 | |
| 472 | for line in self.connection.before.split('\r\n'): |
| 473 | line1 = re.sub('(\x08|\x0D)*', '', line.strip()) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 474 | longbuf.append(line1) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 475 | |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 476 | return longbuf |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 477 | |
| 478 | def _long_port_name(self, port): |
| 479 | return re.sub('Gi', 'gigabitEthernet ', port) |
| 480 | |
| 481 | def _set_pvid(self, port, pvid): |
| 482 | # Set a port's PVID |
| 483 | self._configure() |
| 484 | self._cli("interface %s" % self._long_port_name(port)) |
| 485 | self._cli("switchport pvid %d" % pvid) |
| 486 | self._end_configure() |
| 487 | |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 488 | def _port_get_all_vlans(self, port, port_type): |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 489 | vlans = [] |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 490 | regex = re.compile(r'(\d+)\s+.*' + port_type) |
Steve McIntyre | aead233 | 2015-08-05 13:45:51 +0100 | [diff] [blame] | 491 | |
| 492 | try: |
| 493 | self._cli("show interface switchport %s" % self._long_port_name(port)) |
| 494 | for line in self._read_long_output("show interface switchport"): |
| 495 | match = regex.match(line) |
| 496 | if match: |
| 497 | vlan = match.group(1) |
| 498 | vlans.append(int(vlan)) |
| 499 | return vlans |
| 500 | |
| 501 | except PExpectError: |
| 502 | # recurse on error |
| 503 | self._switch_connect() |
| 504 | return self._port_get_all_vlans(port, port_type) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 505 | |
| 506 | def _port_remove_general_vlan(self, port, tag): |
| 507 | try: |
| 508 | self._configure() |
| 509 | self._cli("interface %s" % self._long_port_name(port)) |
| 510 | self._cli("no switchport general allowed vlan %d" % tag) |
| 511 | self._end_configure() |
| 512 | |
| 513 | except PExpectError: |
| 514 | # recurse on error |
| 515 | self._switch_connect() |
| 516 | return self._port_remove_general_vlan(port, tag) |
| 517 | |
| 518 | def _get_port_names(self): |
| 519 | logging.debug("Grabbing list of ports") |
| 520 | interfaces = [] |
| 521 | |
| 522 | # Use "Link" to only identify lines in the output that match |
| 523 | # interfaces that exist - it'll match "LinkUp" and "LinkDown" |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 524 | regex = re.compile(r'^\s*([a-zA-Z0-9_/]*).*Link') |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 525 | |
| 526 | try: |
| 527 | self._cli("show interface status") |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 528 | for line in self._read_long_output("show interface status"): |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 529 | match = regex.match(line) |
| 530 | if match: |
| 531 | interface = match.group(1) |
| 532 | interfaces.append(interface) |
Steve McIntyre | d5cfde1 | 2015-08-05 13:49:42 +0100 | [diff] [blame] | 533 | self._port_numbers[interface] = len(interfaces) |
Steve McIntyre | d601ab8 | 2015-07-09 17:42:36 +0100 | [diff] [blame] | 534 | logging.debug(" found %d ports on the switch", len(interfaces)) |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 535 | return interfaces |
| 536 | |
| 537 | except PExpectError: |
| 538 | # recurse on error |
| 539 | self._switch_connect() |
| 540 | return self._get_port_names() |
| 541 | |
Steve McIntyre | 1a7bad5 | 2015-07-20 15:21:42 +0100 | [diff] [blame] | 542 | # Get the mode of a port: access or trunk |
| 543 | def _port_get_mode(self, port): |
| 544 | logging.debug("Getting mode of port %s", port) |
| 545 | |
| 546 | if not self._is_port_name_valid(port): |
| 547 | raise IndexError("Port name %s not recognised" % port) |
| 548 | |
| 549 | # This switch does not support specific modes, so we have to |
| 550 | # make stuff up here. We define trunk ports to be on (1 or |
| 551 | # many) tagged VLANs, anything not tagged to be access. |
| 552 | read_vlans = self._port_get_all_vlans(port, 'Tagged') |
| 553 | if len(read_vlans) > 0: |
| 554 | return "trunk" |
| 555 | else: |
| 556 | return "access" |
| 557 | |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 558 | def _show_config(self): |
| 559 | logging.debug("Grabbing config") |
| 560 | self._cli("show running-config") |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 561 | return self._read_long_output("show running-config") |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 562 | |
| 563 | def _show_clock(self): |
| 564 | logging.debug("Grabbing time") |
| 565 | self._cli("show system-time") |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 566 | return self._read_long_output("show system-time") |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 567 | |
| 568 | def _get_systemdata(self): |
| 569 | logging.debug("Grabbing system sw and hw versions") |
| 570 | |
| 571 | self._cli("show system-info") |
| 572 | self._systemdata = [] |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 573 | for line in self._read_long_output("show system-info"): |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 574 | self._systemdata.append(line) |
| 575 | |
| 576 | # Wrapper around connection.send - by default, expect() the same |
| 577 | # text we've sent, to remove it from the output from the |
| 578 | # switch. For the few cases where we don't need that, override |
| 579 | # this using echo=False. |
| 580 | # Horrible, but seems to work. |
| 581 | def _cli(self, text, echo=True): |
| 582 | self.connection.send(text + '\r') |
| 583 | if echo: |
| 584 | self.connection.expect(text) |
| 585 | |
| 586 | if __name__ == "__main__": |
Steve McIntyre | da9e9c1 | 2018-01-24 16:58:13 +0000 | [diff] [blame] | 587 | |
| 588 | # Simple test harness - exercise the main working functions above to verify |
| 589 | # they work. This does *NOT* test really disruptive things like "save |
| 590 | # running-config" and "reload" - test those by hand. |
| 591 | |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 592 | import optparse |
| 593 | |
| 594 | switch = '10.172.2.50' |
| 595 | parser = optparse.OptionParser() |
| 596 | parser.add_option("--switch", |
| 597 | dest = "switch", |
| 598 | action = "store", |
| 599 | nargs = 1, |
| 600 | type = "string", |
| 601 | help = "specify switch to connect to for testing", |
| 602 | metavar = "<switch>") |
| 603 | (opts, args) = parser.parse_args() |
| 604 | if opts.switch: |
| 605 | switch = opts.switch |
| 606 | |
Steve McIntyre | 144d51c | 2015-07-07 17:56:30 +0100 | [diff] [blame] | 607 | logging.basicConfig(level = logging.DEBUG, |
| 608 | format = '%(asctime)s %(levelname)-8s %(message)s') |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 609 | p = TPLinkTLSG2XXX(switch, 23, debug = False) |
| 610 | p.switch_connect('admin', 'admin', None) |
| 611 | |
| 612 | print "Ports are:" |
| 613 | buf = p.switch_get_port_names() |
| 614 | p.dump_list(buf) |
| 615 | |
| 616 | print "VLANs are:" |
| 617 | buf = p.vlan_get_list() |
| 618 | p.dump_list(buf) |
| 619 | |
| 620 | buf = p.vlan_get_name(2) |
| 621 | print "VLAN 2 is named \"%s\"" % buf |
| 622 | |
| 623 | print "Create VLAN 3" |
| 624 | p.vlan_create(3) |
| 625 | |
| 626 | buf = p.vlan_get_name(3) |
| 627 | print "VLAN 3 is named \"%s\"" % buf |
| 628 | |
| 629 | print "Set name of VLAN 3 to test333" |
| 630 | p.vlan_set_name(3, "test333") |
| 631 | |
| 632 | buf = p.vlan_get_name(3) |
| 633 | print "VLAN 3 is named \"%s\"" % buf |
| 634 | |
| 635 | print "VLANs are:" |
| 636 | buf = p.vlan_get_list() |
| 637 | p.dump_list(buf) |
| 638 | |
| 639 | print "Destroy VLAN 3" |
| 640 | p.vlan_destroy(3) |
| 641 | |
| 642 | print "VLANs are:" |
| 643 | buf = p.vlan_get_list() |
| 644 | p.dump_list(buf) |
| 645 | |
| 646 | buf = p.port_get_mode("Gi1/0/10") |
| 647 | print "Port Gi1/0/10 is in %s mode" % buf |
| 648 | |
| 649 | buf = p.port_get_mode("Gi1/0/11") |
| 650 | print "Port Gi1/0/11 is in %s mode" % buf |
| 651 | |
| 652 | # Test access stuff |
| 653 | buf = p.port_get_mode("Gi1/0/9") |
| 654 | print "Port Gi1/0/9 is in %s mode" % buf |
| 655 | |
| 656 | print "Set Gi1/0/9 to access mode" |
| 657 | p.port_set_mode("Gi1/0/9", "access") |
| 658 | |
| 659 | print "Move Gi1/0/9 to VLAN 4" |
| 660 | p.port_set_access_vlan("Gi1/0/9", 4) |
| 661 | |
| 662 | buf = p.port_get_access_vlan("Gi1/0/9") |
| 663 | print "Read from switch: Gi1/0/9 is on VLAN %s" % buf |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 664 | |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 665 | print "Move Gi1/0/9 back to VLAN 1" |
| 666 | p.port_set_access_vlan("Gi1/0/9", 1) |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 667 | |
Steve McIntyre | 448c1d0 | 2015-04-29 18:21:25 +0100 | [diff] [blame] | 668 | # Test access stuff |
| 669 | print "Set Gi1/0/9 to trunk mode" |
| 670 | p.port_set_mode("Gi1/0/9", "trunk") |
| 671 | print "Read from switch: which VLANs is Gi1/0/9 on?" |
| 672 | buf = p.port_get_trunk_vlan_list("Gi1/0/9") |
| 673 | p.dump_list(buf) |
| 674 | print "Add Gi1/0/9 to VLAN 2" |
| 675 | p.port_add_trunk_to_vlan("Gi1/0/9", 2) |
| 676 | print "Add Gi1/0/9 to VLAN 3" |
| 677 | p.port_add_trunk_to_vlan("Gi1/0/9", 3) |
| 678 | print "Add Gi1/0/9 to VLAN 4" |
| 679 | p.port_add_trunk_to_vlan("Gi1/0/9", 4) |
| 680 | print "Read from switch: which VLANs is Gi1/0/9 on?" |
| 681 | buf = p.port_get_trunk_vlan_list("Gi1/0/9") |
| 682 | p.dump_list(buf) |
| 683 | |
| 684 | p.port_remove_trunk_from_vlan("Gi1/0/9", 3) |
| 685 | p.port_remove_trunk_from_vlan("Gi1/0/9", 3) |
| 686 | p.port_remove_trunk_from_vlan("Gi1/0/9", 4) |
| 687 | print "Read from switch: which VLANs is Gi1/0/9 on?" |
| 688 | buf = p.port_get_trunk_vlan_list("Gi1/0/9") |
| 689 | p.dump_list(buf) |
| 690 | |
| 691 | |
| 692 | p.switch_save_running_config() |
| 693 | |
| 694 | p.switch_disconnect() |
| 695 | # p._show_config() |