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 06-06-2017, 09:22 AM
jwats06 jwats06 is offline
Registered User
 
Join Date: May 2017
Posts: 12
Help with script where output is off screen

Hello everyone,

I have a script that I'm trying to build where the challenge is that crt.screen.waitforstring doesn't work due to the length of the output literally taking it off the screen.

The goal is to run a command in a router, and capture the entire output, count those lines, and return a message box with the count. The script works so long as the output doesn't go off the screen. I'm not sure from the reading I have done that there is a function that allows me to get a string from the scrollback but it would appear that might be the ticket. Any ideas?

Code:
#$language = "VBScript"
#$interface = "1.0"

' Service check in a click -

' Be "tab safe" by getting a reference to the tab for which this script
' has been launched:
Set objTab = crt.GetScriptTab

' Setup to allow on screen messaging
Set g_shell = CreateObject("WScript.Shell")


Sub Main()

			' Check Services
			crt.screen.send "environment no more" & chr(13)
			crt.screen.waitforstring "# "
			crt.Screen.Send "show service service-using | match invert-match _ | match ""Down """ & chr(13)
			crt.Screen.WaitForString "# "
			
	
				' Call the function to Count Lines Down in Services
				LineCountOfDown
End Sub




'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' FUNCTIONS
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

' Capture something from the screen from a command we ran (services in this case)
Function LineCountOfDown()


lineCount = 0
      do while lineCount < 500
	 
      
            ' Get the row number of the current row
            row = crt.screen.CurrentRow - lineCount

            ' Get the contents of that row
            rowString = crt.Screen.Get(row, 1, row, 80)
			

            ' Look for the access-class string
            if(InStr(rowString, "show service") > 0) then
			 

                  ' Stop the loop
                  lineCount = 500
                  
                  ' Grab the line
                 numRows = crt.screen.CurrentRow - row -1
				  X = g_shell.Popup("Number of Services Down: " & numRows, 5, "Service Validation Checks")
            else        
                        
                  ' Increment the line counter
                  lineCount = lineCount + 1
                  
            end if

      loop 
	  
	  End Function
Reply With Quote
  #2  
Old 06-06-2017, 10:22 AM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 998
WaitForString doesn't capture the data, but ReadString works similarly and does capture the data. See Chapter 4 of the online scripting guide, specifically the subsection of 4.3 titled, "Capturing Data Streamed from the Remote".

If you're using ReadString to not only wait for something to appear, but also capture everything that arrived up to that point, you'll be able to do something like this:

Code:
#$language = "VBScript"
#$interface = "1.0"

Set objTab = crt.GetScriptTab

Sub Main()

    ' Use "Screen.Synchronous = True" to make sure that
    ' we won't miss out on any data that might get displayed
    ' to the screen before we have a chance to wait for it.
    crt.Screen.Synchronous = True
    
    strCommand = "show config"
    crt.Screen.Send strCommand & vbcr
    ' Wait for the remote system to echo back to SCRT the
    ' the command we just sent (the command itself, not any
    ' of the output). We do this in order to avoid having
    ' the command itself part of what we capture.
    crt.Screen.WaitForString strCommand & vbcrlf
    
    ' Now, let's use "ReadString()" to not only wait for our
    ' shell prompt to appear, but also capture all output
    ' received into a variable...
    strOutput = crt.Screen.ReadString("# ")
    
    ' Once we have all of the output stored in a variable
    ' (Thank you, ReadString()!), we can "split" it up into
    ' lines using the "Split()" VBscript native:
    vLines = Split(strOutput, vbcrlf)
    
    ' Number of lines in the output is merely going to be
    ' the number of elements in the array that was created
    ' by Split() (because it's zero-based, we'll already
    ' have eliminated the last element of the array -- where
    ' the shell prompt appears)
    nLineCount = Ubound(vLines)
    
    ' Now that we know how many lines of output there are, let's
    ' show what we know:
    crt.Dialog.MessageBox "#Lines: " & nLineCount & vbcrlf & _
        strOutput
End Sub

Does this help?

--Jake
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
Reply With Quote
  #3  
Old 06-06-2017, 11:19 AM
jwats06 jwats06 is offline
Registered User
 
Join Date: May 2017
Posts: 12
Yes, with a few mods that worked perfect, and seems to be much quicker too! I simply had to "send" the command "environment no more" which outputs the entire length rather than making the user hit a key to keep scrolling down. Thank you for the quick help and a new toy to use

The finished code:
Code:
#$language = "VBScript"
#$interface = "1.0"

' Service check in a click -

' Be "tab safe" by getting a reference to the tab for which this script
' has been launched:
Set objTab = crt.GetScriptTab

' Setup to allow on screen messaging
Set g_shell = CreateObject("WScript.Shell")


