Error While exiting Configuration mode using Python Script

172 views Asked by At

It's been long since I opened stack websites. please ignore any mistakes.

I am using telnetlib library to login into my Multilayer switch on GNS3. I am Successfully able to configure loopback interfaces using script and execute show commands in it, including the show commands on configuration mode. Example : "do sh ip int br" OR "do sh run".

The issue is when my script does it's configuration and tries to exit then it is not exiting and the code is stuck.

I have to manually login into device and clear the vty line session to get the output.

Catch is : for both commands "exit" and "end" the code is removing the initial "e" which is happening only to the last "exit" and/or "end.

Below is the code :

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'))

Output of switch 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#

Output of Windows-command line/ When I execute the script :

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#

I have also tried to add space, extra "e" in the last exit command. however still getting the same error Please let me know if I am missing something/anything.

Environment details: Python 3.10.6 GNS3 2.2.21 Switch is L3 Multilayer Switch and is IOS router running on local PC as server

PC is connected to GNS3 via loopback interface on PC and cloud on GNS3

Topology on GNS3 : Multi-layer Switch=======L2Switch=====Cloud

The same script is working fine with Cisco Router-7200

1

There are 1 answers

0
Mystery On BEST ANSWER

Resolved by encoding it to string

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