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-05-2020, 11:04 AM
Dino Dino is offline
Registered User
 
Join Date: Jun 2020
Posts: 4
crt.Screen.ReadString() question

Hi,

I'm trying to get this script to capture the output of a linux grep:

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

crt.Screen.Synchronous = True

' This automatically generated script may need to be
' edited in order to work correctly.

Sub Main
	crt.Screen.Send "cd /mmc/logs" & chr(13)
	crt.Screen.Send "grep JESD messages" & chr(13)
 
	'crt.Screen.WaitForString vbcr
	' Read from the prompt to the command.  This should capture the required data.
	' By including the prompt in the command, the prompt will not be captured.
	strResult = crt.Screen.ReadString("bash-3.2#")
	' You may want to see what was captured prior to sending it to ensure that it is
	' the correct data.  If the variable contains more information that you need,
	' you can use VBScript functions to remove the unwanted information.
	'MsgBox strResult
	'If strResult = "JESD" Then 
		'crt.Screen.Send "Found a JESD failure"
		
	If Instr(strResult, "JESD") then
		crt.Screen.Send "Found a JESD failure"
	Else 
		crt.Screen.Send "#No JESD failure found" & chr(13)
	End If
	crt.Screen.Send "## print strResult" & vbcr
	crt.Screen.Send strResult & vbcr
End Sub
Output:
bash-3.2# cd /mmc/logs
bash-3.2# grep JESD messages
#No JESD failure found
## print strResult
cd /mmc/logs



JESD
bash-3.2# #No JESD failure found
bash-3.2# ## print strResult
bash-3.2# cd /mmc/logs
bash-3.2#

Thanks,
Dino

Last edited by jdev; 06-05-2020 at 11:19 AM. Reason: Please use [code][/code] tags around your code to preserve indentation/spacing
Reply With Quote
  #2  
Old 06-05-2020, 02:15 PM
berdmann berdmann is offline
VanDyke Technical Support
 
Join Date: Aug 2017
Posts: 360
Hi Dino,

When performing multiple crt.Screen.Send operations (or a single Send() operation with multiple "\r" or vbcr elements (python vs vbscript)) with crt.Screen.Synchronous = True, it's important to keep the number of crt.Screen.WaitForString statements consistent with the number of times that crt.Screen.Send is called. (Additional synchronization can be achieved by waiting for each command itself in addition to its corresponding shell prompt.) For example:

