Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit 3ed1f6a

Browse files
committed
update netconf and netmiko connections
1 parent 3ccb7a8 commit 3ed1f6a

6 files changed

Lines changed: 70 additions & 11 deletions

File tree

netnir/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
initialize netnir by looking for the netnir config. If the netnir config doesn't exist,
3-
netnir will create the default config and folders.
2+
initialize netnir by looking for the netnir config. If the netnir config
3+
doesn't exist, netnir will create the default config and folders.
44
"""
55

6-
__version__ = "0.0.17"
6+
__version__ = "0.0.18"

netnir/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ class Cli:
1212

1313
def __init__(self):
1414
"""
15-
A class object used to setup the netnir cli, consume the available commands from plugins,
16-
display the available commands, and execute the available commands based on user input.
15+
A class object used to setup the netnir cli, consume the available
16+
commands from plugins, display the available commands, and execute
17+
the available commands based on user input.
1718
"""
1819
from netnir.helpers import plugins_import
1920

netnir/core/tasks/netconf.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
netconf_filter,
55
netconf_source,
66
netconf_target,
7+
netconf_capabilities,
78
config,
89
)
910

@@ -18,19 +19,28 @@ def parser(parser):
1819
netconf_filter(parser)
1920
netconf_source(parser)
2021
netconf_target(parser)
22+
netconf_capabilities(parser)
2123
config(parser)
2224

2325
def run(self):
2426
"""execute netconf commands
2527
2628
:returns: nornir Result object
2729
"""
28-
from netnir.plugins.netconf import netconf_get_config, netconf_edit_config
30+
from netnir.plugins.netconf import (
31+
netconf_get_config,
32+
netconf_edit_config,
33+
netconf_capabilities,
34+
)
2935
from nornir.plugins.functions.text import print_result
3036

3137
self.nr = self._inventory()
3238

33-
if self.args.config:
39+
if self.args.capabilities:
40+
results = self.nr.run(
41+
task=netconf_capabilities, name="NETCONF SERVER CAPABILITIES",
42+
)
43+
elif self.args.config:
3444
results = self.nr.run(
3545
task=netconf_edit_config,
3646
name="NETCONF EDIT CONFIG",

netnir/helpers/common/args.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,17 @@ def netconf_config(parser, required: bool = False):
157157
help="yang config file that defines what to fetch from a device or edit on a device",
158158
required=required,
159159
)
160+
161+
162+
def netconf_capabilities(parser, required: bool = False):
163+
"""
164+
common argument to retrieve the netconf capabilities from a netconf server
165+
via netconf with the --capabilities flag.
166+
"""
167+
parser.add_argument(
168+
"--capabilities",
169+
help="retrieve netconf capabilities from a netconf server",
170+
required=required,
171+
nargs="?",
172+
const=True,
173+
)

netnir/plugins/netconf.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33
from ncclient import manager
44

55

6+
def netconf_capabilities(task: Task) -> Result:
7+
"""nornir get netconf capabilities
8+
9+
:params task: type object
10+
:returns: nornir result object
11+
"""
12+
device_params = {
13+
"host": task.host.hostname,
14+
"port": task.host.port or 830,
15+
"username": task.host.username,
16+
"password": task.host.password,
17+
"hostkey_verify": False,
18+
"device_params": {
19+
"name": device_mapper(os_type=task.host.data["os"], proto="netconf")
20+
},
21+
}
22+
23+
with manager.connect(**device_params) as conn:
24+
results = [capability for capability in conn.server_capabilities]
25+
26+
return Result(host=task.host, result=results)
27+
28+
629
def netconf_get_config(
730
task: Task,
831
source: str = "running",

netnir/plugins/netmiko.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@ def netmiko_send_commands(task: Task, commands: list()) -> Result:
1111
1212
:returns: nornir Result object
1313
"""
14+
try:
15+
secret = task.host.connection_options["netmiko"]["extras"]["secret"]
16+
except KeyError:
17+
secret = None
18+
1419
device_params = {
1520
"host": task.host.hostname,
1621
"device_type": device_mapper(os_type=task.host.platform, proto="netmiko"),
1722
"port": task.host.port,
1823
"username": task.host.username,
1924
"password": task.host.password,
20-
"secret": task.host.connection_options["netmiko"]["extras"].get("secret", None),
25+
"secret": secret,
26+
"ssh_config_file": task.nornir.config.ssh.config_file or None,
2127
}
28+
output = str()
2229

2330
with ConnectHandler(**device_params) as conn:
24-
output = str()
25-
2631
for command in commands:
2732
output += conn.send_command(command)
2833

@@ -37,13 +42,19 @@ def netmiko_send_config(task: Task, commands: list()) -> Result:
3742
3843
:returns: nornir Result object
3944
"""
45+
try:
46+
secret = task.host.connection_options["netmiko"]["extras"]["secret"]
47+
except KeyError:
48+
secret = None
49+
4050
device_params = {
4151
"host": task.host.hostname,
4252
"device_type": device_mapper(os_type=task.host.platform, proto="netmiko"),
4353
"port": task.host.port,
4454
"username": task.host.username,
4555
"password": task.host.password,
46-
"secret": task.host.connection_options["netmiko"]["extras"].get("secret", None),
56+
"secret": secret,
57+
"ssh_config_file": task.nornir.config.ssh.config_file or None,
4758
}
4859

4960
with ConnectHandler(**device_params) as conn:

0 commit comments

Comments
 (0)