daquezada
daquezada

Reputation: 1037

Netmiko: AttributeError: 'NoneType' object has no attribute 'recv_ready'

I am trying to connect to Cisco Router to pull some information and I am receiving the following error when I run my code:

Traceback (most recent call last): File "./cisco_auto_back_up_v4.py", line 72, in redispatch(net_connect, device_type=target_device['device_type']) File "/home/ipautowppprod/.pyenv/versions/3.6.5/lib/python3.6/site-packages/netmiko/ssh_dispatcher.py", line 208, in redispatch obj.session_preparation() File "/home/ipautowppprod/.pyenv/versions/3.6.5/lib/python3.6/site-packages/netmiko/cisco/cisco_ios.py", line 16, in session_preparation self._test_channel_read(pattern=r'[>#]') File "/home/ipautowppprod/.pyenv/versions/3.6.5/lib/python3.6/site-packages/netmiko/base_connection.py", line 791, in _test_channel_read new_data += self._read_channel_timing() File "/home/ipautowppprod/.pyenv/versions/3.6.5/lib/python3.6/site-packages/netmiko/base_connection.py", line 494, in _read_channel_timing new_data = self.read_channel() File "/home/ipautowppprod/.pyenv/versions/3.6.5/lib/python3.6/site-packages/netmiko/base_connection.py", line 395, in read_channel output = self._read_channel() File "/home/ipautowppprod/.pyenv/versions/3.6.5/lib/python3.6/site-packages/netmiko/base_connection.py", line 373, in _read_channel if self.remote_conn.recv_ready(): AttributeError: 'NoneType' object has no attribute 'recv_ready'

Here is my code:

    #!/home/ipautowppprod/.pyenv/shims/python

# cisco_auto_back_up_v4

from netmiko import ConnectHandler, redispatch
from netmiko import NetMikoAuthenticationException, NetMikoTimeoutException
import datetime
import sys
import time

now = datetime.datetime.now()
time_now = now.strftime("%Y-%m-%d_%H:%M:%S")

target_info = sys.argv[1].split(',')
ipmon_info = sys.argv[2].split(',')

target_info = [x.strip() for x in target_info]
ipmon_info = [x.strip() for x in ipmon_info]

target_device = {
    'device_type': 'cisco_ios',
    'ip': target_info[0],
    "host": target_info[1],
    'username': target_info[2],
    'password': target_info[3],
    'secret': target_info[4]
}

ipmon = {
    'device_type': 'linux',
    'ip': ipmon_info[0],
    'username': ipmon_info[1],
    'password': ipmon_info[2]
}

try:
    print("Attempting to Connect...")
    # Connect to ipmon
    net_connect = ConnectHandler(**ipmon)
    print(net_connect.find_prompt())

    net_connect.write_channel("ssh {}@{}\n".format(target_device["username"],
                                                   target_device["ip"]))
    time.sleep(1)
    output = net_connect.read_channel()

    print(output)

    if "RSA" in output:
        net_connect.write_channel("yes\n")
        time.sleep(1)
        output = net_connect.read_channel()
        print(output)
    if "user" in output:
        net_connect.write_channel(target_device["username"] + "\n")
        time.sleep(1)
        output = net_connect.read_channel()
    if "password" in output:
        net_connect.write_channel(target_device["password"] + "\n")
        time.sleep(5)
        output = net_connect.read_channel()
    if "password" in output:
        print("Wrong credentials.")
        sys.exit(1)
    elif "refused" in output:
        net_connect.disconnect()
        print("Connection Refused")
    else:
        net_connect.disconnect()

    # Connect to device
    redispatch(net_connect, device_type=target_device['device_type'])
    CMD = net_connect.send_command_timing('show running-config')

    # Converts CMD output to a string
    cmd_output = str(CMD.stdout)

except NetMikoAuthenticationException as autom_err:
    print(autom_err)
    sys.exit(1)

except NetMikoTimeoutException as timeout_err:
    print(timeout_err)
    sys.exit(1)

Please advise.

Thanks in advance

Upvotes: 2

Views: 2671

Answers (1)

Kirk Byers
Kirk Byers

Reputation: 499

You might want to step through your code execution using a debugger, but it looks like when you get to the redispatch call, that your net_connect object is no longer connected (i.e. net_connect.remote_conn has been set to None).

This makes me think that the disconnect() in the elif or else clause was possibly called. You probably should raise an exception in those cases (or sys.exit() ) as your program won't be able to continue if either of those happen.

Upvotes: 0

Related Questions