diff --git a/.gitignore b/.gitignore index 68bc17f9ff2104a9d7b6777058bb4c343ca72609..b803f358e4f28e4085c901cead7dd9ca24390c5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +config.toml + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/src/nctool/cli.py b/src/nctool/cli.py index ad4d289fc32a84c3af16a0ad7d18969f1c6f18e0..7b473334a573ef6a1bc69236525feab205b02f65 100644 --- a/src/nctool/cli.py +++ b/src/nctool/cli.py @@ -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)):