简体   繁体   English

Netmiko 在为 cisco IOS 设备输入横幅 exec 时未找到提示

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

Hi Stackoverflow community!嗨,Stackoverflow 社区!

Netmiko hasn't been stable for me today so I opted to upgrade to version 3.0.0. Netmiko 今天对我来说不稳定,所以我选择升级到 3.0.0 版。 While its stability has increased, I noticed that I could no longer send banner exec lines to cisco devices.虽然它的稳定性有所提高,但我注意到我无法再将横幅 exec 行发送到 cisco 设备。 I was able to deploy this successfully to almost 100 devices yesterday.昨天我能够成功地将它部署到近 100 台设备。

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.我调试了它,看起来它正在等待 R1_Router# 提示。 It might not be getting the prompt it expects because the prompt after entering "banner exec ^" is "Enter TEXT message. End with the character '^'."它可能没有得到预期的提示,因为输入“banner exec ^”后的提示是“输入文本消息。以字符'^'结尾。” . . 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.不确定 netmiko 软件包升级是否是原因。 My paramiko version is currently 2.6.0 and there is a 2.7.1 available but I haven't tried it yet.我的 paramiko 版本目前是 2.6.0,有一个 2.7.1 可用,但我还没有尝试过。 Any thoughts on how this could be solved would be greatly appreciated.关于如何解决这个问题的任何想法将不胜感激。 Thank you in advance.先感谢您。 :) :)

That is very probably this issue:这很可能是这个问题:

https://github.com/ktbyers/netmiko/issues/1531 https://github.com/ktbyers/netmiko/issues/1531

You will need to use Netmiko 2.4.2 until I put in a fix for that.您将需要使用 Netmiko 2.4.2,直到我对此进行修复。 Hopefully, I can get a fix into the develop branch in a week or two.希望我能在一两周内修复开发分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM