Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def uptime(task):
cmd_mapper = {
"ios": "show version | inc uptime",
"eos": "show version | inc Uptime",
"nxos": "show version | inc uptime",
"junos": "show system uptime | match System",
}
host = task.host
platform = host.platform
cmd = cmd_mapper[platform]
multi_result = task.run(task=networking.netmiko_send_command, command_string=cmd)
print(multi_result)
from nornir.core import InitNornir
from nornir.plugins.tasks.networking import netmiko_send_command
from nornir.plugins.functions.text import print_result
commands = input("Enter commands: ")
cmds = commands.split(",")
for cmd in cmds:
nr = InitNornir()
result = nr.run(
task=netmiko_send_command,
command_string=cmd
)
print_result(result)
def main():
nr = InitNornir(config_file="config.yaml")
ios_filt = F(groups__contains="ios")
nr = nr.filter(ios_filt)
my_results = nr.run(task=netmiko_send_command, command_string="show ip int brief")
print_result(my_results)
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 main():
nr = InitNornir(config_file="config.yaml")
ios_filt = F(groups__contains="ios")
nr = nr.filter(ios_filt)
# Set one of the devices to an invalid password
nr.inventory.hosts["cisco3"].password = "bogus"
my_results = nr.run(task=netmiko_send_command, command_string="show ip int brief")
print()
print_result(my_results)
print()
print(f"Task failed hosts: {my_results.failed_hosts}")
print(f"Global failed hosts: {nr.data.failed_hosts}")
print()
# Re-set password back to valid value
nr.inventory.hosts["cisco3"].password = os.environ["NORNIR_PASSWORD"]
# Nornir doesnt reset connection for failed hosts causing issues
print(nr.inventory.hosts["cisco3"].connections)
try:
nr.inventory.hosts["cisco3"].close_connections()
except ValueError:
pass
def log_send_command(task):
log_file = f"{task.host}_session.log"
# Obtain the group object using the "refs" attribute
group = task.host.groups.refs[0]
group.connection_options["netmiko"].extras["session_log"] = log_file
task.run(
task=networking.netmiko_send_command, command_string="show mac address-table"
)
def main():
nr = InitNornir(config_file="config.yaml")
nr = nr.filter(name="arista1")
agg_result = nr.run(
task=networking.netmiko_send_command,
command_string="show run | i hostname",
enable=True,
)
print(agg_result["arista1"].result)
def main():
nr = InitNornir(config_file="config.yaml")
filt = F(groups__contains="ios")
nr = nr.filter(filt)
my_results = nr.run(
task=netmiko_send_command, command_string="show run | i hostname"
)
host_results = my_results["cisco3"]
print()
print(type(host_results))
print(repr(host_results[0]))
print(host_results.__iter__)
print()
def main():
nr = InitNornir(config_file="config.yaml")
filt = F(groups__contains="ios")
nr = nr.filter(filt)
my_results = nr.run(
task=netmiko_send_command, command_string="show run | i hostname"
)
host_results = my_results["cisco3"]
task_result = host_results[0]
print()
print(type(task_result))
print(
f"Host: {task_result.host}\n"
f"Task Name: {task_result.name}\n"
f"Failed: {task_result.failed}\n"
f"Result: {task_result.result}"
)
print()
std_print(result)
# Reload
continue_func(msg="Do you want to reload the device (y/n)? ")
result = brg_ios.run(
netmiko_send_command,
use_timing=True,
command_string="reload",
num_workers=1,
)
# Confirm the reload (if 'confirm' is in the output)
for device_name, multi_result in result.items():
if 'confirm' in multi_result[0].result:
result = brg_ios.run(
netmiko_send_command,
use_timing=True,
command_string="y",
)
print("Devices reloaded")