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-04-2016, 02:08 AM
mehbooburrehman mehbooburrehman is offline
Registered User
 
Join Date: Jun 2016
Posts: 2
Make Secure CRT script wait till command finished

When I use VBS to run command on a device my script keeps running through commands until the end without waiting for previous device command to finish.

for example in the following script. The commands on the router take a few seconds to execute but the last command "msgbox("done") is executed immediately.

Is there any way to make the script wait till the commands are executed.



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

Sub Main
crt.Screen.Synchronous = True
crt.Session.Connect "/TELNET x.x.x.x 23"
crt.Screen.WaitForString "sername:"
crt.Screen.Send "xxx" & vbCr
crt.Screen.WaitForString "assword:"
crt.Screen.Send "sl@x" & vbCr

crt.Screen.Send "show ip int br" & vbCr
crt.Screen.Send "show cdp neighbors" &vbCr
crt.Screen.Send "ping 8.8.8.8" & vbCr

crt.Screen.Synchronous = False

msgbox("Done")

End Sub
Reply With Quote
  #2  
Old 06-04-2016, 10:39 AM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 919
Quote:
Code:
crt.Screen.Send "show ip int br" & vbCr
crt.Screen.Send "show cdp neighbors" &vbCr
crt.Screen.Send "ping 8.8.8.8" & vbCr
Here you're committing one of the scripting sins of calling Send() for a command without a corresponding WaitForString() to make sure that the command has completed. Well, it's not really a sin, so much as it violates best practice.

Scripting calls happen *very* quickly... so quickly in fact that the remote device to which you're connected might very well drop some/all command text you've sent after the first one because it's too busy processing the data from the first command to worry about input.

Thinking about how you would do this manually, you wouldn't type a new command into the terminal window while the remote device is displaying output from a prior command you've issued. You'd wait for an indication from the device that it's ready for another command. Typically this indication is the device's shell prompt appears, giving you a visual indication the device is ready for a command.
Do the same thing in your script code: Wait for the shell prompt as an indication that the remote device is ready for a(nother) command to run.

In the other thread you posted recently, my response mentions a few ways to go about determining what your shell prompt is.

Reliable automation code in SecureCRT will almost always exhibit a pattern of Send() followed by a corresponding WaitFor*() call or a ReadString() call (to capture output into a variable, not just wait for it). For example:
Code:
strPrompt = "Router#"
crt.Screen.Synchronous = True
' Send a command:
crt.Screen.Send("command 1")
' Wait for the remote device to be ready for another command:
crt.Screen.WaitForString(strPrompt)

' Send another command:
crt.Screen.Send("command 2")
' Wait for the remote device to be ready for another command:
crt.Screen.WaitForString(strPrompt)


' Send another command:
crt.Screen.Send("command 3")
' Wait for the remote device to be ready for another command:
crt.Screen.WaitForString(strPrompt)


' Send another command:
crt.Screen.Send("command 4")
' Wait for the remote device to be ready for another command:
crt.Screen.WaitForString(strPrompt)


' Send another command:
crt.Screen.Send("command 5")
' Wait for the remote device to be ready for another command:
crt.Screen.WaitForString(strPrompt)

' ...
Reading through the SecureCRT scripting guide can provide you with tools and guidance, including example code. Specifically, chapter 4 (section 4.2, is specific to the challenge you're facing) is a good one to read, but it's all good and provides foundational knowledge to help you understand more about possibilities with SecureCRT scripts.

Also, look at the examples of code found in the Scripting "Sticky". There you might just find examples that help you in the project you're working on.

--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-13-2016, 02:56 PM
pcdocta pcdocta is offline
Registered User
 
Join Date: Jun 2016
Posts: 1
Have you tried adding:

crt.Screen.WaitForString "#"

after each line that sends a command?
Since these commands are on cisco gear, what you need to wait for is the # character, then each command will do its things and move on to the next line..

see below...

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

Sub Main
crt.Screen.Synchronous = True
crt.Session.Connect "/TELNET x.x.x.x 23"
crt.Screen.WaitForString "sername:"
crt.Screen.Send "xxx" & vbCr
crt.Screen.WaitForString "assword:"
crt.Screen.Send "xxx" & vbCr

crt.Screen.Send "show ip int br" & vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send "show cdp neighbors" &vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send "ping 8.8.8.8" & vbCr
crt.Screen.WaitForString "#"

crt.Screen.Synchronous = False

msgbox("Done")

End Sub

Last edited by jdev; 12-13-2016 at 11:32 AM.
Reply With Quote
Reply

Tags
command delay , vbs , wait


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:22 PM.