cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
88
Views
0
Helpful
0
Replies
Highlighted

Netmiko is not finding the prompt when entering banner exec for cisco IOS devices

Hi Stackoverflow community!

Netmiko hasn't been stable for me today so I opted to upgrade to version 3.0.0. While its stability has increased, I noticed that I could no longer send banner exec lines to cisco devices. I was able to deploy this successfully to almost 100 devices yesterday.

Here is a simplified version of code that i'm troubleshooting.

    import threading
    import time
    import re
    import pdb
    import difflib
    from netmiko import NetMikoAuthenticationException
    from netmiko import ConnectHandler
    from datetime import datetime
    import logging
    logging.basicConfig(filename='test.log', level=logging.DEBUG)
    logger = logging.getLogger("netmiko")


    class MyThread(threading.Thread

        def run(self
            date = datetime.now()
            date_file = date.strftime("%Y-%b-%d_%H%M%S")
            print("{} started!".format(self.getName()))
            IP = self.getName().split(" ")[-1]

            my_device = {
                "host": IP,
                "username": "cisco_ios_user",
                "password": "cisco_ios_pwd",
                "device_type": "cisco_ios",
                "secret": "cisco_ios_pwd"
            }

            try:

                print("--- Connecting to device " + IP)
                net_connect = ConnectHandler(**my_device)
                net_connect.enable()
                hostname = net_connect.find_prompt().rstrip(">").rstrip("#").rstrip(">t")
                print("        Connected to " + hostname)
                location = "Central_1"
                serial = "FTX0945W0MY"

                config = ""
                #config = config + "\n!"
                config = config + "\nbanner exec ^"
                config = config + "\nSite Location: " + location
                config = config + "\nHostname: " + hostname
                config = config + "\nModel: 3725"
                config = config + "\nChassis Serial Number: " + serial
                config = config + "\nAsset: None"
                config = config + "\n^"
                config = config + "\n"

                print("Config is:" + config)

                log = net_connect.send_config_set(config)
                print(log)

                for line in config.splitlines():
                    print(line)


                print("{} finished!".format(self.getName()))




            except NetMikoAuthenticationException:
                print("ERROR for " + IP + ": Authentication Error @ " + date_file)
                print('# ' * 40 + "\n")
                f = open("run_summary.txt", "a")
                f.write("\n" + "unknown, " + IP + ", " + "AuthenticationError, " + date_file)
                f.close()





    def main():
        with open('inventory.txt') as IP_LIST:
            for IP in IP_LIST:
                IP = IP.rstrip("\n")
                my_thread = MyThread(name="Script for device {}".format(IP))
                my_thread.start()
                time.sleep(300)


    if __name__ == '__main__':
        main()

Output:

    --- Connecting to device 192.168.163.101
            Connected to R1_Router
    Config is:
    banner exec ^
    Site Location: Central_1
    Hostname: R1_Router
    Model: 3725
    Chassis Serial Number: FTX0945W0MY
    Asset: None
    ^

    Exception in thread Script for device 192.168.163.101:
    Traceback (most recent call last):
      File "C:\Users\user\PycharmProjects\untitled\venv\lib\site-packages\paramiko\channel.py", line 699, in recv
        out = self.in_buffer.read(nbytes, self.timeout)
      File "C:\Users\user\PycharmProjects\untitled\venv\lib\site-packages\paramiko\buffered_pipe.py", line 164, in read
        raise PipeTimeout()
    paramiko.buffered_pipe.PipeTimeout

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:\Users\user\PycharmProjects\untitled\venv\lib\site-packages\netmiko\base_connection.py", line 541, in _read_channel_expect
        new_data = self.remote_conn.recv(MAX_BUFFER)
      File "C:\Users\user\PycharmProjects\untitled\venv\lib\site-packages\paramiko\channel.py", line 701, in recv
        raise socket.timeout()
    socket.timeout

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 926, in _bootstrap_inner
        self.run()
      File "C:/Users/user/PycharmProjects/untitled/network modules/2020-02-01 - Threading/multiline send_config.py", line 54, in run
        log = net_connect.send_config_set(config)
      File "C:\Users\user\PycharmProjects\untitled\venv\lib\site-packages\netmiko\base_connection.py", line 1726, in send_config_set
        new_output = self.read_until_pattern(pattern=re.escape(cmd.strip()))
      File "C:\Users\user\PycharmProjects\untitled\venv\lib\site-packages\netmiko\base_connection.py", line 618, in read_until_pattern
        return self._read_channel_expect(*args, **kwargs)
      File "C:\Users\user\PycharmProjects\untitled\venv\lib\site-packages\netmiko\base_connection.py", line 552, in _read_channel_expect
        "Timed-out reading channel, data not available."
    netmiko.ssh_exception.NetmikoTimeoutException: Timed-out reading channel, data not available.

I debugged this and it looks like it is waiting for the R1_Router# prompt. It might not be getting the prompt it expects because the prompt after entering "banner exec ^" is "Enter TEXT message. End with the character '^'." . But I could be wrong or this might be a red herring.

    DEBUG:netmiko:_read_channel_expect read_data: R1_Router(config)#
    DEBUG:netmiko:Pattern found: # 

    R1_Router(config)#
    DEBUG:netmiko:write_channel: b'banner exec ^\n'
    DEBUG:netmiko:Pattern is: banner\ exec\ \^
    DEBUG:netmiko:_read_channel_expect read_data: b
    DEBUG:netmiko:_read_channel_expect read_data: anner exe
    DEBUG:netmiko:_read_channel_expect read_data: c ^

    Enter TEXT message.  End with the character '^'.


    DEBUG:netmiko:Pattern found: banner\ exec\ \^ banner exec ^

    Enter TEXT message.  End with the character '^'.


    DEBUG:netmiko:Pattern is: (?:R1_Router|#)

Not sure if the netmiko package upgrade is the cause. My paramiko version is currently 2.6.0 and there is a 2.7.1 available but I haven't tried it yet. Any thoughts on how this could be solved would be greatly appreciated. Thank you in advance.

0 REPLIES 0
CreatePlease to create content
Content for Community-Ad