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-10-2010, 12:24 PM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 149
waitforstrings problem

I wrote a script using the waitforstrings command. I want to be able to use a variable as a timeout value so it can be changed, but when I do this, the waitforstrings command is interpreting it as one of the strings to look for. For example,

Code:
result = crt.Screen.waitforStrings(Prompt , "More" , 5)
returns a 0 on a timeout and doesn't trigger on a "5" returned in the data stream.

But if I do this:
Code:
timeout = 5
result = crt.Screen.waitforStrings(Prompt , "More" , timeout)
waitforstrings will trigger on a 5 in the returned data and result gives a 3.

Any idea how to workaround this? Is this a bug? Most of the outputs are showing IP addresses, so a 5 is usually there.

Currently using 6.5.3 (build 490)

Thanks,

Wes
Reply With Quote
  #2  
Old 06-10-2010, 01:32 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Hi Wes,

I'm not sure exactly what in the code could be causing the behavior you're seeing, but it's clear that VBScript is treating "timeout" as a string in your WaitForStrings call. There are a number of things which could cause this.

For example, an InputBox returns a string; any VBScript function that returns a string will result the variable being treated as a string. Whatever the cause of timeout being treated as a string, there is a simple solution.

You can force timeout into an integer by wrapping it in CInt().
Code:
result = crt.Screen.waitforStrings(Prompt , "More" , CInt(timeout))
Can you try this and see if it helps?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 06-10-2010, 02:01 PM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 149
Nice! that works.

FYI I was originally using this 'timeout' variable in a "waitforstring" command which did work. It was only when I changed it to a "waitforstrings" command that there was a problem.

Wes
Reply With Quote
  #4  
Old 06-10-2010, 03:44 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Hi Wes,

I'm glad to hear that worked. I ran several tests and am pretty sure there's not an inherent problem with WaitForStrings. I get consistent and expected results with simple tests.

It makes sense that the problem would only surface for you with WaitForStrings. WaitForString knows that the second parameter is a timeout, which is going to be a number. WaitForStrings takes 'n' strings and doesn't necessarily take a timeout, so WaitForStrings needs to make a choice about the last parameter.

I'm not sure if you're interested, but if you would like to post more of the code we might be able to pinpoint exactly when/where/why the variable is treated as a string in your code. Interested?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #5  
Old 06-11-2010, 06:55 AM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 149
Thanks Michael,

I did find where I went wrong. The script reads commands from a file and allows changing the timeout value. It was reading this in as a string. For what it's worth, I've included the script, but as you can see it's written with total disregard of pretty much all scripting conventions...

The comments help explain using it. We use this in my group so users can run different repetitive things without me having to write a specific script every time.

Code:
#$Language="VBScript"
#$Interface="1.0"

'-------------------------------------------------------------------------
'# This script will take commands from a specifed file and send them
'# to the connected device. 

'# When run, the script will prompt you to open the file containing the
'# commands to be run. The prompt command is the only required command
'# that must be the first line of the command file!

'# The script will loop through the commands in the 'command file' until 
'# stopped or the 'exitscript' command is found. The command file can have 
'# comment lines lines that start with the pound symbol (#) or have blank lines.

'# The Command File can be changed dynamically and the script will re-read
'# it the next time through the list of commands.

'# Supports and intercepts the pause, pacing, timeout, exitscript and  
'# prompt commands in the command file. All of these special commands 
'# with the exception of the pause command are persistent, so once set, 
'# that value will be used until changed. Each of these commands MUST 
'# be left justified and be the only command on a line.

'# Enter these special commands as shown below, but note that none of 
'# these require a specific number of characters. The xx's are just to
'# show a required entry:
'#
'#   prompt "xxx" ' wait for the prompt specified within quotes. REQUIRED!
'#   pacing xxxx  ' mSec between commands. Must be > 0. Default 50mSec
'#   pause xx     ' Number of seconds to pause
'#   timeout xx   ' Number of seconds to wait for the prompt. Default 5.
'#                ' Script will terminate if the timeout occurs.
'#   exitscript   ' Script will exit when this command is found. Usefull if
'#                ' you don't want the script to run in a loop.

'# The prompt command MUST be the first non-comment, non-blank line in the file.

'# The prompt command (maybe in combination with the timeout command) and could
'# be used to look for events as well as prompts, but in either case,
'# the prompt command change must be specified prior to the command
'# which will change the prompt. For Example you would enter:
'#
'#  prompt "(config)#"        ' To change the value being looked for
'#  config                    ' Command that will result in a changed prompt
'#
'# in the command file to change the prompt the script will look for
'# when the config command is entered.

'# In newer versions of SecureCRT (6.3 and above), the script will update
'# the status line to indicate what the script is currently doing.

'# Version 1.1, WesR
'-------------------------------------------------------------------------

Option Explicit
Const ForReading = 1, ForWriting = 2, ForAppending = 8 
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

Dim objDialog, fso, f, ts, FileName, FileNm, sTemp
Dim text, tempStr, Result, RunCnt, Status, Temp
Dim Commands(1000), cmdCnt, Index
Dim Timeout, Prompt, Pause, Pacing
Set text = crt.Screen

' -------- Default Time Between commands in mSeconds. --------
Pacing = 50

