Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 10-18-2017, 09:54 AM
tcurtis45 tcurtis45 is offline
Registered User
 
Join Date: Jan 2016
Posts: 15
Issue with simple script

I am having an issue with a very simple section of a script I am creating. I have a much more complicated script that I pulled this section out to illustrate it. I am at a loss after a few thousand lines of code.

The issue is the msgBox sResult will popup before the two previous commands, crt.screen.send "dir | include ....", and obviously has the wrong information because it is being executed too early.

What am I missing here? Not only that, but the sresult comes back as the hostname with additional line feeds (even though I have trimmed them). Here is the output from my router when I do this:

Any ideas? Thanks.

Tony


Quote:
GenItTestSW1#$assword@1.1.1.1/oh-3560cx-L2-sw1.txt flash:
Destination filename [oh-3560cx-L2-sw1.txt]?
%Warning:There is a file already existing with this name
Do you want to over write? [confirm]
**WARNING** **WARNING** **WARNING**

This computer system is restricted to XXXXX authorized users for XXXXX business purposes only. The unauthorized access, use or modification of this system, data contained in this system, or data in transit to or from this system is subject to criminal penalties and civil liability, and there is no reasonable expectation of privacy regarding such communications or data.
At any time and for any lawful purpose, XXXXX may monitor, intercept, record, and search any communications or data transiting or stored on this information system. At XXXXX sole discretion,XXXXX may disclose pertinent information to the U.S. Government and its authorized representatives to protect the security of critical infrastructure and key resources, ensure information security, or to comply with any applicable law, regulation, legal process, or enforceable government request.
By using this system, you agree to and understand these terms. With your help, we can continue to protect XXXXX systems and information.!!!!
31450 bytes copied in 1.867 secs (16845 bytes/sec)
GenItTestSW1#
GenItTestSW1#dir | include c3560cx-universalk9-tar.152-4.E.tar
591 -rwx 26726400 Oct 18 2017 10:20:14 +00:00 c3560cx-universalk9-tar.152-4.E.tar
GenItTestSW1#
GenItTestSW1#
GenItTestSW1#
GenItTestSW1#
GenItTestSW1#
GenItTestSW1#!sResult:
GenItTestSW1#
GenItTestSW1#GenItTestSW1
Trying GenItTestSW1.duke-energy.com (10.250.38.125)... Open



Password required, but none set

[Connection to GenItTestSW1 closed by foreign host]
GenItTestSW1#
Code:
#$Language="VBScript"
#$Interface="1.0"


g_DeviceIOS	= "c3560cx-universalk9-tar.152-4.E.tar"						

g_PromptExtension = "#"


Sub Main
 
 
	crt.Screen.Synchronous = True
	
	
	crt.screen.send "copy scp://username:password@1.1.1.1/oh-3560cx-L2-sw1.txt flash:" & vbcrlf & vbcrlf
	
	crt.screen.WaitForString "#", 90
 	'crt.sleep 5000
	crt.screen.send "dir | include " & g_DeviceIOS & vbcrlf	
	Dim sResult
	sResult = crt.Screen.ReadString(g_PromptExtension)
	msgBox sResult
	
	set reIOSPattern = New RegExp
	reIOSPattern.pattern = ".*("& g_DeviceIOS & ")"
	
	Set matches = reIOSPattern.Execute(sResult)
			For each match in matches
				sIOSPattern = match.SubMatches(0)
			Next
	
	crt.screen.send vbcrlf & vbcrlf & "!sResult: " & trim(sResult) & vbcrlf &_
			"! IOS Pattern: " & sIOSPattern & vbcrlf &_
			"!g_DeviceIOS = " & g_DeviceIOS & vbcrlf &_
			"! sIOSPattern = " & sIOSPattern & vbcrlf
	
	
	
	
End Sub
Reply With Quote
  #2  
Old 10-18-2017, 11:03 AM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 654
Hi tcurtis45,

With Synchronous set to true, there are a few things that may be causing you to get out of sync.

As a rule of thumb you will want to send one command and one vbcr (not vbcrlf) then wait for the results. I would suggest modifying your script so that each send command only includes one command and one vbcr, and ensure that with each send command you follow up with a WaitForString that to ensure you stay synced.

