Python Forum
Telnetlib reading output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Telnetlib reading output
#1
Hi,

Im using a python telnetlib script to communicate with a device.

I want to keep an open connection without exiting. My problem is that i cant read the output before sending the exit command.
Based on the telnetlib documentation i tested with read_very_eager() and read_eager() without success.

An example of the code:
HOST = "192.168.79.182"
user = "root"
password = "xxx"
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 time\n')
Output = (tn.read_very_eager().decode('ascii'))
print(Output)
any ideas would be appreciated
Reply
#2
Are you able to see any logs from the device just to make sure that the script can login to your device?

From your command verify the ff.
tn.read_until(b"username: ") # <- Make sure script read the correct string or login promt
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"password: ")
    tn.write(password.encode('ascii') + b"\n")
after this you read this #(priv mode) before sending the command this is for cisco.. but you can use the same method to other platform.
tn.read_until(b”#”)
tn.write(b'show time\n')
tn.read_until(b”#”) # <-- once the show time fully executed script should see this before reading or exiting.
To read
readoutput = tn.read_all().decode(‘ascii’)
print readoutput
Larz60+ write Sep-16-2024, 11:06 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Tags have been added, Please use BBCode tags on future posts.
Reply
#3
Output = (tn.read_very_eager().decode('ascii'))
print(Output)

don't use this
but you can use this command

tn.listener()

Big Grin Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  telnetlib. configure hostnames lado 1 3,624 May-30-2020, 01:49 PM
Last Post: matya0403
  telnetlib timeout kerzol81 0 4,324 Sep-12-2019, 08:38 AM
Last Post: kerzol81
  Python script hangs after entering username to remote login using telnetlib auto 0 5,521 Sep-10-2018, 01:10 PM
Last Post: auto
  telnetlib timeout expections handling issue anna 0 8,634 Jun-08-2018, 06:28 AM
Last Post: anna
  python telnetlib prompt matching issue anna 0 4,539 Jan-30-2018, 05:34 PM
Last Post: anna
  telnetlib.Telnet timeout issue.. taking much time anna 1 9,336 Jan-19-2018, 01:52 PM
Last Post: devs4p

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020