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