Another option to help illustrate the required format would be to use the Script Recorder to record you manually completing these steps. You would simply choose Start Recording Script from the main Script drop down menu, manually complete these steps, then choose Stop Recording Script. This will prompt you to save the results, these results should help demonstrate the structure you would want to use.

Does this information help?
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 10-18-2017, 12:39 PM
tcurtis45 tcurtis45 is offline
Registered User
 
Join Date: Jan 2016
Posts: 15
Issue with simple script - cont

Eric,

Thanks for the follow up. I see what you are saying about the vbcr & wait for each step. I have modified my script to do that. However the second issue is still happening when using the ReadString function. Why is it sending results before preceding lines in the code have been run?

I have updated the code below. The issue occurs on line 38, msgbox "! show run data: " & sResult. The dialog is popping up before the previous two commands of 'term len 0' and 'show run | inc Gig.*0/1' have even executed on the screen. It therefore is showing existing data on the screen in the sResult, rather than the output from what should be the last command executed.

Thanks for the feedback.

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


g_DeviceIOS	= "c3560cx-universalk9-tar.152-4.E.tar"						' Set by Gen.IT on file output

g_PromptExtension = "GenItTestSW1#"


Sub Main
 
 
	crt.Screen.Synchronous = True
	
	
	crt.screen.send "copy scp://username:password@1.1.1.1/oh-3560cx-L2-sw1.txt flash:" & vbCr	
	nresult = crt.screen.WaitForStrings ("Destination filename")
	crt.screen.send vbCr
	
	Dim nresult
	nresult = crt.screen.WaitForStrings ("GenItTestSW1#","Do you want to over", 90)
	'msgbox nresult
	if nresult = 2 Then 
		crt.screen.send vbCr
		nresult = crt.screen.WaitForStrings ("GenItTestSW1#", 90)
		If nresult = 1 Then
			crt.screen.send "! Found Prompt" & vbCr
		Else
			crt.screen.send "! nresult is: " & nresult & vbCr
		End If
	End If
	
	'crt.sleep 5000
	crt.screen.send "term len 0" & vbCr
	crt.screen.send "show run | inc Gi.*0/1" & vbCr
	Dim sResult
	sResult = crt.Screen.ReadString("GenItTestSW1#",1)
	msgbox "! show run data : " & sResult
	
	crt.screen.send vbCr
	crt.screen.WaitForStrings "GenItTestSW1#",1
	crt.screen.send "dir | include " & g_DeviceIOS & vbCr
	sResult = crt.Screen.ReadString("GenItTestSW1#",1)
	crt.screen.send "! sResult is : " & sResult
	
	set reIOSPattern = New RegExp
	reIOSPattern.pattern = ".*("& g_DeviceIOS & ")"
	
	Set matches = reIOSPattern.Execute(sResult)
			For each match in matches
				sIOSPattern = match.SubMatches(0)
			Next	
	crt.screen.send vbcrlf & vbcrlf & "!sResult: " & trim(sResult) & vbcr &_
			"! IOS Pattern: " & sIOSPattern & vbcr &_
			"!g_DeviceIOS = " & g_DeviceIOS & vbcr &_
			"! sIOSPattern = " & sIOSPattern & vbcr
	'crt.sleep 5000
	
	
	
	
End Sub

Last edited by tcurtis45; 10-18-2017 at 12:45 PM.
Reply With Quote
  #4  
Old 10-18-2017, 02:36 PM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 654
Hi tcurtis45,

I believe that what has been sent to the remote and what is displayed to your screen is out of sync. One way to help ensure this does not happen is to use a wait or readstring after every command/vbcr sent.

Before line 38, where the issue becomes apparent, the last WaitFor I see is line 25. Between that and the msgbox there are three different sends that happen without a wait or read. Sending something without waiting after will cause your screen and the underlying script/incoming data to get out of sync.

One option may be to do something like:

Code:
crt.screen.send "! Found Prompt" & vbCr
crt.screen.WaitForString ("! Found Prompt" & vbCr)
crt.screen.WaitForString ("#")
This would ensure that you send your command, the screen stays in sync by displaying the command, and it waits until you are at the prompt before sending the next command.

Because you will want to do this for nearly every command it may be best to create a function that does this for you.

