gpt4 book ai didi

python - 从 telnetlib python 3 获取重复的接收计数器输出

转载 作者:行者123 更新时间:2023-12-01 00:38:43 26 4
gpt4 key购买 nike

我通过访问设备从 telnetlib 获得覆盖的输出,但我不知道如何解决它以获得正确的输出。

我尝试了以下代码:

import re

import os
import sys
import time
import telnetlib

def telnet_dut():
tn1 = telnetlib.Telnet('10.22.12.98')
print(tn1)
tn1.read_until(b"AB-OS login:")
tn1.write('adm'.encode('ascii') + b"\n")
tn1.read_until(b"Password:")
tn1.write('adm'.encode('ascii') + b"\n")
time.sleep(2)
tn1.read_until(b"AB-OS")
tn1.write('sh int gi 0/10'.encode('ascii') + b"\n")
output = str(tn1.read_until(b"AB-OS login:").decode('ascii'))
file1 = open('PATH_TO_FILE/sample.txt' , 'w+')
file1.write(output)
file1.close()
tn1.close()

预期输出:

Recept Counters Oct : 64

Transmit Counters Oct : 64

实际输出:

Recept Counters^M^M Oct : 64^M^M

^[[100B^M^[[K^M--More--^[[K^M Idle Timer expired, Timing Out !!! ^M

Recept Counters^M^M

Oct : 64^M

^M Transmit Counters^M Oct : 64^M

最佳答案

默认分页对您没有任何好处。禁用它,这样您就不会在路由器时遇到超时问题等待您按空格键。

替换:

    tn1.read_until(b"AB-OS")

与:

    tn1.read_until(b"AB-OS")
tn1.write(b"set terminal length 0\n")
tn1.read_until(b"AB-OS")

关于python - 从 telnetlib python 3 获取重复的接收计数器输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57539294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com