Telnet script help

Dear Python Community,

I am very new to Python and am trying to build a Telnet script that will automatically telnet to one or more IP addresses on a specific port, check for a valid connection, print a result “connection successful” or “connection not successful” and then close the connection. It seems very simple, but I am struggling mightily to get it created. Here is what I have so far, this is just a test connection going to one fo Googles servers:

import telnetlib
host = “108.177.122.106”
tn = telnetlib.Telnet()
tn.open(host, “80”)
tn.write(b"\x1d")
output = tn.read_all()
print(output)
if b"" in output:
print(“Connection Successful”)
else:
print(“Connection not successful”)

So far, I know the connection is successful, however, Im stuck on what options I have for validating whether or not the connection was a success or not a success. Typically, when connecting, you know you’re connected because the screen will go blank, you issue the “escape sequence” (CTRL + ]) and then you can navigate the Telnet options. However, I am struggling with writing the feedback portion of the script. Any help/pointers you can provide would be great!

Thanks!

So far, I know the connection is successful, however, Im stuck on
what options I have for validating whether or not the connection
was a success or not a success. Typically, when connecting, you
know you’re connected because the screen will go blank, you issue
the “escape sequence” (CTRL + ]) and then you can navigate the
Telnet options. However, I am struggling with writing the feedback
portion of the script. Any help/pointers you can provide would be
great!

I’d use something like this:


tn.open(host, "80")
try:
    tn.fill_rawq()
except ConnectionResetError:
    print("Connection refused")
    tn.close()
print("Connection successful")

Note that your use of port 80 suggests you’re trying to use
telnetlib to connect to an HTTP server, which is a pretty terrible
idea. There are actual modules in the Python stdlib intended for
communicating via HTTP, while telnetlib is specifically meant for
the Telnet protocol (IETF RFC 854 and its various extensions). Don’t
make the mistake of confusing the conventional convenience of using
a Telnet client to emulate other TCP protocl connections for good
programming practice. If the reason you’re (ab)using telnetlib to
open a HTTP connection is that you want to manipulate a low-level
TCP socket connection to the server rather than just acting as a
conventional HTTP client, look into the socket module instead:

https://docs.python.org/3/library/socket.html

To put a further nail in this coffin, telnetlib is slated for
removal from the Python stdlib soon:

fungi, thanks so much! I will look into the socket library. The reason behind using telnet was so that I could force a TCP connection on a specific port, not just port 80. But it looks like the socket library might be the most appropriate way to go about this? Thanks.

By Chris Craddock via Discussions on Python.org at 04Apr2022 13:17:

fungi, thanks so much! I will look into the socket library. The reason
behind using telnet was so that I could force a TCP connection on a
specific port, not just port 80. But it looks like the socket library
might be the most appropriate way to go about this? Thanks.

Very much so.

Telnet is actually a whole protocol, not just a bare TCP connection.
See: RFC 854: Telnet Protocol Specification

Cheers,
Cameron Simpson cs@cskk.id.au