View Single Post
  #6  
Old 05-16-2013, 09:33 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
I understand what you're saying, and hesitate in making coding suggestions, but I think the approach taken in Read Data From Hosts/Commands File... is a good combination of WaitForString and ReadString.

Here's a snippet which demonstrates using WaitForString to know when to send a command, and ReadString so that output can be parsed. A general approach like this may be easier to write, maintain, modify, than an approach consisting of a series of sequential WaitForStrings.
Code:
For Each strCommand In vCommands
	If strCommand = "" Then Exit For
	
	' Send the command text to the remote
	g_objNewTab.Screen.Send strCommand & vbcr

	' Wait for the command to be echo'd back to us.
	g_objNewTab.Screen.WaitForString strCommand
	
	' Since we don't know if we're connecting to a cisco switch or a
	' linux box or whatever, let's look for either a Carriage Return
	' (CR) or a Line Feed (LF) character in any order.
	vWaitFors = Array(vbcr, vblf)
	bFoundEOLMarker = False
	Do
		' Call WaitForStrings, passing in the array of possible
		' matches.
		g_objNewTab.Screen.WaitForStrings vWaitFors, 1
		
		' Determine what to do based on what was found)
		Select Case g_objNewTab.Screen.MatchIndex
			Case 0 ' Timed out
				Exit Do
			
			Case 1,2 ' found either CR or LF
				' Check to see if we've already seen the other
				' EOL Marker
				If bFoundEOLMarker Then Exit Do
				
				' If this is the first time we've been through
				' here, indicate as much, and then loop back up
				' to the  top and try to find the other EOL
				' marker.
				bFoundEOLMarker = True
		End Select
	Loop

	' Now that we know the command has been sent to the remote
	' system, we'll begin the process of capturing the output of
	' the command.
	
	Dim strResult
	' Use the ReadString() method to get the text displayed
	' while the command was runnning.  Note that the ReadString
	' usage shown below is not documented properly in SecureCRT
	' help files included in SecureCRT versions prior to 6.0
	' Official.  Note also that the ReadString() method captures
	' escape sequences sent from the remote machine as well as
	' displayed text.  As mentioned earlier in comments above,
	' if you want to suppress escape sequences from being
	' captured, set the Screen.IgnoreEscape property = True.
	strResult = g_objNewTab.Screen.ReadString(strPrompt)

	' If you want the command logged along with the results,
	' uncomment the next two lines
	' objFile.WriteLine "Results of command """ & strCommand & _
	'    """ sent to host """ & strHost & """: "

	' Write out the results of the command and a separator
	objFile.WriteLine strResult
	
	' Add a separator between commands 
	objFile.WriteLine _
		"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"

Next   ' Command Loop
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote