networka 0.1.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- network_toolkit/__about__.py +4 -0
- network_toolkit/__init__.py +8 -0
- network_toolkit/__main__.py +10 -0
- network_toolkit/builtin_sequences/arista_eos/common.yml +53 -0
- network_toolkit/builtin_sequences/mikrotik_routeros/common.yml +52 -0
- network_toolkit/cli.py +265 -0
- network_toolkit/commands/__init__.py +8 -0
- network_toolkit/commands/backup.py +441 -0
- network_toolkit/commands/complete.py +172 -0
- network_toolkit/commands/config.py +1068 -0
- network_toolkit/commands/diff.py +485 -0
- network_toolkit/commands/download.py +197 -0
- network_toolkit/commands/firmware.py +780 -0
- network_toolkit/commands/info.py +377 -0
- network_toolkit/commands/list.py +345 -0
- network_toolkit/commands/routerboard_upgrade.py +204 -0
- network_toolkit/commands/run.py +839 -0
- network_toolkit/commands/schema_unified.py +91 -0
- network_toolkit/commands/sequence.py +17 -0
- network_toolkit/commands/ssh.py +540 -0
- network_toolkit/commands/ssh_engines.py +0 -0
- network_toolkit/commands/ssh_fallback.py +97 -0
- network_toolkit/commands/ssh_platform.py +120 -0
- network_toolkit/commands/upload.py +237 -0
- network_toolkit/commands/vendor_config_backup.py +378 -0
- network_toolkit/common/color_magic.py +177 -0
- network_toolkit/common/command.py +135 -0
- network_toolkit/common/command_base.py +135 -0
- network_toolkit/common/command_helpers.py +214 -0
- network_toolkit/common/credentials.py +86 -0
- network_toolkit/common/defaults.py +17 -0
- network_toolkit/common/errors.py +130 -0
- network_toolkit/common/interactive_confirmation.py +144 -0
- network_toolkit/common/logging.py +47 -0
- network_toolkit/common/output.py +594 -0
- network_toolkit/common/output_clean.py +244 -0
- network_toolkit/common/paths.py +86 -0
- network_toolkit/common/resolver.py +207 -0
- network_toolkit/common/styles.py +199 -0
- network_toolkit/common/table_generator.py +169 -0
- network_toolkit/common/table_providers.py +716 -0
- network_toolkit/config.py +1506 -0
- network_toolkit/credentials.py +288 -0
- network_toolkit/device.py +1658 -0
- network_toolkit/device_transfers.py +170 -0
- network_toolkit/exceptions.py +78 -0
- network_toolkit/ip_device.py +241 -0
- network_toolkit/platforms/__init__.py +21 -0
- network_toolkit/platforms/base.py +267 -0
- network_toolkit/platforms/cisco_ios/__init__.py +6 -0
- network_toolkit/platforms/cisco_ios/constants.py +15 -0
- network_toolkit/platforms/cisco_ios/operations.py +426 -0
- network_toolkit/platforms/cisco_iosxe/__init__.py +6 -0
- network_toolkit/platforms/cisco_iosxe/constants.py +15 -0
- network_toolkit/platforms/cisco_iosxe/operations.py +580 -0
- network_toolkit/platforms/cisco_shared/__init__.py +0 -0
- network_toolkit/platforms/cisco_shared/operations.py +0 -0
- network_toolkit/platforms/factory.py +190 -0
- network_toolkit/platforms/mikrotik_routeros/__init__.py +8 -0
- network_toolkit/platforms/mikrotik_routeros/confirmation_patterns.py +24 -0
- network_toolkit/platforms/mikrotik_routeros/constants.py +31 -0
- network_toolkit/platforms/mikrotik_routeros/operations.py +686 -0
- network_toolkit/results_enhanced.py +364 -0
- network_toolkit/schemas/__init__.py +8 -0
- network_toolkit/sequence_manager.py +239 -0
- network_toolkit/shell_completion/bash_completion_nw.sh +339 -0
- network_toolkit/shell_completion/zsh_completion_nw.zsh +136 -0
- network_toolkit/transport/__init__.py +15 -0
- network_toolkit/transport/factory.py +113 -0
- network_toolkit/transport/interfaces.py +46 -0
- network_toolkit/transport/nornir_inventory.py +173 -0
- network_toolkit/transport/nornir_netmiko.py +277 -0
- network_toolkit/transport/scrapli_sync.py +36 -0
- network_toolkit/tui/__init__.py +18 -0
- network_toolkit/tui/app.py +1275 -0
- network_toolkit/tui/clipboard.py +46 -0
- network_toolkit/tui/compat.py +111 -0
- network_toolkit/tui/constants.py +111 -0
- network_toolkit/tui/controller.py +550 -0
- network_toolkit/tui/data.py +149 -0
- network_toolkit/tui/dialogs.py +196 -0
- network_toolkit/tui/helpers.py +55 -0
- network_toolkit/tui/keymap.py +31 -0
- network_toolkit/tui/layout.py +111 -0
- network_toolkit/tui/models.py +93 -0
- network_toolkit/tui/output_manager.py +372 -0
- network_toolkit/tui/prompt.py +172 -0
- network_toolkit/tui/services.py +285 -0
- networka-0.1.9.dist-info/METADATA +316 -0
- networka-0.1.9.dist-info/RECORD +93 -0
- networka-0.1.9.dist-info/WHEEL +4 -0
- networka-0.1.9.dist-info/entry_points.txt +2 -0
- networka-0.1.9.dist-info/licenses/LICENSE +73 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"""Nornir inventory builder from NetworkConfig."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from nornir.core.inventory import Inventory
|
|
10
|
+
|
|
11
|
+
from network_toolkit.config import NetworkConfig
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def build_nornir_inventory(config: NetworkConfig) -> Inventory:
|
|
15
|
+
"""Build Nornir inventory from NetworkConfig.
|
|
16
|
+
|
|
17
|
+
Parameters
|
|
18
|
+
----------
|
|
19
|
+
config : NetworkConfig
|
|
20
|
+
The network configuration containing device definitions
|
|
21
|
+
|
|
22
|
+
Returns
|
|
23
|
+
-------
|
|
24
|
+
Inventory
|
|
25
|
+
Nornir inventory object
|
|
26
|
+
|
|
27
|
+
Raises
|
|
28
|
+
------
|
|
29
|
+
ImportError
|
|
30
|
+
If Nornir is not installed
|
|
31
|
+
"""
|
|
32
|
+
try:
|
|
33
|
+
from nornir.core.inventory import (
|
|
34
|
+
ConnectionOptions,
|
|
35
|
+
Defaults,
|
|
36
|
+
Groups,
|
|
37
|
+
Host,
|
|
38
|
+
Hosts,
|
|
39
|
+
Inventory,
|
|
40
|
+
)
|
|
41
|
+
except ImportError as e:
|
|
42
|
+
error_msg = (
|
|
43
|
+
"Nornir package required for Nornir transport. "
|
|
44
|
+
"Install with: pip install nornir"
|
|
45
|
+
)
|
|
46
|
+
raise ImportError(error_msg) from e
|
|
47
|
+
|
|
48
|
+
if not config.devices:
|
|
49
|
+
# Return empty inventory if no devices
|
|
50
|
+
return Inventory(hosts=Hosts(), groups=Groups(), defaults=Defaults())
|
|
51
|
+
|
|
52
|
+
hosts = Hosts()
|
|
53
|
+
|
|
54
|
+
for device_name, device_config in config.devices.items():
|
|
55
|
+
# Skip devices that don't use nornir transport
|
|
56
|
+
transport_type = getattr(device_config, "transport_type", "scrapli")
|
|
57
|
+
if transport_type != "nornir_netmiko":
|
|
58
|
+
continue
|
|
59
|
+
|
|
60
|
+
# Get credentials for this device
|
|
61
|
+
user_env_var = f"NW_USER_{device_name.upper().replace('-', '_')}"
|
|
62
|
+
password_env_var = f"NW_PASSWORD_{device_name.upper().replace('-', '_')}"
|
|
63
|
+
username = os.getenv(user_env_var) or os.getenv("NW_USER_DEFAULT", "admin")
|
|
64
|
+
password = os.getenv(password_env_var) or os.getenv("NW_PASSWORD_DEFAULT", "")
|
|
65
|
+
|
|
66
|
+
if not password:
|
|
67
|
+
# Skip devices without proper credentials
|
|
68
|
+
continue
|
|
69
|
+
|
|
70
|
+
# Map device type to Netmiko platform
|
|
71
|
+
netmiko_platform = _map_to_netmiko_platform(device_config.device_type)
|
|
72
|
+
|
|
73
|
+
# Create connection options
|
|
74
|
+
netmiko_options = ConnectionOptions(
|
|
75
|
+
extras={
|
|
76
|
+
"device_type": netmiko_platform,
|
|
77
|
+
"timeout": getattr(device_config, "timeout", 30),
|
|
78
|
+
"global_delay_factor": 1,
|
|
79
|
+
"use_keys": False,
|
|
80
|
+
"key_file": None,
|
|
81
|
+
}
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
# Create host with all necessary parameters
|
|
85
|
+
host = Host(
|
|
86
|
+
name=device_name,
|
|
87
|
+
hostname=device_config.host,
|
|
88
|
+
username=username,
|
|
89
|
+
password=password,
|
|
90
|
+
port=device_config.port or 22,
|
|
91
|
+
platform=netmiko_platform,
|
|
92
|
+
connection_options={"netmiko": netmiko_options},
|
|
93
|
+
# Store additional device metadata
|
|
94
|
+
data={
|
|
95
|
+
"description": device_config.description,
|
|
96
|
+
"device_type": device_config.device_type,
|
|
97
|
+
"model": device_config.model,
|
|
98
|
+
"location": device_config.location,
|
|
99
|
+
"tags": device_config.tags or [],
|
|
100
|
+
"original_config": device_config.model_dump(),
|
|
101
|
+
},
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
hosts[device_name] = host
|
|
105
|
+
|
|
106
|
+
# Create empty groups for now - could be extended to map device_groups
|
|
107
|
+
groups = Groups()
|
|
108
|
+
|
|
109
|
+
# Create defaults
|
|
110
|
+
default_netmiko_options = ConnectionOptions(
|
|
111
|
+
extras={
|
|
112
|
+
"timeout": config.general.timeout,
|
|
113
|
+
"global_delay_factor": 1,
|
|
114
|
+
"use_keys": False,
|
|
115
|
+
}
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
defaults = Defaults(connection_options={"netmiko": default_netmiko_options})
|
|
119
|
+
|
|
120
|
+
return Inventory(hosts=hosts, groups=groups, defaults=defaults)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _map_to_netmiko_platform(device_type: str) -> str:
|
|
124
|
+
"""Map internal device types to Netmiko platform names.
|
|
125
|
+
|
|
126
|
+
Parameters
|
|
127
|
+
----------
|
|
128
|
+
device_type : str
|
|
129
|
+
Internal device type identifier
|
|
130
|
+
|
|
131
|
+
Returns
|
|
132
|
+
-------
|
|
133
|
+
str
|
|
134
|
+
Netmiko platform name
|
|
135
|
+
"""
|
|
136
|
+
platform_mapping = {
|
|
137
|
+
# MikroTik
|
|
138
|
+
"mikrotik_routeros": "mikrotik_routeros",
|
|
139
|
+
# Cisco
|
|
140
|
+
"cisco_iosxe": "cisco_xe",
|
|
141
|
+
"cisco_ios": "cisco_ios",
|
|
142
|
+
"cisco_nxos": "cisco_nxos",
|
|
143
|
+
"cisco_iosxr": "cisco_xr",
|
|
144
|
+
# Arista
|
|
145
|
+
"arista_eos": "arista_eos",
|
|
146
|
+
# Juniper
|
|
147
|
+
"juniper_junos": "juniper_junos",
|
|
148
|
+
# Others
|
|
149
|
+
"linux": "linux",
|
|
150
|
+
"generic": "terminal_server",
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return platform_mapping.get(device_type, device_type)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def get_supported_nornir_platforms() -> dict[str, str]:
|
|
157
|
+
"""Get mapping of supported platforms for Nornir transport.
|
|
158
|
+
|
|
159
|
+
Returns
|
|
160
|
+
-------
|
|
161
|
+
dict[str, str]
|
|
162
|
+
Mapping of internal device_type to description
|
|
163
|
+
"""
|
|
164
|
+
return {
|
|
165
|
+
"mikrotik_routeros": "MikroTik RouterOS",
|
|
166
|
+
"cisco_iosxe": "Cisco IOS-XE",
|
|
167
|
+
"cisco_ios": "Cisco IOS",
|
|
168
|
+
"cisco_nxos": "Cisco NX-OS",
|
|
169
|
+
"cisco_iosxr": "Cisco IOS-XR",
|
|
170
|
+
"arista_eos": "Arista EOS",
|
|
171
|
+
"juniper_junos": "Juniper JunOS",
|
|
172
|
+
"linux": "Linux SSH",
|
|
173
|
+
}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
"""Nornir + Netmiko transport implementation."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import time
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from nornir import Nornir
|
|
10
|
+
|
|
11
|
+
from network_toolkit.transport.interfaces import CommandResult, ConnectionState
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class NornirNetmikoTransport:
|
|
15
|
+
"""Nornir + Netmiko transport implementation."""
|
|
16
|
+
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
nornir_runner: Nornir,
|
|
20
|
+
device_name: str,
|
|
21
|
+
host: str,
|
|
22
|
+
port: int,
|
|
23
|
+
) -> None:
|
|
24
|
+
"""Initialize the Nornir+Netmiko transport.
|
|
25
|
+
|
|
26
|
+
Parameters
|
|
27
|
+
----------
|
|
28
|
+
nornir_runner : Nornir
|
|
29
|
+
The Nornir runner instance
|
|
30
|
+
device_name : str
|
|
31
|
+
Name of the device in Nornir inventory
|
|
32
|
+
host : str
|
|
33
|
+
Device hostname/IP
|
|
34
|
+
port : int
|
|
35
|
+
Connection port
|
|
36
|
+
"""
|
|
37
|
+
self.nr = nornir_runner
|
|
38
|
+
self.device_name = device_name
|
|
39
|
+
self.host = host
|
|
40
|
+
self.port = port
|
|
41
|
+
self._last_activity: float | None = None
|
|
42
|
+
self._connected = False
|
|
43
|
+
|
|
44
|
+
def open(self) -> None:
|
|
45
|
+
"""Open the connection to the device."""
|
|
46
|
+
# Nornir connections are managed automatically
|
|
47
|
+
# We'll just mark as connected and test connectivity
|
|
48
|
+
try:
|
|
49
|
+
# Import here to avoid dependency issues if nornir not installed
|
|
50
|
+
from nornir_netmiko.tasks import netmiko_send_command
|
|
51
|
+
|
|
52
|
+
# Test connection with a simple command
|
|
53
|
+
filtered_nr = self.nr.filter(name=self.device_name)
|
|
54
|
+
result = filtered_nr.run(
|
|
55
|
+
task=netmiko_send_command,
|
|
56
|
+
command_string="", # Empty command for connection test
|
|
57
|
+
read_timeout=5,
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
if result.failed:
|
|
61
|
+
error_msg = f"Failed to connect to {self.device_name}"
|
|
62
|
+
raise ConnectionError(error_msg)
|
|
63
|
+
|
|
64
|
+
self._connected = True
|
|
65
|
+
self._last_activity = time.time()
|
|
66
|
+
except ImportError as e:
|
|
67
|
+
error_msg = (
|
|
68
|
+
"Nornir and nornir-netmiko packages required. "
|
|
69
|
+
"Install with: pip install nornir nornir-netmiko"
|
|
70
|
+
)
|
|
71
|
+
raise ImportError(error_msg) from e
|
|
72
|
+
|
|
73
|
+
def close(self) -> None:
|
|
74
|
+
"""Close the connection to the device."""
|
|
75
|
+
# Nornir manages connections automatically
|
|
76
|
+
self._connected = False
|
|
77
|
+
self._last_activity = None
|
|
78
|
+
|
|
79
|
+
def send_command(self, command: str) -> CommandResult:
|
|
80
|
+
"""Send a command to the device and return the result."""
|
|
81
|
+
start_time = time.time()
|
|
82
|
+
|
|
83
|
+
try:
|
|
84
|
+
# Use Netmiko directly instead of Nornir
|
|
85
|
+
from netmiko import ConnectHandler
|
|
86
|
+
|
|
87
|
+
# Get device config from Nornir inventory
|
|
88
|
+
host = self.nr.inventory.hosts[self.device_name]
|
|
89
|
+
|
|
90
|
+
# Create Netmiko connection directly
|
|
91
|
+
device_params = {
|
|
92
|
+
"device_type": host.platform,
|
|
93
|
+
"host": host.hostname,
|
|
94
|
+
"username": host.username,
|
|
95
|
+
"password": host.password,
|
|
96
|
+
"port": host.port,
|
|
97
|
+
"timeout": 30,
|
|
98
|
+
"global_delay_factor": 1,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
# Connect and execute command
|
|
102
|
+
with ConnectHandler(**device_params) as connection:
|
|
103
|
+
output = connection.send_command(command)
|
|
104
|
+
|
|
105
|
+
execution_time = time.time() - start_time
|
|
106
|
+
self._last_activity = time.time()
|
|
107
|
+
|
|
108
|
+
return CommandResult(
|
|
109
|
+
result=str(output), # Ensure it's a string
|
|
110
|
+
failed=False,
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
except Exception as e:
|
|
114
|
+
execution_time = time.time() - start_time
|
|
115
|
+
error_msg = f"Failed to execute command '{command}': {e}"
|
|
116
|
+
|
|
117
|
+
return CommandResult(
|
|
118
|
+
result="",
|
|
119
|
+
failed=True,
|
|
120
|
+
)
|
|
121
|
+
"""Send a single command and return the result."""
|
|
122
|
+
try:
|
|
123
|
+
from nornir_netmiko.tasks import netmiko_send_command
|
|
124
|
+
except ImportError as e:
|
|
125
|
+
error_msg = (
|
|
126
|
+
"nornir-netmiko package required. "
|
|
127
|
+
"Install with: pip install nornir-netmiko"
|
|
128
|
+
)
|
|
129
|
+
raise ImportError(error_msg) from e
|
|
130
|
+
|
|
131
|
+
start_time = time.time()
|
|
132
|
+
|
|
133
|
+
# Filter to single device and run command
|
|
134
|
+
filtered_nr = self.nr.filter(name=self.device_name)
|
|
135
|
+
result = filtered_nr.run(
|
|
136
|
+
task=netmiko_send_command,
|
|
137
|
+
command_string=command,
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
execution_time = time.time() - start_time
|
|
141
|
+
self._last_activity = time.time()
|
|
142
|
+
|
|
143
|
+
# Extract result from Nornir MultiResult
|
|
144
|
+
device_result = result[self.device_name]
|
|
145
|
+
|
|
146
|
+
return CommandResult(
|
|
147
|
+
result=str(device_result.result) if device_result.result else "",
|
|
148
|
+
failed=device_result.failed,
|
|
149
|
+
execution_time=execution_time,
|
|
150
|
+
error_context=(
|
|
151
|
+
{
|
|
152
|
+
"exception": str(device_result.exception)
|
|
153
|
+
if device_result.exception
|
|
154
|
+
else None,
|
|
155
|
+
"host": device_result.host.name if device_result.host else None,
|
|
156
|
+
}
|
|
157
|
+
if device_result.failed
|
|
158
|
+
else None
|
|
159
|
+
),
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
def send_commands(self, commands: list[str]) -> list[CommandResult]:
|
|
163
|
+
"""Send multiple commands efficiently using Nornir's batch capabilities."""
|
|
164
|
+
try:
|
|
165
|
+
from nornir_netmiko.tasks import netmiko_send_commands
|
|
166
|
+
except ImportError:
|
|
167
|
+
# Fallback to individual commands
|
|
168
|
+
return [self.send_command(cmd) for cmd in commands]
|
|
169
|
+
|
|
170
|
+
filtered_nr = self.nr.filter(name=self.device_name)
|
|
171
|
+
result = filtered_nr.run(
|
|
172
|
+
task=netmiko_send_commands,
|
|
173
|
+
command_string=commands,
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
self._last_activity = time.time()
|
|
177
|
+
|
|
178
|
+
device_result = result[self.device_name]
|
|
179
|
+
|
|
180
|
+
if device_result.failed:
|
|
181
|
+
# Return failed result for all commands
|
|
182
|
+
failed_result = CommandResult(
|
|
183
|
+
result="",
|
|
184
|
+
failed=True,
|
|
185
|
+
)
|
|
186
|
+
return [failed_result for _ in commands]
|
|
187
|
+
|
|
188
|
+
# Convert results to CommandResult list
|
|
189
|
+
results = []
|
|
190
|
+
if isinstance(device_result.result, list):
|
|
191
|
+
for _i, cmd_result in enumerate(device_result.result):
|
|
192
|
+
results.append(
|
|
193
|
+
CommandResult(
|
|
194
|
+
result=str(cmd_result),
|
|
195
|
+
failed=False,
|
|
196
|
+
)
|
|
197
|
+
)
|
|
198
|
+
else:
|
|
199
|
+
# Single result, split by commands
|
|
200
|
+
result_text = str(device_result.result)
|
|
201
|
+
for _ in commands:
|
|
202
|
+
results.append(
|
|
203
|
+
CommandResult(
|
|
204
|
+
result=result_text,
|
|
205
|
+
failed=False,
|
|
206
|
+
)
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
return results
|
|
210
|
+
|
|
211
|
+
def send_interactive(
|
|
212
|
+
self,
|
|
213
|
+
_interact_events: list[tuple[str, str, bool]],
|
|
214
|
+
_timeout_ops: float,
|
|
215
|
+
) -> str:
|
|
216
|
+
"""Send interactive command with prompts and responses."""
|
|
217
|
+
# This is more complex with Netmiko, would need custom implementation
|
|
218
|
+
# For now, raise NotImplementedError
|
|
219
|
+
error_msg = (
|
|
220
|
+
"Interactive commands not yet implemented for Nornir+Netmiko transport. "
|
|
221
|
+
"Use Scrapli transport for interactive commands."
|
|
222
|
+
)
|
|
223
|
+
raise NotImplementedError(error_msg)
|
|
224
|
+
|
|
225
|
+
def get_connection_state(self) -> ConnectionState:
|
|
226
|
+
"""Get current connection state information."""
|
|
227
|
+
return ConnectionState(
|
|
228
|
+
connected=self._connected,
|
|
229
|
+
transport_type="nornir_netmiko",
|
|
230
|
+
device_name=self.device_name,
|
|
231
|
+
host=self.host,
|
|
232
|
+
port=self.port,
|
|
233
|
+
last_activity=self._last_activity,
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
def is_alive(self) -> bool:
|
|
237
|
+
"""Check if the connection is still active."""
|
|
238
|
+
return self._connected
|
|
239
|
+
|
|
240
|
+
def execute_on_group(
|
|
241
|
+
self, device_names: list[str], command: str
|
|
242
|
+
) -> dict[str, CommandResult]:
|
|
243
|
+
"""Execute command on multiple devices efficiently using Nornir's
|
|
244
|
+
parallel execution."""
|
|
245
|
+
try:
|
|
246
|
+
from nornir_netmiko.tasks import netmiko_send_command
|
|
247
|
+
except ImportError as e:
|
|
248
|
+
error_msg = (
|
|
249
|
+
"nornir-netmiko package required. "
|
|
250
|
+
"Install with: pip install nornir-netmiko"
|
|
251
|
+
)
|
|
252
|
+
raise ImportError(error_msg) from e
|
|
253
|
+
|
|
254
|
+
# Filter to target devices and run command in parallel
|
|
255
|
+
filtered_nr = self.nr.filter(name__in=device_names)
|
|
256
|
+
results = filtered_nr.run(
|
|
257
|
+
task=netmiko_send_command,
|
|
258
|
+
command_string=command,
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
# Convert Nornir results to CommandResult dict
|
|
262
|
+
command_results = {}
|
|
263
|
+
for device_name in device_names:
|
|
264
|
+
if device_name in results:
|
|
265
|
+
device_result = results[device_name]
|
|
266
|
+
command_results[device_name] = CommandResult(
|
|
267
|
+
result=str(device_result.result) if device_result.result else "",
|
|
268
|
+
failed=device_result.failed,
|
|
269
|
+
)
|
|
270
|
+
else:
|
|
271
|
+
# Device not found in results
|
|
272
|
+
command_results[device_name] = CommandResult(
|
|
273
|
+
result="",
|
|
274
|
+
failed=True,
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
return command_results
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""Scrapli sync adapter implementing the Transport protocol."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from scrapli import Scrapli
|
|
6
|
+
|
|
7
|
+
from network_toolkit.transport.interfaces import CommandResult
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ScrapliSyncTransport:
|
|
11
|
+
"""Thin adapter over Scrapli to satisfy our Transport Protocol."""
|
|
12
|
+
|
|
13
|
+
def __init__(self, driver: Scrapli) -> None:
|
|
14
|
+
self._driver = driver
|
|
15
|
+
|
|
16
|
+
def open(self) -> None: # pragma: no cover - passthrough
|
|
17
|
+
self._driver.open()
|
|
18
|
+
|
|
19
|
+
def close(self) -> None: # pragma: no cover - passthrough
|
|
20
|
+
self._driver.close()
|
|
21
|
+
|
|
22
|
+
def send_command(self, command: str) -> CommandResult:
|
|
23
|
+
resp = self._driver.send_command(command)
|
|
24
|
+
# Scrapli returns an object with .result and .failed
|
|
25
|
+
return CommandResult(
|
|
26
|
+
result=resp.result, failed=bool(getattr(resp, "failed", False))
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
def send_interactive(
|
|
30
|
+
self, interact_events: list[tuple[str, str, bool]], timeout_ops: float
|
|
31
|
+
) -> str:
|
|
32
|
+
resp = self._driver.send_interactive(
|
|
33
|
+
interact_events=interact_events, timeout_ops=timeout_ops
|
|
34
|
+
)
|
|
35
|
+
# Scrapli returns a Response; we only care about repr/text for now
|
|
36
|
+
return repr(resp)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Textual-based TUI for Network Worker (nw).
|
|
2
|
+
|
|
3
|
+
This package contains an optional Textual UI that lets users select
|
|
4
|
+
devices/groups on the left and sequences/commands on the right.
|
|
5
|
+
|
|
6
|
+
Design goals:
|
|
7
|
+
- Keep TUI isolated from core CLI and libraries (no imports from here in CLI paths)
|
|
8
|
+
- Lazy-import Textual so normal CLI usage doesn't require the dependency
|
|
9
|
+
- Provide a simple first iteration that focuses on selection UX; execution comes later
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"run",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
from network_toolkit.tui.app import run # re-export for console script
|