I would like to understand how to use telnetlib3 for a simple scenario.
The longstanding telnetlib (not 3) has a simple example at https://docs.python.org/3/library/telnetlib.html where the python program connects to a telnet server, then looks for prompts and provides responses. One can readily see how to extend this example to different prompts, add timeouts, and add more prompt-response steps.
import getpass
import telnetlib
HOST = "localhost"
user = input("Enter your remote account: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until(b"login: ")
tn.write(user.encode('ascii') + b"\n")
if password:
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"ls\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))
However, telnetlib (not 3) is deprecated.
The replacement, telnetlib3 ( https://telnetlib3.readthedocs.io/en/latest/intro.html#quick-example ) provides an example based on asyncio, and the async "shell" function (that interacts with the server) blocks waiting for prompt (rationale for async) and always responds to the server with 'y'.
import asyncio, telnetlib3
async def shell(reader, writer):
while True:
# read stream until '?' mark is found
outp = await reader.read(1024)
if not outp:
# End of File
break
elif '?' in outp:
# reply all questions with 'y'.
writer.write('y')
# display all server output
print(outp, flush=True)
# EOF
print()
loop = asyncio.get_event_loop()
coro = telnetlib3.open_connection('localhost', 6023, shell=shell)
reader, writer = loop.run_until_complete(coro)
loop.run_until_complete(writer.protocol.waiter_closed)
I am a few clues short on how to get code that's structured this way to perform the more mainstream task that's demonstrated in the (literally!) straightforward telnetlib (not 3) example where the server provides a series of different prompts, and the python program is to provide corresponding responses. I suspect that this is partly down to my unfamiliarity with asyncio and what code patterns one should use to get an async function to carry out a series of steps.
So it would be a great help to see the telnetlib (not 3) example implemented in this style.
I think it's a bit of a stretch to call
telnetlib3a "replacement" fortelnetlib. I guess it's similar in that it allows you to write a telnet client (or server), but it's really an entirely different beast. For the sort of thing you're doing in the initialtelnetlibexample, I would generally reach for pexpect (or just, you know, normalexpect).It looks like PEP 594 also points to Exscript as a solution, and that looks closer to
telnetlibthantelnetlib3.Here's an example I whipped up that uses
telnetlib3to connect to a local switch, login, send theenablecommand, and then log out:I don't really like it. I'd much rather do something like this: