#1
|
||||
|
||||
![]()
A frequent question is often asked which comes in various forms generally resembling:
Why doesn't my script work like I need it to?If you have not yet taken any time to read through the scripting essentials guide, we strongly urge you to read through it and become familiar with the concepts -- at least chapter 4, as it provides specific guidance as to how to ensure that you can keep things "in sync". The purpose of this FAQ is to highlight common pitfalls that a number of first-time script writers encounter (and even long-time script writers such as myself); following each pitfall highlighted, a corresponding "Best Practice:" will be included. Pitfall: Sending commands before the remote is ready to receive anything. One mistake many first-time SecureCRT script writers make is failing to ensure that the remote system is ready to receive commands before actually sending any commands. The crt.Screen.Synchronous property being set to True is essential for keeping things in sync. Be aware that when crt.Screen.Synchronous is set to True and while the script is running:
In any script that performs a connection (or is a logon script itself) always call Screen.WaitForString("<your-shell-prompt-text-here>") near the beginning of your script to a) make sure the host is ready to receive commands, and b) to consume any output that may be buffered (not yet displayed to the screen) so that it doesn't interfere with any WaitFor*() or ReadString() calls following your Send(). Pitfall: Calling Send() multiple times in succession without intervening WaitForString() calls after each Send(). Another pitfall first-time script writers encounter is the mistake of performing two or more Send() operations in succession (where they are "pressing" {Enter} as part of each Send()) but then fail to wait for the shell prompt to appear after each first Send() before the next Send() is performed. In the case of this mistake, a WaitForString() call made after successive Send() calls (again, where the Send() involves "pressing" {Enter}) will end up finding the shell prompt that appeared as a result of the first Send(). Best Practice: For each Send() where the text you're sending involves carriage return ("\r" in python, or vbcrlf -- same as chr(13) -- for VBScript), make sure you have a matching WaitForString() call. Pitfall: Waiting for a simplistic single-character shell prompt when the output of a command might include that character. This pitfall involves the script writer being unaware of the ramifications of the output of commands potentially including the very character they are waiting for as an indication of the command completing. To be certain, a script writer should be waiting for the full CLI shell prompt (as long as the full shell prompt isn't just a ">" or a "#") because it's less likely that the output of any command would include that as part of its data. If all you are doing is waiting for a "#" and the output of your command has one of those "#" characters, then the subsequent WaitForString("#") call is going to return after finding the "#" in the command's output, rather than the "#" that is part of the CLI shell prompt. Best practice: Wait for the entire shell prompt to appear so that there aren't any false positives triggered by the output of the command you're running. Pitfall: Output doesn't appear on the screen in real time (or until after the script has completed). Individuals will sometimes report something similar to the following: While a script is running, I observed that the text received from the connected host isn't displayed in real time. Only when the script execution is complete (or when the script is cancelled), do I see the commands/output printed to the screen. Is there any way to make it real-time?In such cases where your script is looping and only performing Screen.Send() operations after a delay, if you don't see output appear in SecureCRT's terminal window until after the script has terminated, it means that your script has set Screen.Synchronous = True, but you're never calling any Screen.WaitFor*() or Screen.ReadString() methods. As mentioned earlier in this post, Quote:
If all your script does is send a command every 5 seconds, and you're never using Screen.WaitFor*() or Screen.ReadString(), then make sure that Screen.Synchronous = False in your code; otherwise, output received from the connected host will be queued up for display and will only appear after your script has terminated/canceled. Alternatively, if you want to keep Screen.Synchronous = True (because at some point you might care about waiting for specific text to appear later), then insert a Screen.WaitForString() call in your loop, passing in a string of text that you'll never expect to find, with a timeout value equal to the number of seconds you wish to pause in your loop, instead of using crt.Sleep() to do your pauses. For example: Code:
# $language = "Python3" # $interface = "1.0" crt.Screen.Synchronous = True # Send the same command over and over until the script is canceled; while True: crt.Screen.Send("show ip interface brief\r") # Wait for 5 seconds before looping. Use WaitForString() # so that non-matching text will appear on the screen as # soon as it has been received: crt.Screen.WaitForString("...Never expecting to find this text...", 5) Is there another approach to keeping things "in sync"? Another way to sync things up is to take the approach of:
Code:
strShellPrompt = "MyHost'sShellPromptText# " ' 1. Send the command (but don't "press" {Enter}!): strCommand = "ls -alR /" crt.Screen.Send strCommand ' 2. Wait for the text of the command to appear: crt.Screen.WaitForString strCommand ' 3. Now "press" {Enter} to run the command crt.Screen.Send vbcr 'same as chr(13), but easier to type ' 4. Now wait for the shell prompt to appear: crt.Screen.WaitForString strShellPrompt
__________________
Jake Devenport VanDyke Software Technical Support YouTube Channel: https://www.youtube.com/vandykesoftware Email: support@vandyke.com Web: https://www.vandyke.com/support Last edited by jdev; 10-27-2021 at 09:48 AM. |
![]() |
Tags |
faq , how-to , readstring , scripting , send , synchronous , waitforstring |
Thread Tools | |
Display Modes | Rate This Thread |
|
|