Я успешно могу настраивать интерфейсы обратной связи с помощью сценария и выполнять в нем команды show, включая команды show в режиме конфигурации. Пример: «do sh ip int br» ИЛИ «do sh run».
Проблема в том, что когда мой скрипт выполняет настройку и пытается выйти, он не выходит, и код зависает.
Мне приходится вручную войти в устройство и очистить сеанс линии vty, чтобы получить выходные данные.
Загвоздка: для обеих команд «exit» и «end» код удаляет начальную букву «e», которая происходит только до последнего «выхода» и/или «конца».
Ниже приведен код:
Код: Выделить всё
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'))
Код: Выделить всё
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#
Код: Выделить всё
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#
Пожалуйста, дайте мне знать, если я что-то/что-то упустил.
Подробности о среде:
Python 3.10.6
GNS3 2.2.21
Коммутатор представляет собой многоуровневый коммутатор L3 и маршрутизатор IOS работает на локальном ПК в качестве сервера
ПК подключен к GNS3 через интерфейс обратной связи на ПК и в облаке GNS3
Топология GNS3: многоуровневый коммутатор=======L2Switch=====Облако
Тот же сценарий отлично работает с Cisco Router-7200