【问题标题】:Error While exiting Configuration mode using Python Script使用 Python 脚本退出配置模式时出错
【发布时间】:2022-08-19 01:10:08
【问题描述】:

我已经很久没有打开堆栈网站了。 请忽略任何错误。

我正在使用 telnetlib 库登录到 GNS3 上的多层交换机。 我能够成功地使用脚本配置环回接口并在其中执行 show 命令,包括配置模式下的 show 命令。示例:\"do sh ip int br\" 或 \"do sh run\"。

问题是当我的脚本进行配置并尝试退出时,它没有退出并且代码被卡住。

我必须手动登录设备并清除 vty 线路会话才能获得输出。

问题是:对于两个命令 \"exit\" 和 \"end\",代码正在删除最初的 \"e\",这仅在最后一个 \"exit\" 和/或 \"end 中发生。

下面是代码:

import getpass
import sys
import telnetlib

HOST = input(\"Enter Device IP: \")
user = input(\"Enter your telnet username: \") 
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\"enable\\n\")
#tn.write(b\"xxpasswordxx\\n\")             
tn.write(b\"conf t\\n\")

tn.write(b\"int loop 2 \\n\")
tn.write(b\"ip address 1.1.1.2 255.255.255.255 \\r\\n\")


tn.write(b\"exit \\r\\n\")     **>>>>> This exit is working fine.**
tn.write(b\"do wr\\n\")

tn.write(b\"exit \\r\\n\")     **>>>>> This exit is working fine.**
tn.write(b\"sh ip int br \\n\")

tn.write(b\"end\\n\")         **>>>>> This exit/end is not working no matter if I use \"exit\" or \"end\", it removes initial \"e\"**

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

开关 CLI 的输出:

ESW1#
ESW1#
*Mar  1 03:00:07.635: %SYS-5-CONFIG_I: Configured from console by user1 on vty0 (20.20.20.6)
ESW1#sh users
    Line       User       Host(s)              Idle       Location
*  0 con 0                idle                 00:00:00
 162 vty 0     user1      idle                 00:00:22 20.20.20.6

  Interface    User               Mode         Idle     Peer Address

ESW1#clear line vty 0
[confirm]
 [OK]
ESW1#

Windows命令行的输出/当我执行脚本时:

Path:\\P_Project\\MyScript>python Telnet_1.py
Enter Device IP: 20.20.20.1
Enter your telnet username: user1
Password:


***************************************************************
This is a normal Router with a Switch module inside (NM-16ESW)
It has been pre-configured with hard-coded speed and duplex

To create vlans use the command \"vlan database\" in exec mode
After creating all desired vlans use \"exit\" to apply the config

To view existing vlans use the command \"show vlan-switch brief\"

Alias(exec)     : vl   - \"show vlan-switch brief\" command
Alias(configure): va X - macro to add vlan X
Alias(configure): vd X - macro to delete vlan X
***************************************************************


ESW1#enable
ESW1#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
ESW1(config)#int loop 2
ESW1(config-if)#ip address 1.1.1.2 255.255.255.255
ESW1(config-if)#exit
ESW1(config)#do wr
Building configuration...
[OK]
ESW1(config)#exit
ESW1#sh ip int br
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            20.20.20.1      YES manual up                    up
FastEthernet0/1            unassigned      YES NVRAM  administratively down down
FastEthernet1/0            unassigned      YES unset  up                    down
FastEthernet1/1            unassigned      YES unset  up                    down
FastEthernet1/2            unassigned      YES unset  up                    down
FastEthernet1/3            unassigned      YES unset  up                    down
FastEthernet1/4            unassigned      YES unset  up                    down
FastEthernet1/5            unassigned      YES unset  up                    down
FastEthernet1/6            unassigned      YES unset  up                    down
FastEthernet1/7            unassigned      YES unset  up                    down
FastEthernet1/8            unassigned      YES unset  up                    down

ESW1#nd
Translating \"nd\"      >>>> To print this error I have to execute the command \"clear line vty 0\" on switch (refer the output of switch CLI) 

Translating \"nd\"

% Unknown command or computer name, or unable to find computer address
ESW1#

我还尝试在最后一个退出命令中添加空间,额外的 \"e\"。但是仍然遇到相同的错误如果我遗漏了什么/什么,请告诉我。

环境细节: Python 3.10.6 GNS3 2.2.21 Switch 是 L3 多层交换机,是在本地 PC 上作为服务器运行的 IOS 路由器

PC通过PC上的环回接口和GNS3上的云连接到GNS3

GNS3上的拓扑:多层交换机=======L2Switch=====云

相同的脚本适用于 Cisco Router-7200

如果需要任何其他信息,请告诉我。

----- TIA

  • 通过更改数据类型解决了该问题:```

标签: network-programming python-3.10 telnetlib cisco-ios gns3


【解决方案1】:

通过将其编码为字符串来解决

tn.write(str.encode("exit\n"))

【讨论】:

    猜你喜欢
    • 2011-05-21
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2019-07-05
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多