pythonpython-asynciotelnetlibtelnetlib3

Python telnetlib3 examples


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.


Solution

  • I think it's a bit of a stretch to call telnetlib3 a "replacement" for telnetlib. 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 initial telnetlib example, I would generally reach for pexpect (or just, you know, normal expect).

    It looks like PEP 594 also points to Exscript as a solution, and that looks closer to telnetlib than telnetlib3.


    Here's an example I whipped up that uses telnetlib3 to connect to a local switch, login, send the enable command, and then log out:

    import asyncio
    import telnetlib3
    
    async def shell(reader, writer):
        rules = [
                ('User:', 'admin'),
                ('Password:', 'secret'),
                (') >', 'enable'),
                (') #', 'exit'),
                (') >', 'logout'),
                ]
    
        ruleiter = iter(rules)
        expect, send = next(ruleiter)
        while True:
            outp = await reader.read(1024)
            if not outp:
                break
    
            if expect in outp:
                writer.write(send)
                writer.write('\r\n')
                try:
                    expect, send = next(ruleiter)
                except StopIteration:
                    break
    
            # display all server output
            print(outp, flush=True)
    
        # EOF
        print()
    
    async def main():
        reader, writer = await telnetlib3.open_connection('office-sw-0', 23, shell=shell)
        await writer.protocol.waiter_closed
    
    
    if __name__ == '__main__':
        asyncio.run(main())
    

    I don't really like it. I'd much rather do something like this:

    import sys
    import pexpect
    
    rules = [
        ("User:", "admin"),
        ("Password:", "secret"),
        (r"\) >", "enable"),
        (r"\) #", "exit"),
        (r"\) >", "logout"),
        (pexpect.EOF, None),
    ]
    
    client = pexpect.spawn("telnet office-sw-0")
    
    # This is so we can see what's happening.
    client.logfile = sys.stdout.buffer
    
    for expect, send in rules:
        client.expect(expect)
        if send is None:
            break
        client.sendline(send)