Bob
Bob

Reputation: 1

loop through a list of switches with NAPALM

I have NAPALM install and working to get facts from a single device at a time. However, when I try to open a file that has multiple switches and iterative over them to get facts from each switch in the file, I get an error. The error seems to be related to looping over the file as opposed to having a specific host name parameter. Below is the code:

from napalm import get_network_driver
with open('fetch.txt') as f:
for line in f:
    driver = get_network_driver('ios')
    optional_args = {'secret': 'secret!'}
    device = driver('line', 'user', 'password',optional_args=optional_args)
    print device.get_facts()

I receive the following error, AttributeError: 'NoneType' object has no attribute 'send_command'. I'm not sure how to get NAPALM to loop through a text file of switches instead of one specific host name.

Upvotes: 0

Views: 409

Answers (1)

David Barroso
David Barroso

Reputation: 136

You never got to connect to the device. Either call device.open() or use the with keyword to enter in the context:

from napalm import get_network_driver
with open('fetch.txt') as f:
for line in f:
    driver = get_network_driver('ios')
    optional_args = {'secret': 'secret!'}
    device = driver('line', 'user', 'password',optional_args=optional_args)
    device.open()
    print device.get_facts()

or:

from napalm import get_network_driver
with open('fetch.txt') as f:
for line in f:
    driver = get_network_driver('ios')
    optional_args = {'secret': 'secret!'}
    with driver('line', 'user', 'password',optional_args=optional_args) as device:
        print device.get_facts()

I suspect there might be other problems like line, user, password are strings and not variables but that seems to be outside the scope of your question.

Upvotes: 1

Related Questions