Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_napalm_configure_change_commit(self, nornir):
opt = {"path": THIS_DIR + "/test_napalm_configure_change_commit/step1"}
configuration = "hostname changed-hostname"
d = nornir.filter(name="dev3.group_2")
d.run(connections.napalm_connection, optional_args=opt)
result = d.run(
networking.napalm_configure, dry_run=False, configuration=configuration
)
assert result
for h, r in result.items():
assert "+hostname changed-hostname" in r.diff
assert r.changed
opt = {"path": THIS_DIR + "/test_napalm_configure_change_commit/step2"}
d.run(connections.napalm_connection, optional_args=opt)
result = d.run(
networking.napalm_configure, dry_run=True, configuration=configuration
)
assert result
for h, r in result.items():
assert "+hostname changed-hostname" not in r.diff
assert not r.changed
def remove_ex2_flash_files():
# prep to ensure test files do not exist on devices
nornir_inventory = gen_inventory_dict("~/nornir_inventory/")
nr = InitNornir(inventory=nornir_inventory, logging=NORNIR_LOGGING)
eos = nr.filter(F(groups__contains="eos"))
# remove test files from eos flash
eos.run(task=networking.netmiko_send_command, command_string="terminal dont-ask")
eos.run(
task=networking.netmiko_send_command,
command_string="delete flash:arista_zzzzz.txt",
)
nr = InitNornir(config_file="config.yaml")
nr = nr.filter(name="arista4")
# Current running config
agg_result = nr.run(
task=networking.napalm_get, getters=["config"], retrieve="running"
)
arista4_result = agg_result["arista4"][0].result
arista4_running_config = arista4_result["config"]["running"] # noqa
# New config
config = """
interface loopback123
description verycoolloopback
"""
agg_result = nr.run(task=networking.napalm_configure, configuration=config)
print_result(agg_result)
task.run(task=networking.netmiko_send_command, command_string=cmd)
except NornirSubTaskError as e:
if isinstance(e.result.exception, NetMikoAuthenticationException):
# Remove the failed task (so ultimately the Nornir print output is cleaner)
task.results.pop()
# For failed devices reset the password to the correct value using environment var
task.host.password = os.environ["NORNIR_PASSWORD"]
# Force Nornir to close stale connections
try:
task.host.close_connections()
except ValueError:
pass
task.run(task=networking.netmiko_send_command, command_string=cmd)
else:
return f"Unhandled exception: {e}"
def send_command(task):
task.run(
task=networking.netmiko_send_command,
command_string="set cli complete-on-space off",
)
task.run(task=networking.netmiko_send_command, command_string="show ip interface")
def send_command(task):
task.run(
task=networking.netmiko_send_command,
command_string="set cli complete-on-space off",
)
mul_result = task.run(
task=networking.netmiko_send_command, command_string="show ip interface"
)
if "syntax error" in mul_result.result:
raise ValueError("Invalid Junos command")
def main():
nr = InitNornir(config_file="config_a.yaml")
nr = nr.filter(~F(name__contains="localhost"))
agg_result = nr.run(
task=networking.netmiko_send_command, command_string="show version"
)
print_result(agg_result)
def my_task(task):
changed = False
if task.host.groups[0] == "ios":
cmd = "show run | i hostname"
task.run(task=networking.netmiko_send_command, command_string=cmd)
changed = True
elif task.host.groups[0] == "junos":
cmd = "show configuration system host-name "
task.run(task=networking.netmiko_send_command, command_string=cmd)
changed = True
return Result(host=task.host, changed=changed, failed=False, result="ran command")