Replies: 2 comments 3 replies
-
Not sure if you ever got this figured out, but I came across your code while looking for a similar solution. I made some changes to the get_version function that resulted in a functional script:
The function now checks to see if the host has already been tried for each credential set in our list, and once we find a good set we append it to the list and bypass it on future tries. We also return() the output of the function instead of quit() to make sure we iterate through the entire set of hosts and all credential sets in the list, and only raise an error if no credentials in the list return a result. I'm still a novice so this method likely isn't the most efficient. Feedback welcome. Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
I am using multiple credentials for login from many years but just saw this question. Here is the code # import creentials
from secret.credentials import credentials
# progress bar
from tqdm import tqdm
# create directory
from pathlib import Path
# importing nornir and related plugins
from nornir import InitNornir
from nornir_netmiko.tasks import netmiko_send_command
from nornir_utils.plugins.functions import print_result
from nornir_utils.plugins.tasks.files import write_file
from nornir_utils.plugins.inventory.yaml_inventory import ConnectionOptions
#import exceptions
from paramiko.ssh_exception import AuthenticationException
from netmiko.exceptions import NetmikoAuthenticationException
from nornir.core.exceptions import NornirSubTaskError
import ipdb
from datetime import datetime, timezone
import subprocess
import os
from prettytable import PrettyTable
import requests
import json
import pandas as pd
# preparing login to the hosts
def backup(task, progress_bar):
for cred in credentials:
try:
task.host.username = cred['user']
task.host.password = cred['pass']
task.host.connection_options['netmiko'] = ConnectionOptions(extras={'secret': cred['secret']})
site = task.host.data['pynautobot_dictionary']['site']['name']
# directory check if exist else create
Path(f"{dir_path}/{site}").mkdir(parents=True, exist_ok=True)
# Task 1. Run the Netmiko config getter to collect the config
device_config = task.run(name="Fetching Running Config", task=netmiko_send_command, command_string="show running-config", enable=True, read_timeout=900)
# Task 2. Write the device config to a file using the Nornir, write_file task
task.run(
task=write_file,
content=device_config.result,
filename=f"{dir_path}/{site}/{task.host}.txt",)
# Task 3. Update success
task.host['fact_status'] = "Backup Complete"
progress_bar.update()
# login successfull
break
except AuthenticationException or NetmikoAuthenticationException:
continue
except NornirSubTaskError:
continue
except:
continue
if __name__=="__main__":
dir_path = "/root/plugin/configuration-backup/running-config"
nr = InitNornir(
inventory={
"plugin": "NautobotInventory",
"options": {
"nautobot_url": "http://",
"nautobot_token": "",
"ssl_verify": False,
},
},
runner={
"plugin": "threaded",
"options": {'num_workers': 30}
},
logging={
"enabled": False
},
)
with tqdm(total=len(nr.inventory.hosts)) as progress_bar:
results = nr.run(task=backup, progress_bar=progress_bar)
|
Beta Was this translation helpful? Give feedback.
-
Hello,
I'm wondering if someone can help me understand what I am doing wrong.
I have a number of devices and a list of credentials. I would like to use nornir to test the credentials against a device and once a credential is successful stop any further attempts.
So far, I am able to go through a list and receive a login authentication error for all the failed attempts, but when it finds a successful match, it continues to run the task again and any further attempts report a success even though it is using a previous login cred that had failed. In this example, test fails and good works, by third run of the task test credentials report as successful. I figured the "on_good=True" would prevent any subsequent runs of the task.
Thank you.
Beta Was this translation helpful? Give feedback.
All reactions