Sub Main()

    ' Use "Screen.Synchronous = True" to make sure that
    ' we won't miss out on any data that might get displayed
    ' to the screen before we have a chance to wait for it.
    crt.Screen.Synchronous = True
			
	crt.screen.send "environment no more" & chr(13)
	crt.screen.waitforstring "# "
    
    strCommand = "show service service-using | match invert-match _ | match ""Down """
    crt.Screen.Send strCommand & vbcr
    ' Wait for the remote system to echo back to SCRT the
    ' the command we just sent (the command itself, not any
    ' of the output). We do this in order to avoid having
    ' the command itself part of what we capture.
    crt.Screen.WaitForString strCommand & vbcrlf
    
    ' Now, let's use "ReadString()" to not only wait for our
    ' shell prompt to appear, but also capture all output
    ' received into a variable...
    strOutput = crt.Screen.ReadString("# ")
    
    ' Once we have all of the output stored in a variable
    ' (Thank you, ReadString()!), we can "split" it up into
    ' lines using the "Split()" VBscript native:
    vLines = Split(strOutput, vbcrlf)
    
    ' Number of lines in the output is merely going to be
    ' the number of elements in the array that was created
    ' by Split() (because it's zero-based, we'll already
    ' have eliminated the last element of the array -- where
    ' the shell prompt appears)
    nLineCount = Ubound(vLines)
    
    ' Now that we know how many lines of output there are, let's
    ' show what we know:
    X = g_shell.Popup( "Number of Services Down: " & nLineCount, 8, "Service Validation Checks")
End Sub
Reply With Quote
  #4  
Old 06-26-2017, 01:36 PM
jwats06 jwats06 is offline
Registered User
 
Join Date: May 2017
Posts: 12
Jake,

If I wanted to add additional commands to this code and count those in a similar fashion, how does one go about "replicating" the code to run this code here, throw up the count, run the next one, throw up the count, etc.

It obviously isn't as easy as just adding the code below it and changing the commands as it doesn't seem to take that. In fact, if I simply take the original code and change the command strCommand is sending, it fails on the line
Code:
crt.Screen.WaitForString strCommand & vbcrlf
if I remove the & vbcrlf the code will run, but the count is off by +1.

Can you help me understand what might be going on? Let me know if you need the output of the code or anything else. Just seemed strange that it completely wouldn't run with a different command sent in the same working code, I change the command back to the first one and it works. Guessing that it has something to do with some formatting in the output of the second command that the first one doesn't have and the & vbcrlf throws it off further.

The code you helped me with, counted the following output:
Code:
A:PNSCFLJA6DW-COXMIS# environment no more 
A:PNSCFLJA6DW-COXMIS# show service service-using | match invert-match _ | match "Down "
140000075    Epipe     Up   Down 14000001   epipe service-23313 PNSCFLJA6DW-CO*
140000079    Epipe     Up   Down 14000001   epipe service-23423 PNSCFLJA6DW-CO*
140000109    Epipe     Up   Down 14000001   epipe service-34701 PNSCFLJA6DW-CO*
140000129    Epipe     Up   Down 14000001   epipe service-44770 PNSCFLJA6DW-CO*
140768266    Epipe     Up   Down 14644244   epipe service-76419 PNSCFLJA6DW-CO*
I simply change the command to get a different output: the only way I can get it to even complete running the code is remove the "& vbcrlf", but instead of a count of 4 here, I get a count of 5.

Code:
A:PNSCFLJA6DW-COXMIS# environment no more 
A:PNSCFLJA6DW-COXMIS# show service sdp-using | match Down 
140000075   1501:140000075      Spok    172.XX.XXX.1   Down   131054   0
140000075   1502:140000075      Spok    172.XX.XXX.2   Down   131065   0
140000079   1501:140000079      Spok    172.XX.XXX.1   Down   131052   0
140000079   1502:140000079      Spok    172.XX.XXX.2   Down   131063   0

Last edited by jwats06; 06-26-2017 at 01:41 PM.
Reply With Quote
  #5  
Old 06-26-2017, 05:36 PM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 998
The & vbcrlf is the portion of the WaitForString that signifies the end of the line (on which the command was issued) has been received. This is included so that we know exactly where to begin counting lines.

Without seeing the full block of code, it will be very difficult to determine what's going wrong.

Can you post the block of code you added, in context of the existing code you had, without exposing any personal/private/sensitive data, so that I can see it all in the proper context?

I might also need to see the full output of the data as it appears in SecureCRT's terminal window as well.

If this info is not something you are comfortable posting to a public forum such as this, please send email to support@vandyke.com with a subject of "Attn Jake - forum thread #12717" and attach the relevant info (taking care to not expose any sensitive info like usernames and passwords in the copy of the script file you send).

--Jake
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
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 09:09 AM.