0

I usually worked with python library "telnetlib"

but i faced a problem and i don't know how to figure it out.

Here's the code:

def bbb():
    tn = telnetlib.Telnet(Ip, port=23, timeout=20)
    print(tn)
    tn.write(bytes("Something no.1 \n", encoding='utf-8'))        
    time.sleep(10)    

    ## ~~~ blah blah read data ~~~ ##

    tn.mt_interact()

    tn.write(bytes("Something no.2 \n", encoding='utf-8'))
    
    tn.close()
    time.sleep(1.5)
    

I wanna read a lot of data from telnet and any of tn.read command doensn't work.. (ex. tn.read_all())

So i need to use tn.mt_interact()

But after that line, code doesn't work and telnet connection looks like just pause so I cannot close tn connection.

SURPRISINGLY, when i run the same code at SPYDER(Anaconda), it WORKS!!! Is that possible?

And I need to make this code as a exe file and exe file doesn't work at the same point: tn.mt_interact()

Kraay89
  • 919
  • 1
  • 7
  • 19
ellen
  • 1
  • I cannot reproduce with Python 3.9 on Windows 10 connecting to a Unix machine: `tn.read_until(':')` `tn.write(b'login\r')` `tn.read_until(':')` `tn.write(b'password\r')` `tn.read_until(b'$ ')` `tn.write(b'ls\r')` `tn.read_until(b'$ ')` `tn.write(b'\x04')` `tn.read_all()` works perfectly from IDLE. – Serge Ballesta Nov 03 '21 at 09:56
  • After a second reading of the question, I think that using `interact` (or `mt_interact`) is indeed not possible in IDLE, probably because of the way io streams are implemented: You need to send *dummy* commands (just type *Enter*) to have `interact` read and display what host has sent. But anyway, `interact` is only meant to be able to quickly see what happens, and `read_until` or `expect` are the methods of first choice. – Serge Ballesta Nov 04 '21 at 10:21

1 Answers1

0

According to the telnet library, tn_interact is a multithreaded version of interact, which is just emulating a dumb telnet client.

Telnet.interact()
Interaction function, emulates a very dumb Telnet client.

So even if it works in Spyder, using interact or tn_interact won't solve your problem.

You have to see why the reading and writing functions don't work.

Is the connection open successfully? Is the device replying to your commands?

You will need to debug and understand why it doesn't communicate