' -------- Default seconds to wait for the prompt after sending a command. --------
Timeout = 5

Sub Main
	' -------- Check for crt.Session.SetStatusText support --------
	On Error Resume Next
		result = crt.Session.SetStatusText("Ready")
		if Err.Number = 0 Then
			' SetStatusText supported
			Status = 1
		End If
	On Error GoTo 0

	OpenFile ' -------- Prompt the user for the file --------
	LoadFile ' -------- Read the file into an Array --------
	
	
	' -------- Main Loop --------
	Do
		RunCnt = RunCnt + 1
		
		' -------- Get the initial "Prompt" command --------
		sTemp = Commands(0)
		if LCase(Left(sTemp,6)) = "prompt" Then 
			' -------- Get only the characters between the two quote (") characters... --------
			Prompt = Mid(sTemp,instr(sTemp,Chr(34))+1,Instr(InStr(sTemp,Chr(34))+1,sTemp,Chr(34))-1 -instr(sTemp,Chr(34)))
		Else
			MsgBox "Prompt Command is Not the First Command!" & vbLf & vbLf & vbTab & "Exiting..."
			Exit Sub
		End If	
	
		' -------- Read each line of the array looking for --------
		' -------- Prompt, Pacing and Timeout changes and Pause and Exitscript commands --------
	
		for index = 1 to cmdCnt
			sTemp = Commands(index)
			tempStr = LCase(sTemp)
			if instr(tempStr," ") then tempStr = Left(tempStr,instr(tempStr," ")-1)
			select case tempStr
			
				Case "prompt"
					' Get only the characters between the two " characters...
					Prompt = Mid(sTemp,instr(sTemp,Chr(34))+1,Instr(InStr(sTemp,Chr(34))+1,sTemp,Chr(34))-1 -instr(sTemp,Chr(34)))
				Case "pacing"
					sTemp = trim(sTemp)
					Pacing = Mid(sTemp,instr(sTemp," ")+1,Len(sTemp)-instr(sTemp," ")+1)
				Case "pause"
					sTemp = trim(sTemp)
					Pause = Mid(sTemp,instr(sTemp," ")+1,Len(sTemp)-instr(sTemp," ")+1)
					For temp = Pause to 1 Step -1
						If Status then crt.Session.SetStatusText "Count=" & RunCnt & " : Pausing " & Temp & " Seconds" 
						crt.sleep 1000
					Next
				Case "timeout"
					sTemp = trim(sTemp)
					Timeout = CInt(Mid(sTemp,instr(sTemp," ")+1,Len(sTemp)-instr(sTemp," ")+1))
				Case "exitscript"
					MsgBox "Exiting Script at Exit command"
					Exit Sub
				Case Else
					' -------- Send the command and wait for prompt --------
					If Status then crt.Session.SetStatusText "Count=" & RunCnt & " : Cmd = " & sTemp
					text.synchronous = True
					text.send sTemp & vbCr
					Result = text.waitforStrings(Prompt , "More" , timeout)
					If Result = 0 Then
						MsgBox "Failed to get Prompt <" & Prompt & ">1"
						Exit Sub
					End If
					If Result = 2 then
						Do While Result = 2
							text.send " "
							Result = text.waitforStrings(Prompt , "More" , timeout)
							If Result = 0 Then
								MsgBox "Failed to get Prompt <" & Prompt & ">2"
								Exit Sub
							End If
						Loop
					End If
					text.synchronous = False
					crt.sleep Pacing
			End Select
		Next
		
		' -------- Open file and reload the Array to support dynamic changes --------
		RereadFile
		
	Loop
end sub

Sub OpenFile
	' -------- Open file dialog --------
	Set fso = CreateObject("Scripting.FileSystemObject") 
	Set objDialog = CreateObject("UserAccounts.CommonDialog")
	objDialog.Filter = "Text Files|*.txt|VBScript Scripts|*.vbs|Log Files|*.log|All Files|*.*"
	objDialog.FilterIndex = 1
	Result = objDialog.ShowOpen
 	If Result = 0 Then
		exit sub
	End If
	'set f = fso.GetFile("D:\Profiles\mgi2581\My Documents\SecureCRT_Logon_Scripts\Command.txt")
	Set f = fso.GetFile(objDialog.FileName)   
	Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault) 
	FileNm = objDialog.FileName
End Sub

Sub RereadFile
	' ------- Reopen and re-read the Command file to dynamically take updates -------
	Set ts = fso.OpenTextFile(FileNm)
	LoadFile
End Sub

Sub LoadFile
	' -------- Load the commands into an array --------
	cmdCnt = 0
	Do While ts.AtEndOfStream <> True
		sTemp = ts.ReadLine
		If Left(sTemp,1) <> "#" and Trim(sTemp) <> "" Then
			Commands(cmdCnt) = sTemp
			cmdCnt = cmdCnt + 1
		End If
	Loop
	cmdCnt = cmdCnt - 1
	ts.close
End Sub
Reply With Quote
  #6  
Old 06-11-2010, 11:25 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Thanks Wes,

Reading the variable in as a string explains it. I've started reading through the script and am glad you posted it, scripting conventions or not!
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #7  
Old 06-11-2010, 11:33 AM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 149
No problem. Hopefully someone can get some use out of it!

Wes
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 08:32 AM.