← Back to context

Comment by chasd00

1 month ago

i bet this is something an ai could help with. "write a simple telnet client in python. It only needs to connect to the host and display what the host sends. conform to any connection initialization requirements per rfc 854". That would probably get you close.

/edit front page of google did this and it worked for me. Need to do a pip install telnetlib3

  import telnetlib3
  import sys

  def simple_telnet_client(host, port=23):
    """
    Connects to a telnet server and prints incoming data.
    Compliant with RFC 854 (via telnetlib handling of NVTs).
    """
    try:
        # Initialize connection
        print(f"Connecting to {host}:{port}...")
        tn = telnetlib3.Telnet(host, port)

        # Read and display output indefinitely until connection closes
        while True:
            # read_eager() reads data already available without blocking
            data = tn.read_eager()
            if data:
                sys.stdout.write(data.decode('ascii', errors='ignore'))
                sys.stdout.flush()

            # Check if socket is closed
            if tn.get_socket() is None:
                break

    except ConnectionRefusedError:
        print("Connection refused.")
    except Exception as e:
        print(f"An error occurred: {e}")
    finally:
        if 'tn' in locals():
            tn.close()
            print("\nConnection closed.")

  if __name__ == "__main__":
    # Example usage:
    # simple_telnet_client("telehack.com", 23)

    # Replace with desired host
    host = input("Enter host: ")
    simple_telnet_client(host)

apt/brew/your-package-manager install telnet is simpler and more reliable.