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

2024-04-20 10:57:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在通过访问一个设备从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


Tags: importreadabtimeosasciifile1oct
1条回答
网友
1楼 · 发布于 2024-04-20 10:57:34

默认分页对您没有任何帮助。 禁用它,这样你就不会遭受超时,而路由器 等待你点击空格键

替换:

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

相关问题 更多 >