Python telnet读取了很长的输出问题

问题描述 投票:0回答:1

这是我的telnet脚本,为:

import sys
import getpass
import telnetlib
import time

HOST = "192.168.182.129"

user = input("Enter your remote  telnet account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
 tn.read_until(b"Password: ")
 tn.write(password.encode('ascii') + b"\n")

tn.write(b"show version\n")
time.sleep(5)
tn.write(b"config t\n")
time.sleep(2)
tn.write(b"interface loopback 1\n")
time.sleep(2)
tn.write(b"ip address 8.8.8.8 255.255.255.0\n")
tn.write(b"end\n")
tn.write(b"exit\n")

print(tn.read_all().decode('ascii'))

这里,如果我使用一些较长的输出命令作为“显示版本”,则此脚本无法正常运行,它将只显示几行输出,并将下一个命令剪切为:

Enter your remote  telnet account: deepak
Password:

R1#show version
Cisco IOS Software, 7200 Software (C7200-ADVIPSERVICESK9-M), Version 15.2(4)S5, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2014 by Cisco Systems, Inc.
Compiled Thu 20-Feb-14 06:51 by prod_rel_team

ROM: ROMMON Emulation Microcode
BOOTLDR: 7200 Software (C7200-ADVIPSERVICESK9-M), Version 15.2(4)S5, RELEASE SOFTWARE (fc1)

R1 uptime is 2 hours, 16 minutes
System returned to ROM by unknown reload cause - suspect boot_data[BOOT_COUNT] 0x0, BOOT_COUNT 0, BOOTDATA 19
System image file is "tftp://255.255.255.255/unknown"
Last reload reason: Unknown reason



This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, **distributors and users are responsible for**

**R1#onfig t**

% Invalid input detected at '^' marker.

R1#interface loopback 1

% Invalid input detected at '^' marker.

R1#ip address 8.8.8.8 255.255.255.0

% Invalid input detected at '^' marker.

R1#end

期待您的帮助。

python python-3.5 cisco telnetlib
1个回答
0
投票

而不是:

© www.soinside.com 2019 - 2024. All rights reserved.