From d1742d3034a9b566ecae20ba1f3df7bd1c61ecdf Mon Sep 17 00:00:00 2001 From: Jun Futagawa Date: Fri, 16 Jul 2021 15:00:49 +0900 Subject: [PATCH] Add secret option(-s/--secret|-S/--Secret) to enter enable mode (#50) --- docs/import.rst | 12 ++++++++++++ docs/interconnect.rst | 12 ++++++++++++ netbox_netprod_importer/__main__.py | 17 +++++++++++++++++ netbox_netprod_importer/devices_list.py | 8 +++++++- 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/docs/import.rst b/docs/import.rst index 456eed9..bb29354 100644 --- a/docs/import.rst +++ b/docs/import.rst @@ -47,6 +47,9 @@ An import can be started through the subcommand ``import``:: -p, --password ask for credentials for connections to the devices -P PASSWORD, --Password PASSWORD credentials for connections to the devices + -s, --secret ask for secret credentials to enter enable mode + -S SECRET, --Secret SECRET + secret credentials to enter enable mode -t THREADS, --threads THREADS number of threads to run -v LEVEL, --verbose LEVEL @@ -59,6 +62,11 @@ password/authentication by key. To change this behavior, the ``-u/--user`` and ``-p/--password|-P/--Password`` options can be used to specify the user to use, and tells the importer to ask|set for the password to use. +The importer will collect the data of the devices with the permissions of the +user logged in by the napalm driver. If enable mode is required to collect the +data, the ``-s/--secret|-S/--Secret`` options tells the importer to ask|set for +the secret to enter enable mode. + The import is multithreaded, and split by device. The default number of threads is 10, but can be changed with the ``-t/--threads`` option. @@ -102,6 +110,10 @@ To use a different user, for example `bar` do:: $ netbox-netprod-importer import -u bar -p -f ~/importer/devices.yml +If enable mode is required to collect the data of these devices, do:: + + $ netbox-netprod-importer import -u bar -p -s -f ~/importer/devices.yml + And to use more threads and enable the overwrite mode to get a clean clone of a device state:: diff --git a/docs/interconnect.rst b/docs/interconnect.rst index 6f1e949..2d761af 100644 --- a/docs/interconnect.rst +++ b/docs/interconnect.rst @@ -38,6 +38,9 @@ The interconnections feature can be started through the subcommand -p, --password ask for credentials for connections to the devices -P PASSWORD, --Password PASSWORD credentials for connections to the devices + -s, --secret ask for secret credentials to enter enable mode + -S SECRET, --Secret SECRET + secret credentials to enter enable mode -t THREADS, --threads THREADS number of threads to run --overwrite overwrite data already pushed @@ -51,6 +54,11 @@ password/authentication by key. To change this behavior, the ``-u/--user`` and ``-p/--password`` options can be used to specify the user to use, and tells netbox-netprod-importer to ask for the password to use. +The importer will collect the data of the devices with the permissions of the +user logged in by the napalm driver. If enable mode is required to collect the +data, the ``-s/--secret|-S/--Secret`` options tells the importer to ask|set for +the secret to enter enable mode. + The process is multithreaded, and split by device. The default number of threads is 10, but can be changed with the ``-t/--threads`` option. @@ -100,6 +108,10 @@ To use a different user, for example `bar` do:: $ netbox-netprod-importer interco -u bar -p -f ~/importer/devices.yml +If enable mode is required to collect the data of these devices, do:: + + $ netbox-netprod-importer interco -u bar -p -s -f ~/importer/devices.yml + And to use more threads:: $ netbox-netprod-importer interco -u bar -p -t 30 -f ~/importer/devices.yml diff --git a/netbox_netprod_importer/__main__.py b/netbox_netprod_importer/__main__.py index da8a977..c3fac3a 100644 --- a/netbox_netprod_importer/__main__.py +++ b/netbox_netprod_importer/__main__.py @@ -69,6 +69,16 @@ def parse_args(): help="credentials for connections to the devices", dest="password", type=str ) + sp.add_argument( + "-s", "--secret", + help="ask for secret credentials to enter enable mode", + dest="ask_secret", action="store_true" + ) + sp.add_argument( + "-S", "--Secret", metavar="SECRET", + help="secret credentials to enter enable mode", + dest="secret", type=str + ) sp.add_argument( "-t", "--threads", metavar="THREADS", help="number of threads to run", @@ -144,6 +154,13 @@ def _get_creds(parsed_args): elif parsed_args.password: creds = (parsed_args.user or getpass.getuser(), parsed_args.password) + if parsed_args.ask_secret: + creds = creds + tuple([getpass.getpass(prompt="Secret pasword: ")]) + elif parsed_args.secret: + creds = creds + tuple([parsed_args.secret]) + else: + creds = creds + tuple([None]) + return creds diff --git a/netbox_netprod_importer/devices_list.py b/netbox_netprod_importer/devices_list.py index 8e73af4..a4ef779 100644 --- a/netbox_netprod_importer/devices_list.py +++ b/netbox_netprod_importer/devices_list.py @@ -18,10 +18,16 @@ def parse_devices_yaml_def(devices_yaml, creds=None): with open(devices_yaml) as devices_yaml_str: for hostname, props in tqdm(yaml.safe_load(devices_yaml_str).items()): try: + optional_args = props.get("optional_args") + if creds[2] is not None: + if optional_args is None: + optional_args = {"secret": creds[2]} + else: + optional_args["secret"] = creds[2] devices[hostname] = DeviceImporter( props.get("target") or hostname, napalm_driver_name=props["driver"], - napalm_optional_args=props.get("optional_args"), + napalm_optional_args=optional_args, creds=creds, discovery_protocol=props.get("discovery_protocol") )