-
Notifications
You must be signed in to change notification settings - Fork 20
CMLDEV-12: Add posibility to switch serial console used by PyAts conn… #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| from __future__ import annotations | ||
|
|
||
| import io | ||
| import re | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
|
|
@@ -120,6 +121,37 @@ def sync_testbed(self, username: str, password: str) -> None: | |
| self._testbed = self._load_pyats_testbed(testbed_yaml) | ||
| self.set_termserv_credentials(username, password) | ||
|
|
||
| def switch_pyats_serial_console(self, node_label: str, console_number: int) -> None: | ||
| """ | ||
| Switch to different serial console that is used to execute PyAts commands | ||
| should be executed after sync_testbed | ||
| and re-executed after every sync_testbed call. | ||
|
|
||
| :param node_label: The label/title of the device. | ||
| :param console_number: The serial console number to be used for PyAts. | ||
| """ | ||
|
|
||
| try: | ||
| pyats_device: Device = self._testbed.devices[node_label] | ||
| except KeyError: | ||
| raise PyatsDeviceNotFound(node_label) | ||
|
|
||
| node = self._lab.get_node_by_label(node_label) | ||
|
|
||
| # will raise API error inside if console does not exist for device | ||
| node.console_key(console_number) | ||
|
||
|
|
||
| old_connect_command = pyats_device.connections["a"]["command"] | ||
|
|
||
| pattern = rf"({re.escape(node_label)})/\d+" | ||
| new_console_cfg = f"/{console_number}" | ||
|
|
||
| new_connect_command = re.sub( | ||
| pattern, rf"\1{new_console_cfg}", old_connect_command | ||
|
||
| ) | ||
|
|
||
| pyats_device.connections["a"]["command"] = new_connect_command | ||
|
|
||
| def set_termserv_credentials( | ||
| self, | ||
| username: str | None = None, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
speaking of - if you use console 0, then switch to console 1 using this method, will the next command perhaps reuse the connection it already has established with console 0? Maybe coordinate with the other ticket about closing an existing connection if the connection number does not match.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you keep older connection open then it will still use console 0.