From 4ada37f63131919a6b91cbd611a5eb366199c2d8 Mon Sep 17 00:00:00 2001 From: Nabil CHARAF Date: Tue, 25 Oct 2022 08:20:35 +0200 Subject: [PATCH 1/6] Add new platform netmiko_auto that use sshautodetect --- nornir_netmiko/connections/netmiko.py | 136 ++++++++++++++------------ 1 file changed, 73 insertions(+), 63 deletions(-) diff --git a/nornir_netmiko/connections/netmiko.py b/nornir_netmiko/connections/netmiko.py index 59dd641..087e142 100644 --- a/nornir_netmiko/connections/netmiko.py +++ b/nornir_netmiko/connections/netmiko.py @@ -1,63 +1,73 @@ -from typing import Any, Dict, Optional - -from netmiko import ConnectHandler - -from nornir.core.configuration import Config - - -CONNECTION_NAME = "netmiko" - -napalm_to_netmiko_map = { - "ios": "cisco_ios", - "nxos": "cisco_nxos", - "nxos_ssh": "cisco_nxos", - "eos": "arista_eos", - "junos": "juniper_junos", - "iosxr": "cisco_xr", -} - - -class Netmiko: - """ - This plugin connects to the device using the Netmiko driver and sets the - relevant connection. - Inventory: - extras: maps to argument passed to ``ConnectHandler``. - """ - - def open( - self, - hostname: Optional[str], - username: Optional[str], - password: Optional[str], - port: Optional[int], - platform: Optional[str], - extras: Optional[Dict[str, Any]] = None, - configuration: Optional[Config] = None, - ) -> None: - parameters = { - "host": hostname, - "username": username, - "password": password, - "port": port, - } - - try: - parameters[ - "ssh_config_file" - ] = configuration.ssh.config_file # type: ignore - except AttributeError: - pass - - if platform is not None: - # Look platform up in corresponding map, if no entry return the host.nos unmodified - platform = napalm_to_netmiko_map.get(platform, platform) - parameters["device_type"] = platform - - extras = extras or {} - parameters.update(extras) - connection = ConnectHandler(**parameters) - self.connection = connection - - def close(self) -> None: - self.connection.disconnect() +from typing import Any, Dict, Optional + +from netmiko import SSHDetect +from netmiko import ConnectHandler + +from nornir.core.configuration import Config + + +CONNECTION_NAME = "netmiko" + +napalm_to_netmiko_map = { + "ios": "cisco_ios", + "nxos": "cisco_nxos", + "nxos_ssh": "cisco_nxos", + "eos": "arista_eos", + "junos": "juniper_junos", + "iosxr": "cisco_xr", + "netmiko_auto":"autodetect" +} + + +class Netmiko: + """ + This plugin connects to the device using the Netmiko driver and sets the + relevant connection. + Inventory: + extras: maps to argument passed to ``ConnectHandler``. + """ + + def open( + self, + hostname: Optional[str], + username: Optional[str], + password: Optional[str], + port: Optional[int], + platform: Optional[str], + extras: Optional[Dict[str, Any]] = None, + configuration: Optional[Config] = None, + ) -> None: + parameters = { + "host": hostname, + "username": username, + "password": password, + "port": port, + } + + try: + parameters[ + "ssh_config_file" + ] = configuration.ssh.config_file # type: ignore + except AttributeError: + pass + + if platform is not None: + # Look platform up in corresponding map, if no entry return the host.nos unmodified + platform = napalm_to_netmiko_map.get(platform, platform) + if platform == "autodetect":# If we are using autodect + parameters["device_type"] = platform + connection = ConnectHandler(**parameters) + best_match = guesser.autodetect() + if best_match: # If we find a device_type + parameters['device_type'] = best_match + else: + raise ValueError("Netmiko cannot detect the device_type") + parameters["device_type"] = platform + + extras = extras or {} + parameters.update(extras) + connection = ConnectHandler(**parameters) + self.connection = connection + + def close(self) -> None: + self.connection.disconnect() From a59bc9081c15a83d3baf631bb1eb614c2939b57b Mon Sep 17 00:00:00 2001 From: Nabil CHARAF Date: Tue, 25 Oct 2022 08:22:47 +0200 Subject: [PATCH 2/6] change version --- pyproject.toml | 66 +++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 3003126..e360e9f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,33 +1,33 @@ -[tool.poetry] -name = "nornir_netmiko" -version = "0.2.0" -description = "Netmiko's plugins for Nornir" -authors = ["Kirk Byers "] -license = "Apache-2.0" -readme = "README.md" -repository = "https://github.com/ktbyers/nornir_netmiko" -classifiers = [ - "License :: OSI Approved :: Apache Software License", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", -] - -[tool.poetry.plugins."nornir.plugins.connections"] -"netmiko" = "nornir_netmiko.connections:Netmiko" - -[tool.poetry.dependencies] -python = "^3.7" -netmiko = "^4.0.0" -textfsm = "1.1.2" - -[tool.poetry.dev-dependencies] -black = "22.3.0" -mypy = "0.942" -pylama = "8.3.8" -pytest = "7.1.1" - -[build-system] -requires = ["poetry>=1.1.8"] -build-backend = "poetry.masonry.api" +[tool.poetry] +name = "nornir_netmiko" +version = "0.2.1" +description = "Netmiko's plugins for Nornir" +authors = ["Kirk Byers "] +license = "Apache-2.0" +readme = "README.md" +repository = "https://github.com/ktbyers/nornir_netmiko" +classifiers = [ + "License :: OSI Approved :: Apache Software License", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", +] + +[tool.poetry.plugins."nornir.plugins.connections"] +"netmiko" = "nornir_netmiko.connections:Netmiko" + +[tool.poetry.dependencies] +python = "^3.7" +netmiko = "^4.0.0" +textfsm = "1.1.2" + +[tool.poetry.dev-dependencies] +black = "22.3.0" +mypy = "0.942" +pylama = "8.3.8" +pytest = "7.1.1" + +[build-system] +requires = ["poetry>=1.1.8"] +build-backend = "poetry.masonry.api" From 047ec955aab7947795c9593e86ab4836f7c109b2 Mon Sep 17 00:00:00 2001 From: Nabil Date: Sat, 8 Jul 2023 21:31:19 +0200 Subject: [PATCH 3/6] Restore pyproject content --- pyproject.toml | 66 +++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2bbce44..50f81cd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,33 +1,33 @@ -[tool.poetry] -name = "nornir_netmiko" -version = "1.0.0" -description = "Netmiko's plugins for Nornir" -authors = ["Kirk Byers "] -license = "Apache-2.0" -readme = "README.md" -repository = "https://github.com/ktbyers/nornir_netmiko" -classifiers = [ - "License :: OSI Approved :: Apache Software License", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", -] - -[tool.poetry.plugins."nornir.plugins.connections"] -"netmiko" = "nornir_netmiko.connections:Netmiko" - -[tool.poetry.dependencies] -python = "^3.7" -netmiko = "^4.0.0" - -[tool.poetry.dev-dependencies] -black = "22.3.0" -mypy = "0.942" -pylama = "8.4.1" -pytest = "7.1.1" - -[build-system] -requires = ["poetry>=1.1.8"] -build-backend = "poetry.masonry.api" \ No newline at end of file +[tool.poetry] +name = "nornir_netmiko" +version = "1.0.0" +description = "Netmiko's plugins for Nornir" +authors = ["Kirk Byers "] +license = "Apache-2.0" +readme = "README.md" +repository = "https://github.com/ktbyers/nornir_netmiko" +classifiers = [ + "License :: OSI Approved :: Apache Software License", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", +] + +[tool.poetry.plugins."nornir.plugins.connections"] +"netmiko" = "nornir_netmiko.connections:Netmiko" + +[tool.poetry.dependencies] +python = "^3.7" +netmiko = "^4.0.0" + +[tool.poetry.dev-dependencies] +black = "22.3.0" +mypy = "0.942" +pylama = "8.4.1" +pytest = "7.1.1" + +[build-system] +requires = ["poetry>=1.1.8"] +build-backend = "poetry.masonry.api" From a5fe6c5f82c8e5fe366d81150c85ef2c649e63fe Mon Sep 17 00:00:00 2001 From: Nabil Date: Sat, 8 Jul 2023 21:37:23 +0200 Subject: [PATCH 4/6] Add new platform netmiko_auto that use sshautodetect --- nornir_netmiko/connections/netmiko.py | 146 +++++++++++++------------- 1 file changed, 73 insertions(+), 73 deletions(-) diff --git a/nornir_netmiko/connections/netmiko.py b/nornir_netmiko/connections/netmiko.py index 087e142..c5aedaa 100644 --- a/nornir_netmiko/connections/netmiko.py +++ b/nornir_netmiko/connections/netmiko.py @@ -1,73 +1,73 @@ -from typing import Any, Dict, Optional - -from netmiko import SSHDetect -from netmiko import ConnectHandler - -from nornir.core.configuration import Config - - -CONNECTION_NAME = "netmiko" - -napalm_to_netmiko_map = { - "ios": "cisco_ios", - "nxos": "cisco_nxos", - "nxos_ssh": "cisco_nxos", - "eos": "arista_eos", - "junos": "juniper_junos", - "iosxr": "cisco_xr", - "netmiko_auto":"autodetect" -} - - -class Netmiko: - """ - This plugin connects to the device using the Netmiko driver and sets the - relevant connection. - Inventory: - extras: maps to argument passed to ``ConnectHandler``. - """ - - def open( - self, - hostname: Optional[str], - username: Optional[str], - password: Optional[str], - port: Optional[int], - platform: Optional[str], - extras: Optional[Dict[str, Any]] = None, - configuration: Optional[Config] = None, - ) -> None: - parameters = { - "host": hostname, - "username": username, - "password": password, - "port": port, - } - - try: - parameters[ - "ssh_config_file" - ] = configuration.ssh.config_file # type: ignore - except AttributeError: - pass - - if platform is not None: - # Look platform up in corresponding map, if no entry return the host.nos unmodified - platform = napalm_to_netmiko_map.get(platform, platform) - if platform == "autodetect":# If we are using autodect - parameters["device_type"] = platform - connection = ConnectHandler(**parameters) - best_match = guesser.autodetect() - if best_match: # If we find a device_type - parameters['device_type'] = best_match - else: - raise ValueError("Netmiko cannot detect the device_type") - parameters["device_type"] = platform - - extras = extras or {} - parameters.update(extras) - connection = ConnectHandler(**parameters) - self.connection = connection - - def close(self) -> None: - self.connection.disconnect() +from typing import Any, Dict, Optional + +from netmiko import ConnectHandler +from netmiko.ssh_autodetect import SSHDetect + +from nornir.core.configuration import Config + + +CONNECTION_NAME = "netmiko" + +napalm_to_netmiko_map = { + "ios": "cisco_ios", + "nxos": "cisco_nxos", + "nxos_ssh": "cisco_nxos", + "eos": "arista_eos", + "junos": "juniper_junos", + "iosxr": "cisco_xr", + "netmiko_auto":"autodetect" +} + + +class Netmiko: + """ + This plugin connects to the device using the Netmiko driver and sets the + relevant connection. + Inventory: + extras: maps to argument passed to ``ConnectHandler``. + """ + + def open( + self, + hostname: Optional[str], + username: Optional[str], + password: Optional[str], + port: Optional[int], + platform: Optional[str], + extras: Optional[Dict[str, Any]] = None, + configuration: Optional[Config] = None, + ) -> None: + parameters = { + "host": hostname, + "username": username, + "password": password, + "port": port, + } + + try: + parameters[ + "ssh_config_file" + ] = configuration.ssh.config_file # type: ignore + except AttributeError: + pass + + if platform is not None: + # Look platform up in corresponding map, if no entry return the host.nos unmodified + platform = napalm_to_netmiko_map.get(platform, platform) + if platform == "autodetect":# If we are using autodect + parameters["device_type"] = platform + guesser = SSHDetect(**parameters) + best_match = guesser.autodetect() + if best_match: # If we find a device_type + parameters['device_type'] = best_match + else: + raise ValueError("Netmiko cannot detect the device_type") + parameters["device_type"] = platform + + extras = extras or {} + parameters.update(extras) + connection = ConnectHandler(**parameters) + self.connection = connection + + def close(self) -> None: + self.connection.disconnect() From 7baf91725955a497f0b80f5866e0b962aa2edcb1 Mon Sep 17 00:00:00 2001 From: Nabil Date: Sat, 8 Jul 2023 21:40:34 +0200 Subject: [PATCH 5/6] Add new platform netmiko_auto that use sshautodetect --- nornir_netmiko/connections/netmiko.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nornir_netmiko/connections/netmiko.py b/nornir_netmiko/connections/netmiko.py index c5aedaa..d700385 100644 --- a/nornir_netmiko/connections/netmiko.py +++ b/nornir_netmiko/connections/netmiko.py @@ -62,7 +62,8 @@ def open( parameters['device_type'] = best_match else: raise ValueError("Netmiko cannot detect the device_type") - parameters["device_type"] = platform + else: + parameters["device_type"] = platform extras = extras or {} parameters.update(extras) From f7ced8b5b97c4a8c75f83b2cfc0495fd6e1b9725 Mon Sep 17 00:00:00 2001 From: Nabil Date: Mon, 10 Jul 2023 23:04:46 +0200 Subject: [PATCH 6/6] Correct code to be conform to black --- nornir_netmiko/connections/netmiko.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nornir_netmiko/connections/netmiko.py b/nornir_netmiko/connections/netmiko.py index d700385..1d957bf 100644 --- a/nornir_netmiko/connections/netmiko.py +++ b/nornir_netmiko/connections/netmiko.py @@ -15,7 +15,7 @@ "eos": "arista_eos", "junos": "juniper_junos", "iosxr": "cisco_xr", - "netmiko_auto":"autodetect" + "netmiko_auto": "autodetect", } @@ -54,12 +54,12 @@ def open( if platform is not None: # Look platform up in corresponding map, if no entry return the host.nos unmodified platform = napalm_to_netmiko_map.get(platform, platform) - if platform == "autodetect":# If we are using autodect + if platform == "autodetect": # If we are using autodect parameters["device_type"] = platform guesser = SSHDetect(**parameters) best_match = guesser.autodetect() - if best_match: # If we find a device_type - parameters['device_type'] = best_match + if best_match: # If we find a device_type + parameters["device_type"] = best_match else: raise ValueError("Netmiko cannot detect the device_type") else: