首页 > 解决方案 > Telnetlib 不会因大量输出而退出

问题描述

我正在尝试show tech-support从仅使用的多个设备中收集telnetlib。它适用于一个小问题。它收集show tech-support命令的完整输出并将其导出到文本文件(export_text_support()是一个简单的with open()语句)。

我实验室中两个开关的全部输出是命令32,76816,325show tech-support。我得到了整个输出,一个小问题是会话tn.write(b"exit\n")在完成后没有退出调用。exec-timeout它在交换机上被击中时退出(当会话变为空闲时),这两个交换机都是 10 分钟,而不是当没有其他内容可从tn.

我在下面尝试了相同的代码以获得较短的输出,例如show running-config,我在导出的文件中看到了#exit(末尾有一个空行)。

短输出

...
365 !
366 end
367
368 Switch#
369 Switch#exit
370 

巨大的输出

....
16321 423 entries printed
16322 
16323 
16324 
16325 Switch#

(如您exit所见,在巨大的输出样本中没有看到,最后也没有空行)

这是我的片段

from telnetlib import Telnet


host = ""
username = ""
password = ""

def get_tech_support(host: str) -> None:

    with Telnet(host=host, port=23) as tn:
        # Check for credentials
        if username:
            tn.read_until(b"Username: ")
            tn.write(username.encode("ascii") + b"\n")
        if password:
            tn.read_until(b"Password: ")
            tn.write(password.encode("ascii") + b"\n")
        
        # Some commands
        commands = ["\n", "terminal width 115", "terminal length 0"]
        [tn.write(command.encode("ascii") + b"\n") for command in commands]
        
        # The main command
        cmd = "show tech-support"

        # Send the main command
        tn.write(cmd.encode("ascii") + b"\n")

        hostname = tn.read_until(b"#")

        tn.write(b"\nexit\n")
        command_output = tn.read_all().decode("ascii")

    result = dict(
        ip=host, hostname=hostname, command=cmd, command_output=command_output
    )

    export_tech_support(command_output=result)  # Export the show command output

如何在完成详细输出后自动退出会话并避免等待exec-timeout被击中(在我的情况下为 10 分钟)

标签: pythonparamikociscotelnetlibnetmiko

解决方案


当我们提取大量输出和配置时,telnetlib 和 paramiko 存在问题。

这是由于在运行脚本时控制台连接关闭而发生的,因此您必须寻找解决方案来维护控制台连接。我建议使用 netmiko,因为 netmiko 版本 >= 1.0 现在也支持 Telnet。

对于示例,您可以检查此脚本:

from pprint import pprint
import yaml
from netmiko import (
    ConnectHandler,
    NetmikoTimeoutException,
    NetmikoAuthenticationException,
)


def send_show_command(device, commands):
    result = {}
    try:
        with ConnectHandler(**device) as ssh:
            ssh.enable()
            for command in commands:
                output = ssh.send_command(command)
                result[command] = output
        return result
    except (NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        print(error)


if __name__ == "__main__":
    device = {
        "device_type": "cisco_ios_telnet", #Refer netmiko device type
        "host": "192.168.100.1",
        "username": "cisco",
        "password": "cisco123",
        "secret": "cisco",  #if not required remove this
    }
    result = send_show_command(device, ["sh clock", "sh ip int br"])
    pprint(result, width=120)

推荐阅读