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.