-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Netmiko cli_tools to make them more modular and use concurrent…
… futures (#3500)
- Loading branch information
Showing
6 changed files
with
207 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import os | ||
|
||
|
||
__version__ = "5.0.0" | ||
MAX_WORKERS = int(os.environ.get("NETMIKO_MAX_THREADS", 10)) | ||
ERROR_PATTERN = "%%%failed%%%" | ||
|
||
GREP = "/bin/grep" | ||
if not os.path.exists(GREP): | ||
GREP = "/usr/bin/grep" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from typing import Any, Dict | ||
from netmiko import ConnectHandler | ||
from netmiko.utilities import load_devices, obtain_all_devices | ||
from netmiko.cli_tools import ERROR_PATTERN | ||
|
||
|
||
def ssh_conn(device_name, device_params, cli_command=None, cfg_command=None): | ||
try: | ||
output = "" | ||
with ConnectHandler(**device_params) as net_connect: | ||
net_connect.enable() | ||
if cli_command: | ||
output += net_connect.send_command(cli_command) | ||
if cfg_command: | ||
output += net_connect.send_config_set(cfg_command) | ||
return device_name, output | ||
except Exception: | ||
return device_name, ERROR_PATTERN | ||
|
||
|
||
def obtain_devices(device_or_group: str) -> Dict[str, Dict[str, Any]]: | ||
""" | ||
Obtain the devices from the .netmiko.yml file using either a group-name or | ||
a device-name. A group-name will be a list of device-names. A device-name | ||
will just be a dictionary of device parameters (ConnectHandler **kwargs). | ||
""" | ||
my_devices = load_devices() | ||
if device_or_group == "all": | ||
devices = obtain_all_devices(my_devices) | ||
else: | ||
try: | ||
singledevice_or_group = my_devices[device_or_group] | ||
devices = {} | ||
if isinstance(singledevice_or_group, list): | ||
# Group of Devices | ||
device_group = singledevice_or_group | ||
for device_name in device_group: | ||
devices[device_name] = my_devices[device_name] | ||
else: | ||
# Single Device (dictionary) | ||
device_name = device_or_group | ||
device_dict = my_devices[device_name] | ||
devices[device_name] = device_dict | ||
except KeyError: | ||
return ( | ||
"Error reading from netmiko devices file." | ||
" Device or group not found: {0}".format(device_or_group) | ||
) | ||
|
||
return devices | ||
|
||
|
||
def update_device_params(params, username=None, password=None, secret=None): | ||
if username: | ||
params["username"] = username | ||
if password: | ||
params["password"] = password | ||
if secret: | ||
params["secret"] = secret | ||
return params |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.