【发布时间】: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