This would look something like the below:

Code:
AddComment("! Found Prompt")

Function AddComment(strComment)
    crt.screen.Send(strComment & vbcr)
    crt.screen.WaitForString(strComment & vbcr)
    crt.screen.WaitForString("#")
End Function
Running commands would be much the same, you want to send the command, wait for the command, then wait for the prompt. Where this will change is if you want to capture the output of the command. If that is the case you would do not want to wait for the prompt prior to using ReadString.


Code:
crt.screen.send "show run | inc Gi.*0/1" & vbCr
crt.screen.WaitForString("show run | inc Gi.*0/1" & vbCr)
Dim sResult
sResult = crt.Screen.ReadString("GenItTestSW1#",1)
msgbox "! show run data : " & sResult
Does this help clarify?
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 10-18-2017, 03:19 PM
tcurtis45 tcurtis45 is offline
Registered User
 
Join Date: Jan 2016
Posts: 15
Thanks Got it!

Eric,

Thanks again for the quick feedback. I got it now. I just didn't realize you had to do a waitforstring after every command. I thought I could feed multiple things through and then wait. I have added some additional lines in the code to wait for vbCr, and that fixed the issue. My fixes are below.

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


g_DeviceIOS	= "c3560cx-universalk9-tar.152-4.E.tar"						' Set by Gen.IT on file output

g_PromptExtension = "GenItTestSW1#"


Sub Main
 
 
	crt.Screen.Synchronous = True
	
	
	crt.screen.send "copy scp://username:password@1.1.1.1/oh-3560cx-L2-sw1.txt flash:" & vbCr	
	nresult = crt.screen.WaitForStrings ("Destination filename")
	crt.screen.send vbCr
	
	Dim nresult
	nresult = crt.screen.WaitForStrings ("GenItTestSW1#","Do you want to over", 90)
	'msgbox nresult
	if nresult = 2 Then 					'If output contained question: overwrite [yes/no]
		crt.screen.send vbCr
		nresult = crt.screen.WaitForStrings ("GenItTestSW1#", 90)
		If nresult = 1 Then
			crt.screen.send "! Found Prompt" & vbCr
			crt.screen.WaitForString "! Found Prompt" & vbCr
			crt.screen.WaitForString "GenItTestSW1#"
		Else
			crt.screen.send "! nresult is: " & nresult & vbCr
		End If
	End If
	
	crt.screen.send "term len 0" & vbCr
	crt.screen.WaitForString vbCr
	crt.screen.send "show run | inc Gi.*0/10" & vbCr
	crt.screen.WaitForString vbCr,10
	Dim sResult
	saResult = crt.Screen.ReadString("GenItTestSW1#")
	msgbox "! show run data : saresult is : " & trim(saResult) & vbCr
	
	crt.screen.send "dir | include " & g_DeviceIOS & vbCr
	crt.screen.WaitForString vbCr, 10
	sbResult = crt.Screen.ReadString("GenItTestSW1#")
	msgbox "! sbResult is : " & trim(sbResult)
	
	set reIOSPattern = New RegExp
	reIOSPattern.pattern = ".*("& g_DeviceIOS & ")"
	
	Set matches = reIOSPattern.Execute(sbResult)
			For each match in matches
				sIOSPattern = match.SubMatches(0)
			Next	
	'crt.screen.send vbcrlf & vbcrlf & "!sResult: " & trim(sResult) & vbcr &_
	'		"! IOS Pattern: " & sIOSPattern & vbcr &_
	'		"!g_DeviceIOS = " & g_DeviceIOS & vbcr &_
	'		"! sIOSPattern = " & sIOSPattern & vbcr
	'crt.sleep 5000
	
	
	crt.screen.send "show config" & vbCr
	crt.screen.WaitForString vbCr, 20
	scResult = crt.Screen.ReadString("GenItTestSW1#")
	crt.screen.send "! scResult is : " & scResult & vbCr
	crt.screen.WaitForString vbCr, 1
	
End Sub
Reply With Quote
  #6  
Old 10-18-2017, 03:28 PM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 654
Hi tcurtis45,

Glad I could help point you in the right direction and that your issue is fixed.

Have a great rest of the day.
__________________
Thanks,
--Eric

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

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 06:49 PM.