Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 12-28-2020, 03:47 PM
garrettwilliams garrettwilliams is offline
Registered User
 
Join Date: Dec 2020
Posts: 11
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')

Last edited by cboyack; 12-29-2020 at 08:03 AM. Reason: Please use the [CODE] and [/CODE] tags to denote a script
Reply With Quote
  #2  
Old 12-29-2020, 08:50 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,548
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).
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 12-29-2020, 11:45 AM
garrettwilliams garrettwilliams is offline
Registered User
 
Join Date: Dec 2020
Posts: 11
Seems very obvious now, thanks so much for the quick reply.

Happy Holidays.
Reply With Quote
  #4  
Old 12-29-2020, 12:07 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,548
Hi garrettwilliams,

You are quite welcome.

Happy Holidays to you also.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 02:44 AM.