Skip to content
Snippets Groups Projects
Commit efccff70 authored by Thomas Schneider's avatar Thomas Schneider
Browse files

Config file support

parent ae3c1490
Branches
No related tags found
No related merge requests found
config.toml
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
......
......@@ -2,6 +2,11 @@ from getpass import getpass
import os
import os.path
try:
import tomllib
except ImportError:
import tomli as tomllib
import click
import lxml.etree as et
......@@ -11,34 +16,54 @@ from ncclient import manager
from tqdm import tqdm
cfg = dict()
@click.group(context_settings=dict(help_option_names=["-h", "--help"]))
def main():
pass
@click.option("-c", "--config", type=click.File("rb"), help="Config file")
def main(config):
if config is not None:
cfg.update(tomllib.load(config))
@main.command()
@click.option("-a", "--host", required=True, help="Device IP address or Hostname")
@click.option("-D", "--device", help="Device defined in config")
@click.option("-a", "--host", help="Device IP address or Hostname")
@click.option(
"--port", type=int, default=830, show_default=True, help="Netconf agent port"
)
@click.option("-u", "--username", help="Device Username (netconf agent username)")
@click.option(
"-u", "--username", required=True, help="Device Username (netconf agent username)"
"-d",
"--destination",
default="models",
type=click.Path(file_okay=False),
help="Destination directory",
)
@click.option("-d", "--destination", default="models", help="Destination directory")
def get_models(host, port, username, destination):
@click.pass_context
def get_models(ctx: click.Context, device, host, port, username, destination):
"""Get YANG models from device"""
dev = cfg.get("devices", {}).get(device, {})
_host = host or dev["host"]
if ctx.get_parameter_source("port").name() == "COMMANDLINE":
_port = port
else:
_port = dev.get("port", port)
_username = username or dev["username"]
try:
password = getpass(prompt="Password (C-d for none/agent only): ")
except EOFError:
password = None
with manager.connect(
host=host,
port=port,
username=username,
host=_host,
port=_port,
username=_username,
password=password,
timeout=90,
device_params={"name": "iosxe"},
device_params=dev.get("params"),
) as m:
caps = list(map(lambda x: x.strip(), m.server_capabilities))
if not any(map(lambda x: x.startswith(manager.NETCONF_MONITORING_NS), caps)):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment