Print_result function to print to file and not to console

Hi There,

Im trying to backup my running-configs of my huawei routers to a text file. Everything works fine except for the part where I decided to save it in text file. The below error message is what I receive:

TypeError: write() argument must be str, not AggregatedResult

My question is, is there a similar function like print_result() which prints the output to a .txt file instead of printing to the console?
If there’s none, then what other workarounds are there for me?

Thank you in advance for your help. Please find below my code.

from nornir import InitNornir
from nornir.plugins.tasks.networking import netmiko_send_command, netmiko_send_config
from nornir.plugins.functions.text import print_result

nr = InitNornir(config_file=“config.yaml”, dry_run = True)

results = nr.run(task = netmiko_send_command,command_string=“display curr”)

f = open(“BackUp.txt”, “w+”)
f.write(results)
f.close()

Hey @waisea

Read this section from the docs. I recommend using an iPython (or other) REPL to explore the structure of the AggregateResults and MultiResult objects.

To access the raw string results, you’d need to do something like this:

r = nr.run(task = netmiko_send_command,command_string=“display curr”)
for device, results in r.items():
    if not results[0].failed:
        print("{} config is:\n{}".format(device, results[0].result))

Why don’t you use the write_file task? Looks like a better fit for your use case.

Heh - yeah, that’s a much cleaner solution.

i am using the write_file task to write the output of the netmiko_send_command for show a show commnd,
however in the output i do not see the host name, though in the console i see the host name.

is there anyway to write the output of the entire console ouptut to text file…