Python Forum
How to read router prompt and use it? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: Networking (https://python-forum.io/Forum-Networking)
+--- Thread: How to read router prompt and use it? (/Thread-How-to-read-router-prompt-and-use-it)



How to read router prompt and use it? - simbha - May-06-2019

When i login to a router, i get a following prompt.

R3-NYC
login: tac
Password:
[Router]R3-NYC#
[Router]R3-NYC#show clock
Wed May 22 04:46:18 2019 CST
[Router]R3-NYC#

1. How to capture CLI prompt to a string variable and use it for read_until() ?
i want to capture the prompt "[Router]R3-NYC#" . If i use read_until(), i have to mention the match. But i want to capture it after i login and use it later for the read_until() function the entire string instead of partial matches. I cannot hard core the hostname because i use this function for several other devices which will have different prompts.

2. How to capture command output to a string ?
I saw many refering to the function read_eager(). For some reason, when i test it, it has no capture. Only read_all() is working as expected. For this i have to wait till the telnet is closed. Could you please share some examples, if read_eager() is working for you.

Thanks
Simbha


RE: How to read router prompt and use it? - simbha - May-06-2019

Took a while but i could figure it out. Just in case any one is interested.

==== skipped the tn connect part===

Prompt capture to a string:
>>>tn.write(b" " + "\n".encode('ascii'))   ===> Send a simple enter("\n" character and capture the output to get prompt.
>>>str2 = tn.read_eager().decode()   ====> Save prompt to a string.
>>>print(str2)
Output:
[Router]R5-NYC#
Command output capture to a string in case output is multiple lines.
>>>tn.write(b"show clock" + "\n".encode('ascii'))
>>>str2 = tn.read_very_eager().decode()
>>>print(str2)
Output:
show clock Wed May 22 07:16:09 2019 CST [Router]R5-NYC#
tn.read_eager() ===> Kinda returns line by line output but very inconsistent and can be saved to a string.
tn.read_very_eager() === > Returned the entire output(including multiple lines) and can be saved to a string.