VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   screen.readstring/get --> Unable to position start? (https://forums.vandyke.com/showthread.php?t=14379)

garrettwilliams 12-28-2020 02:47 PM

screen.readstring/get --> Unable to position start?
 
Hey all,

I feel like I must be missing something simple. I'm trying to put together a simple script to pull the running config off a device and drop it into a text file line by line.
I know there are examples of doing exactly that, but I'd like something as watered down as possible so I can easily troubleshoot / make additions.

What I'm doing here is checking 'show term', setting term length to 0, and then doing my show runn. This script is going up and grabbing the output of the 'show term'. I cannot, no matter what I've tried, just get the thing to read after show runn. What am I missing here?
Code:

# $language = "python3"
# $interface = "1.0"


import os
import re



#send show term command, and snag the terminal length and width.



crt.Screen.Send("sh term\n")
crt.Screen.WaitForString("Session Timeout")
screenrow = crt.Screen.CurrentRow - 1
term_info = crt.Screen.Get(screenrow, 1, screenrow, 30).strip(" ")
re_string = r'Length: (\d+) lines, Width: (\d+)'
term_length = re.search(re_string,term_info).group(1)
term_width = re.search(re_string,term_info).group(2)




crt.Screen.Synchronous = True
filename = 'C:\Tools\output.txt'
fp = open(filename, "w+")

crt.Screen.Send("term length 0\n")
crt.Screen.WaitForString("\n")

prompt = "Nexus9000v#"
wait_strs = ['\n', prompt]
with open(filename, "w+") as fp:
    crt.Screen.Send("sh runn\n")
    crt.Screen.WaitForString('\n')
    while True:

        nextline = crt.Screen.ReadString(wait_strs, 30)
        if crt.Screen.MatchIndex == 1:
            nextline = nextline.strip(" ")
            crt.Dialog.MessageBox(nextline)
            fp.write(nextline + "\n")
        elif crt.Screen.MatchIndex == 2:
            break


        #result = crt.Screen.WaitForStrings(wait_strs)
        #if result == 2:
        #    break
        #screenrow = crt.Screen.CurrentRow - 1
        #line_info = crt.Screen.Get(screenrow, 1, screenrow, 30).strip(" ")
        #fp.write(line_info + '\n')


bgagnon 12-29-2020 07:50 AM

Hi garrettwilliams,
Quote:

This script is going up and grabbing the output of the 'show term'. I cannot, no matter what I've tried, just get the thing to read after show runn.
What is the script supposed to "read"? If you mean it's not reaching the ReadString() it's likely because you are already out of sync after these statements:
Code:

crt.Screen.Send("term length 0\n")
crt.Screen.WaitForString("\n")

The above WaitForString() is only going to capture the echoed new line. You need a WaitForString() to know the command is complete (likely waiting for the prompt, as you have done inside the while construct).

garrettwilliams 12-29-2020 10:45 AM

Seems very obvious now, thanks so much for the quick reply.

Happy Holidays.

bgagnon 12-29-2020 11:07 AM

Hi garrettwilliams,

You are quite welcome.

Happy Holidays to you also. :)


All times are GMT -6. The time now is 06:34 PM.