Skip to content

Commit

Permalink
Merge pull request #154 from nornir-automation/ssh_port
Browse files Browse the repository at this point in the history
Proposed fix for SSH and API port mapping issues
  • Loading branch information
dbarrosop committed Jul 10, 2018
2 parents af948f2 + 7a5138e commit 631cbbe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions nornir/core/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ def password(self):

@property
def ssh_port(self):
"""Either ``nornir_ssh_port`` or 22."""
return self.get("nornir_ssh_port", 22)
"""Either ``nornir_ssh_port`` or ``None``."""
return self.get("nornir_ssh_port")

@property
def network_api_port(self):
Expand Down
30 changes: 25 additions & 5 deletions nornir/plugins/tasks/connections/napalm_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,31 @@ def napalm_connection(task=None, timeout=60, optional_args=None):
"timeout": timeout,
"optional_args": optional_args or host.get("napalm_options", {}),
}
if "port" not in parameters["optional_args"] and host.network_api_port:
parameters["optional_args"]["port"] = host.network_api_port
elif "port" not in parameters["optional_args"] and host.ssh_port:
parameters["optional_args"]["port"] = host.ssh_port
network_driver = get_network_driver(host.nos)

platform = host.nos
api_platforms = ["nxos", "eos", "iosxr", "junos"]
ssh_platforms = ["nxos_ssh", "ios"]

# If port is set in optional_args that will control the port setting (else look to inventory)
if "port" not in parameters["optional_args"]:
if platform in api_platforms and host.network_api_port:
parameters["optional_args"]["port"] = host.network_api_port
elif platform in ssh_platforms and host.ssh_port:
parameters["optional_args"]["port"] = host.ssh_port

# Setting host.nos to 'nxos' is potentially ambiguous
if platform == "nxos":
if not host.network_api_port:
if host.ssh_port or parameters["optional_args"].get("port") == 22:
platform == "nxos_ssh"

# Fallback for community drivers (priority api_port over ssh_port)
if platform not in (api_platforms + ssh_platforms):
if host.network_api_port:
parameters["optional_args"]["port"] = host.network_api_port
elif host.ssh_port:
parameters["optional_args"]["port"] = host.ssh_port

network_driver = get_network_driver(platform)
host.connections["napalm"] = network_driver(**parameters)
host.connections["napalm"].open()
7 changes: 3 additions & 4 deletions nornir/plugins/tasks/connections/netmiko_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ def netmiko_connection(task, **netmiko_args):
"""
host = task.host
parameters = {
"host": host.host,
"username": host.username,
"password": host.password,
"port": host.ssh_port,
"host": host.host, "username": host.username, "password": host.password
}
if host.ssh_port:
parameters["port"] = host.ssh_port

if host.nos is not None:
# Look device_type up in corresponding map, if no entry return the host.nos unmodified
Expand Down
7 changes: 3 additions & 4 deletions nornir/plugins/tasks/connections/paramiko_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ def paramiko_connection(task=None):
ssh_config.parse(f)

parameters = {
"hostname": host.host,
"username": host.username,
"password": host.password,
"port": host.ssh_port,
"hostname": host.host, "username": host.username, "password": host.password
}
if host.ssh_port:
parameters["port"] = host.ssh_port

user_config = ssh_config.lookup(host.host)
for k in ("hostname", "username", "port"):
Expand Down

0 comments on commit 631cbbe

Please sign in to comment.