napalm-automation / napalm-base

Apache License 2.0
32 stars 48 forks source link

is_alive() function in base.py is throwing an AttributeError even before open() is called #267

Closed ipv6freely closed 7 years ago

ipv6freely commented 7 years ago

Im getting an AttributeError exception in the is_alive() function that is used for garbage collection in napalm-base 0.24.1.

Code:

#!.venv/bin/python
import napalm
"""
napalm (1.2.0)
napalm-base (0.24.1)
napalm-junos (0.10.3)
"""

def napalm_this_bitch(password_list, name, ipaddress, napalm_driver):
    for password in password_list:
        print(f'{name:40} {ipaddress:20} {napalm_driver:10} {password:10}')
        driver = napalm.get_network_driver(napalm_driver)
        device = driver(hostname=ipaddress, username='admin', password=password, optional_args={'port': 22})

password_list = ['foo']
device_list = [{'name':'TESTDEVICE-1','ipaddress':'10.212.72.7','napalm_driver':'junos'}]
for device in device_list:
    napalm_this_bitch(password_list, device['name'], device['ipaddress'], device['napalm_driver'])

Output:

cjones@cjones-mbp$ python test.py
TESTDEVICE-1                             10.212.72.7          junos      foo
Exception ignored in: <bound method NetworkDriver.__del__ of <napalm_junos.junos.JunOSDriver object at 0x1021b9f98>>
Traceback (most recent call last):
  File "/Users/cjones/python/pwchanger/.venv/lib/python3.6/site-packages/napalm_base/base.py", line 69, in __del__
    if self.is_alive()["is_alive"]:
  File "/Users/cjones/python/pwchanger/.venv/lib/python3.6/site-packages/napalm_junos/junos.py", line 156, in is_alive
    'is_alive': self.device._conn._session.transport.is_active() and self.device.connected
AttributeError: 'NoneType' object has no attribute '_session'
ipv6freely commented 7 years ago

I just submitted a pull request with an easy fix.

ipv6freely commented 7 years ago

Re-submitted with an exception handler catching all.