Code:
strcmd= "command to be sent"
crt.Screen.Send strCmd& vbcr
crt.Screen.WaitForString strCmd
crt.Screen.WaitForString strShellPrompt
The script code that you posted has 6 total crt.Screen.Send calls and only a single WaitForString/ReadString call (that isn't commented out). The result is that none of the output of any of the commands following the 'cd /mmc/logs' command will be displayed to SecureCRT's terminal window until after the script terminates -- at which point, all the data buffered in the screen's waiting- to-be-displayed buffer will be immediately spewed to the screen.

Please refer to section 4.2 of our "Scripting Essentials: A Guide to Using VBScript in SecureCRT" for more information on this topic:
https://www.vandyke.com/support/tips...essentials.pdf
__________________
Thanks,
--Brittney

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 06-05-2020, 03:03 PM
Dino Dino is offline
Registered User
 
Join Date: Jun 2020
Posts: 4
Never returns from strResult = crt.Screen.ReadString(strShellPrompt)

Hi Brittney,

Thanks for you help. It's behaving differently now with the changes, the script hangs on the call to:
strResult = crt.Screen.ReadString(strShellPrompt)

I have to cancel running the script. Can you please comment on the call to crt.Screen.ReadString()


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

crt.Screen.Synchronous = True

' This automatically generated script may need to be
' edited in order to work correctly.

Sub Main
	strShellPrompt = "bash-3.2#"
	strcmd = "cd /mmc/logs"
	crt.Screen.Send strcmd & chr(13)
	crt.Screen.WaitForString strCmd
	crt.Screen.WaitForString strShellPrompt
	strcmd = "grep JESD messages"
	crt.Screen.Send strcmd & chr(13)
	crt.Screen.WaitForString strCmd
	crt.Screen.WaitForString strShellPrompt
	
	'crt.Screen.WaitForString vbcr
	' Read from the prompt to the command.  This should capture the required data.
	' By including the prompt in the command, the prompt will not be captured.
	strResult = crt.Screen.ReadString(strShellPrompt)
	' You may want to see what was captured prior to sending it to ensure that it is
	' the correct data.  If the variable contains more information that you need,
	' you can use VBScript functions to remove the unwanted information.
	MsgBox strResult
	'If strResult = "JESD" Then 
		'crt.Screen.Send "Found a JESD failure"
		
	If Instr(strResult, "JESD") then
		strcmd = "Found a JESD failure"
		crt.Screen.Send strcmd
		crt.Screen.WaitForString strCmd
		crt.Screen.WaitForString strShellPrompt
		
	Else 
		strcmd = "#No JESD failure found"
		crt.Screen.Send strcmd = strcmd  & chr(13)
		crt.Screen.WaitForString strCmd
		crt.Screen.WaitForString strShellPrompt

	End If
	strcmd = "## print strResult"
	crt.Screen.Send strcmd & vbcr
	crt.Screen.WaitForString strCmd
	crt.Screen.WaitForString strShellPrompt
	
	crt.Screen.Send strResult & vbcr
	crt.Screen.WaitForString strResult
	crt.Screen.WaitForString strShellPrompt

End Sub

Output capture:
bash-3.2# cd /mmc/logs
bash-3.2# grep JESD messages
JESD
bash-3.2#

Thanks,
Dino

Last edited by cboyack; 06-05-2020 at 03:14 PM. Reason: Please use [code][/code] tags around your code to preserve indentation/spacing
Reply With Quote
  #4  
Old 06-06-2020, 07:39 AM
Dino Dino is offline
Registered User
 
Join Date: Jun 2020
Posts: 4
crt.Screen.ReadString(strShellPrompt) never returns - Figured it out

Hi,

For those keeping track, I woke up and realized what my problem was.
After sending the grep commmand, I had two WaitForString, the one for the strShellPromt prevented the ReadString from completing. When I removed that it worked for the failure case. WHen grep found "JESD".

Code:
	strcmd = "grep JESD messages"
	crt.Screen.Send strcmd & chr(13)
	crt.Screen.WaitForString strCmd
	crt.Screen.WaitForString strShellPrompt  <<<<
	
	'crt.Screen.WaitForString vbcr
	' Read from the prompt to the command.  This should capture the required data.
	' By including the prompt in the command, the prompt will not be captured.
	strResult = crt.Screen.ReadString(strShellPrompt)
The case where the grep didn't return anything had some problem:

Code:
	If Instr(strResult, "JESD") then
		strcmd = "Found a JESD failure"
		crt.Screen.Send strcmd
		crt.Screen.WaitForString strCmd
		crt.Screen.WaitForString strShellPrompt
		
	Else 
		strcmd = "#No JESD failure found"
		crt.Screen.Send strcmd = strcmd  & chr(13)
		crt.Screen.WaitForString strCmd
		crt.Screen.WaitForString strShellPrompt

	End If
The Else resulted in a hung script.

Ouput:

bash-3.2# cd /mmc/logs
bash-3.2# grep JESD messages
grep: messages: No such file or directory
bash-3.2# False
bash: False: command not found

I just commented out the Else and I've got a working script for what I need. Any comments on what is wrong with the "Else" would be greatly appreciated.

Thanks,
Dino

Last edited by jdev; 06-09-2020 at 08:51 AM. Reason: wrap code in [code][/code] blocks to preserve indentation
Reply With Quote
  #5  
Old 06-09-2020, 10:43 AM
berdmann berdmann is offline
VanDyke Technical Support
 
Join Date: Aug 2017
Posts: 360
Hi Dino,

Your Else is likely failing due to the following line:

Code:
crt.Screen.Send strcmd = strcmd  & chr(13)
This line of code is a statement comparison, comparing 'strcmd' to 'strcmd & chr(13)'. 'strcmd' clearly does not equate to 'strcmd &chr(13)' , thus the comparison returns "False" and that's what gets sent as the command.

Why not just send 'strcmd & chr(13)' ?
__________________
Thanks,
--Brittney

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

Last edited by jdev; 06-09-2020 at 10:55 AM.
Reply With Quote
  #6  
Old 06-10-2020, 07:31 AM
Dino Dino is offline
Registered User
 
Join Date: Jun 2020
Posts: 4
Thanks!

Hi Brittney,

Doh! It was a typo, I should have noticed that, thanks for your help! It's working now.

Thanks,
Dino
Reply With Quote
  #7  
Old 06-10-2020, 09:19 AM
berdmann berdmann is offline
VanDyke Technical Support
 
Join Date: Aug 2017
Posts: 360
Hi Dino,

I am glad to hear that it is working for you now!
__________________
Thanks,
--Brittney

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 07:59 AM.