python - Telnetlib 不会因大量输出而退出
问题描述
我正在尝试show tech-support
从仅使用的多个设备中收集telnetlib
。它适用于一个小问题。它收集show tech-support
命令的完整输出并将其导出到文本文件(export_text_support()
是一个简单的with open()
语句)。
我实验室中两个开关的全部输出是命令32,768
的16,325
行show 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 分钟)
解决方案
当我们提取大量输出和配置时,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)
推荐阅读
- java - Java:如何在滚动和打印文本到页面末尾时打破 while 循环
- r - 如何订购堆叠条形图
- virtual-machine - Travis CI 中使用的虚拟机的硬件和软件设置是什么?
- wordpress - 在各处使用过滤器隐藏具有特定类别术语的帖子
- c++ - 为什么我的包裹前面有一堆╠?
- node.js - 通过 SSL 在子域上服务 Nodejs Socketio:WebSocket 握手期间出错:意外的响应代码:302
- android - Not controll on No device found 模态
- python - python更新kivy的表
- html - 动态生成内容的 CSS 列后备
- java - 我无法通过改装从 Kitsu Api 获得动漫列表