【问题标题】:Python telnet read a long output issuePython telnet 读取长输出问题
【发布时间】:2020-04-18 15:35:24
【问题描述】:

这是我的 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

寻求您的帮助。

【问题讨论】:

  • 抓到网络上的telnet数据包后找到了解决办法。似乎我的路由器在“显示版本”cmd中显示“更多”并要求下一页“空格”,但不幸的是它没有给出。我将一个新命令称为“Terminal len 0”,问题已得到解决。

标签: python python-3.5 cisco telnetlib


【解决方案1】:

代替:

tn.write(b"config t\n")

尝试使用:

tn.write("config t\n")

压缩脚本的更好方法是将要配置的命令存储到这样的列表中:

commands =['conf t',  'interface loopback 1', 'ip address 8.8.8.8 255.255.255.0']
for command in commands:
    tn.write(command+'\n')
    time.sleep(1)

【讨论】:

    【解决方案2】:

    在你的代码的最后你改变了一些这样的东西,但首先要确定你的路由器:

    确保在您的路由器中配置:

    • aaa new-model (with : aaa authentication login default local)
    • 同样在 vty 0 15 行:传输输入全部,登录验证默认
    # if you configured enable password you should use this section :
    tn.write(b"enable\n")
    tn.write(b"cisco\n")
    # Code for test :
    tn.write(b"des TEST\n")
    tn.write(b"end\n")
    tn.write(b"exit\n")
    # **End section should be exactly like this line**
    print(tn.read_all())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 2021-08-09
      • 2014-07-11
      • 1970-01-01
      • 2011-07-15
      相关资源
      最近更新 更多