cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
4439
Views
15
Helpful
5
Replies

Telnet login timing

Andre Castro
Level 1
Level 1

Dear colleagues

I'm running a script to automate bulk commands across devices and some of them I can only access via telnet.

However it seems like the connect() functions sometimes misses the login time and ends up failing:

aix@N10-ACASTRO:~/pyats2$ /usr/bin/python3 /home/aix/pyats2/main.py

2021-06-08 14:54:38,901: %UNICON-INFO: +++ RT-BIOCOM-TRANSBRAS logfile /tmp/RT-1-cli-20210608T145438895.log +++

2021-06-08 14:54:38,902: %UNICON-INFO: +++ Unicon plugin ios +++
Trying 10.9.248.1...


2021-06-08 14:54:38,943: %UNICON-INFO: +++ connection to spawn: telnet 10.9.248.1, id: 140453737108536 +++

2021-06-08 14:54:38,944: %UNICON-INFO: connection to RT-1
Connected to 10.9.248.1.
Escape character is '^]'.



User Access Verification

Username: Um erro ocorreu ao conectar ao dispositivo Device RT-BIOCOM-TRANSBRAS, type ios
Conectando ao dispositivo Device RT-1, type ios
Trying 10.9.248.1...


2021-06-08 14:54:50,871: %UNICON-INFO: +++ connection to spawn: telnet 10.9.248.1, id: 140453703976160 +++

2021-06-08 14:54:50,872: %UNICON-INFO: connection to RT-1
Connected to 10.9.248.1.
Escape character is '^]'.

Is there any way to fix or adjust the timing properly?

Below a python snippet

from genie.conf import Genie
testbed = Genie.init("my-testbed-2.yaml")
for device in testbed:
     device.connect()

Thank you

5 Replies 5

Alex Stevenson
Cisco Employee
Cisco Employee

 

Hello @Andre Castro,

 

Is there a specific orchestration or management software you're using? I'd like to know which technology and which release version you're running before I make any suggestions. I think other user could benefit from that as well.

 

Best regards!

 

Hi @Alex Stevenson 

No, I'm just using my own script to run batch commands.

Thanks!

renanhingel
Level 1
Level 1

Hello Andre,

 

Like Alex mentioned above, we need more information before we can make suggestions. However, if you're still facing the issue, I'd recommend you to test the telnetlib in a separated environment so you can learn how to use it and the outputs you'll receive while using it.

 

Telnetlib comes with Python and it supports timeout, here's the docs: https://docs.python.org/3/library/telnetlib.html

 

A simple code snippet with telnet and send a command:

 

import socket
import telnetlib
import time

device = ""
user = ""
password = ""

try:
    client = telnetlib.Telnet(host=device,port=23, timeout=100)
    client.read_until(b"username: ")
    client.write(user.encode('ascii') + b"\n")
    if password:
        client.read_until(b"password: ")
        client.write(password.encode('ascii') + b"\n")
    client.write(b"show ip int brief\n")
    time.sleep(2)
    client.write(b"exit\n"))
    print(client.read_all().decode('ascii'))

except socket.timeout:
    print("Socket timed out while connecting to " + device)

Hope that can shed some light, let us know if you solved the issue.

 

Best Regards,

Renan

Hi @renanhingel 

Thank you for the suggestion.

Surely I can use this method but I'm still curious about the pyATS library, as opposed to Ansible for instance which runs flawlessly, there is a lot of additional overhead in it though.

Cheers!

__Babygirl__09
Level 1
Level 1

Hi how can I improve my skills due to it

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: