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_getters(self, nornir):
opt = {"path": THIS_DIR + "/test_napalm_getters"}
d = nornir.filter(name="dev3.group_2")
d.run(connections.napalm_connection, optional_args=opt)
result = d.run(networking.napalm_get, getters=["facts", "interfaces"])
assert result
for h, r in result.items():
assert r.result["facts"]
assert r.result["interfaces"]
def main():
devices = InitNornir(
core={"num_workers": 100},
inventory={
"plugin": "nornir.plugins.inventory.simple.SimpleInventory",
"options": {
"host_file": "inventory/hosts.yaml",
"group_file": "inventory/groups.yaml"
}
}
)
results = devices.run(task=networking.napalm_get, getters=["get_bgp_neighbors_detail"])
bgp = dict()
for k, v in results.items():
bgp[k] = v.result["get_bgp_neighbors_detail"]["global"]
env = Environment(
loader=PackageLoader('render_bgp_topology', 'templates'),
autoescape=select_autoescape(['html'])
)
template = env.get_template('js.jinja')
rendered = template.render(netdev_bgp=bgp)
with open("bgp_topology.html", "w+") as fh:
fh.write(rendered)
def main():
nr = InitNornir(config_file="config.yaml")
nr = nr.filter(F(groups__contains="nxos"))
agg_result = nr.run(task=napalm_get, getters=["config"])
print_result(agg_result)
from pprint import pprint
from nornir import InitNornir
from nornir.plugins.tasks.networking import napalm_get
nr = InitNornir(config_file="nornir.yaml")
results = nr.run(task=napalm_get, getters=["lldp_neighbors"])
print()
for k, v in results.items():
print("-" * 50)
print(k)
pprint(v[0].result)
print("-" * 50)
print()
def main():
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)
from nornir import InitNornir
from nornir.plugins.tasks.networking import napalm_get
from pprint import pprint
nr = InitNornir(config_file="nornir.yaml")
results = nr.run(task=napalm_get, getters=["config"])
print()
for k, v in results.items():
print("-" * 50)
print(k)
pprint(v[0].result)
print("-" * 50)
print()
def main():
nr = InitNornir(config_file="config_b.yaml")
nr = nr.filter(F(groups__contains="nxos"))
agg_result = nr.run(task=networking.napalm_get, getters=["facts"])
print_result(agg_result)
# Assign facts directory to variable
fact_dir = "facts"
# Assign hostname directory to a variable
host_dir = task.host.name
# Assign the destination directory to a variable. i.e facts/hostname/
entry_dir = fact_dir + "/" + host_dir
# If Else to handle creating directories. Default is to create them
if dir is False:
# Create facts directory
pathlib.Path(fact_dir).mkdir(exist_ok=True)
# Create entry directory
pathlib.Path(entry_dir).mkdir(exist_ok=True)
# Try/except block to catch exceptions, such as NotImplementedError
try:
# Gather facts using napalm_get and assign to a variable
facts_result = task.run(task=napalm_get, getters=[getter])
# Write the results to a JSON, using the convention .json
task.run(
task=write_file,
content=json.dumps(facts_result[0].result[getter], indent=2),
filename=f"" + str(entry_dir) + "/" + str(getter) + ".json",
)
# Handle NAPALM Not Implemented Error exceptions
except NotImplementedError:
return "Getter Not Implemented"
except AttributeError:
return "AttributeError: Driver has no attribute"
def main():
nr = InitNornir(config_file="config.yaml")
nr = nr.filter(name="arista4")
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"]
file_name = "arista4-running.txt"
with open(file_name, "w") as f:
f.write(arista4_running_config)
print()
print("#" * 40)
print(arista4_running_config)
print("#" * 40)
print()