Netmiko 在为 cisco IOS 设备输入 banner exec 时找不到提示

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

你好 Whosebug 社区!

Netmiko 今天对我来说还不稳定,所以我选择升级到 3.0.0 版。 虽然它的稳定性有所提高,但我注意到我无法再将横幅 exec 行发送到思科设备。昨天我能够成功地将它部署到近 100 台设备。

这是我正在排除故障的代码的简化版本。

    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()

输出:

    --- 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.

我对此进行了调试,看起来它正在等待 R1_Router# 提示。它可能没有得到预期的提示,因为输入 "banner exec ^" 后的提示是 "Enter TEXT message. End with the character '^'." 。但我可能是错的,或者这可能是一个转移注意力的问题。

    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|#)

不确定是否是 netmiko 包升级的原因。我的 paramiko 版本目前是 2.6.0,还有一个 2.7.1 可用,但我还没有尝试过。任何关于如何解决这个问题的想法将不胜感激。先感谢您。 :)

这很可能是这个问题:

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

在我修复之前,您需要使用 Netmiko 2.4.2。希望我能在一两周内修复开发分支。