Get only output from telnet command

41 Views Asked by At

I am using telnetlib3 to access a Telnet server with python. With the following code, I am correctly getting the result, but the output is cluttered by all the input prompts. I would like to get only the clear result of the command show all-outlets-status, without any other line. Is it possibile?

#!/usr/bin/env python3


import asyncio
import telnetlib3

async def shell(reader, writer):
    rules = [
            ('login as:', '.....'),
            ('password:', '......'),
            ('PE-08I-532236$ ', 'show all-outlets-status'),
            ('PE-08I-532236$ ', '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


async def main():
    reader, writer = await telnetlib3.open_connection('10.0.0.59', 23, shell=shell)
    await writer.protocol.waiter_closed


if __name__ == '__main__':
    asyncio.run(main())
0

There are 0 best solutions below