This commit is contained in:
2025-11-27 11:59:36 +01:00
parent 39f8b87743
commit 558a4c30f7
15 changed files with 1525 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
from api import app
from nornir_napalm.plugins.tasks import napalm_get, napalm_configure, napalm_cli
from nornir.core.task import Task, Result
from nornir_utils.plugins.functions import print_result
def get_config_by_device(device):
nr = app.config.get('nr')
result = nr.filter(device_name=device.get('name')).run(task=napalm_get, getters=["get_config"])
return result[device.get('name')][0].result.get("get_config")
def run_config_from_file_by_device(device=None, file_path=None):
nr = app.config.get('nr')
if device and file_path:
result = nr.filter(device_name=device.get('name')).run(task=netmiko_send_config,
config_file=file_path)
print_result(result)
return result[device.get('name')].changed

View File

@@ -0,0 +1,70 @@
from api import app
from flask import Flask,jsonify,request,abort, make_response
from utils.inventory import *
from nornir import InitNornir
from nornir_utils.plugins.functions import print_result
from nornir_napalm.plugins.tasks import napalm_get,napalm_configure, napalm_cli
from nornir_netmiko.tasks import netmiko_send_config,netmiko_send_command, netmiko_save_config, netmiko_commit
def get_inventory():
hosts = app.config.get('nr').inventory.dict().get('hosts').keys()
return list(map(lambda item: app.config.get('nr').inventory.dict().get('hosts').get(item), hosts))
def add_device(device):
device = add_item_to_hosts_yaml(item=device)
return device
def get_device_by_name(name):
host = app.config.get('nr').inventory.hosts.get(name)
if not host:
abort(make_response(jsonify(message="device not found"), 404))
return host.dict()
def delete_device(device):
try:
delete_item_from_hosts_yaml(item=device)
except Exception as e:
abort(make_response(jsonify(message="Unable to delete this device", error=str(e)), 500))
def get_device_interfaces(device):
nr = app.config.get('nr')
result = nr.filter(device_name=device.get('name')).run(task=napalm_get,
getters=["get_interfaces"])
print_result(result)
print(result)
interfaces = result[device.get('name')][0].result.get('get_interfaces')
return list(map(lambda item: dict(name=item, **interfaces.get(item)), interfaces))
def get_device_technical_info(device):
nr = app.config.get('nr')
result = nr.filter(device_name=device.get('name')).run(
task=napalm_get,
getters=["get_facts"]
)
print_result(result)
facts = result[device.get('name')][0].result.get('get_facts')
return {
"hostname": facts.get("hostname"),
"vendor": facts.get("vendor"),
"model": facts.get("model"),
"serial_number": facts.get("serial_number"),
"os_version": facts.get("os_version"),
"uptime": facts.get("uptime"),
}
def get_device_interfaces_ip(device):
nr = app.config.get('nr')
result = nr.filter(device_name=device.get('name')).run(task=napalm_get,
getters=["get_interfaces_ip"])
print_result(result)
interfaces = result[device.get('name')][0].result.get('get_interfaces_ip')
def transformer(item):
ip_address = list(interfaces.get(item).get('ipv4').keys())[0]
prefix_length = interfaces.get(item).get('ipv4').get(list(interfaces.get(item).get('ipv4').keys())[0]).get('prefix_length')
return dict(name=item, ip=ip_address, prefix_length=prefix_length)
return list(map(lambda item: transformer(item), interfaces))
#return interfaces

View File

@@ -0,0 +1,35 @@
from api import app
from nornir_napalm.plugins.tasks import napalm_get, napalm_configure, napalm_cli
from nornir.core.task import Task, Result
from nornir_utils.plugins.functions import print_result
from nornir_netmiko.tasks import netmiko_send_config, netmiko_send_command, netmiko_save_config,netmiko_commit
def run_show_commands_by_device(device=None, commands=[]):
nr = app.config.get('nr')
commands_sent = []
if device:
if len(commands) > 1:
for command in commands:
result = nr.filter(device_name=device.get('name')).run(task=napalm_cli,
commands=[command])
print_result(result)
output = result[device.get('name')][0].result.get(command)
commands_sent.append(dict(command=command, result=output))
output = commands_sent
else:
result = nr.filter(device_name=device.get('name')).run(task=napalm_cli,
commands=commands)
output = result[device.get('name')][0].result.get(commands[0])
commands_sent.append(dict(command=commands[0], result= output))
output = commands_sent
return output
def run_config_commands_by_device(device=None, commands=[]):
nr = app.config.get('nr')
commands_sent = []
if device:
result = nr.filter(device_name=device.get('name')).run(task=netmiko_send_config,
config_commands=[commands])
print_result(result)
return result[device.get('name')].changed