pythontelnettelnetlib

telnetlib execution not responding and timing out


I've been trying to connect to a telnet service via telnetlib for a few hours now. I've been trying to use telnetlib to read content and interact with the remote service. I've been using this code for testing purposes (with Python version 3.9)

import telnetlib

HOST = "horizons.jpl.nasa.gov"
port = "6775"                                                                                                                                                                              

tn = telnetlib.Telnet(HOST)
tn.open()                                                                                                                                                                            

print(tn.read_until("Horizons> "))
tn.write("x\r")
tn.close()

print(tn.read_all())

I expected seeing the welcome screen and prompt of the JPL Horizons system-the prompt is "Horizons> ", at which point I wanted the program to enter the exit code "x" and enter. However, instead the program is unresponsive, printing nothing to the terminal and timing out (TimeoutError: [Errno 60] Operation timed out). The error log indicates that the error is from the create_connection function of telnetlib.py, specifically sock.connect(sa). Is there something wrong with my code? How can I ensure the connection is made and the output of the service is printed for use?

If not: is there any other way to automate telnet communication and read output with Python?

-=UPDATE=- replacing the initial tn.write(b"x\r") with another command, trying to actually get some content and info out of the connection:

tn.write(b"sun\r")
tn.read_until(b" Select ... [E]phemeris, [F]tp, [M]ail, [R]edisplay, ?, <cr\
>:")
tn.write(b"x\r")
tn.read_until(b"Connection closed by foreign host.")
info = tn.read_until(b"Connection closed by foreign host.")
tn.close()
print(info)
print(tn.read_all().decode("utf-8"))

just prints until the first prompt, not entering sun\r (or at least, not printing the result) and then doesn't end the program, leaving me stuck on the 'Horizons> " prompt.


Solution

  • You're not using your port variable anywhere, so telnetlib is trying to connect to port 23. Try this:

    import telnetlib
    
    HOST = "horizons.jpl.nasa.gov"
    port = "6775"                                                                                                                                                                              
    
    tn = telnetlib.Telnet(HOST, port)
    print(tn.read_until(b"Horizons> "))
    tn.write(b"x\r")
    tn.close()
    
    print(tn.read_all())
    

    Note also that you need to provide bytes instead of strings to the telnetlib functions.


    Update 1

    It looks like you should be sending \n to terminate commands rather than \r. The following code works for me:

    import sys
    import telnetlib
    import time
    
    HOST = "horizons.jpl.nasa.gov"
    port = "6775"                                                                                                                                                                              
    
    with telnetlib.Telnet(HOST, port) as tn:
        tn.read_until(b"\r\nHorizons> ")
        tn.write(b"sun\n")
        data = tn.read_until(b" Select ... [E]phemeris, [F]tp, [M]ail, [R]edisplay, ?, <cr>:")
        tn.write(b"x\n")
        tn.read_all().decode("utf-8")
    
    print('--- data ---')
    print(data.decode())