Securecrt Script Loop



Loop/Repeat command Scripting. Hi e2script, I think the SecureCRT method that will suit your task best is WaitForKey.See Help file topic Scripting / Script Objects Reference / Screen Object. Class securecrttools.scripts.Script(scriptpath) ¶ This is a base class for the script object. This class cannot be used directly, but is instead a blueprint that enforces what any sub-classes must implement. Thanks a lot for your resonse; I was beginning to think I wasn't going to get one! REgExp = Short for Regular Expressions crt.Session commands are SecureCRT scripting commands that directly interface with the application. 'crt.Session.Connect' sends a command to the app to connect to connect to a host using variables such as host, protocol, port number ect. SecureCRT is a great tool, apart from using secure shell or other protocols to manually log in to a remote server, it allows us to write a script to automatically log into a remote server. We can also send any command and retrieve the results in the form of a string. The script itself can be either in VBScript or JScript and it can be run using. Sample outputs: Welcome 1 times. Welcome 2 times. Welcome 3 times. Welcome 4 times. Welcome 5 times. The script initializes the variable n to 1, and then increments it by one.

VbsEdit, the award-winning VBScript editor that dramatically reduces the time you spend writing .VBS scripts

Sample scripts

SecureCrt


# $language = 'VBScript'
# $interface = '1.0'
'library:c:mytypelibsecurecrt.tlb
'Dim crt As SecureCrt.crt
' This script demonstrates how to capture line by line output
' from a command sent to a server. It then saves each line of output
' to a file. This script shows how the 'WaitForStrings' command can be
' use to wait for multiple possible outputs.
' Constants used by OpenTextFile()
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Sub Main
crt.Screen.Synchronous = True
' Create an instance of the scripting filesystem runtime so we can
' manipulate files.
'
Dim fso, file
Set fso = CreateObject('Scripting.FileSystemObject')
' Open a file for writing. The last True parameter causes the file
' to be created if it doesn't exist.
'
Set file = fso.OpenTextFile('c:tempoutput.txt', ForWriting, True)
' Send the initial command then throw out the first linefeed that we
' see by waiting for it.
'
crt.Screen.Send'a.out' & Chr(10)
crt.Screen.WaitForStringChr(10)
' Create an array of strings to wait for.
'
Dim waitStrs
waitStrs = Array( Chr(10), 'linux$' )

Dim row, screenrow, readline, items
row = 1
Do
WhileTrue
' Wait for the linefeed at the end of each line, or the shell prompt
' that indicates we're done.
'
result = crt.Screen.WaitForStrings( waitStrs )
' If we saw the prompt, we're done.
If result = 2Then
ExitDo
EndIf
' The result was 1 (we got a linefeed, indicating that we received 
' another line of of output). Fetch current row number of the 
' cursor and read the first 20 characters from the screen on that row. 
' 
' This shows how the 'Get' function can be used to read line-oriented 
' output from a command, Subtract 1 from the currentRow to since the 
' linefeed moved currentRow down by one.
' 
screenrow = crt.screen.CurrentRow - 1
readline = crt.Screen.Get(screenrow, 1, screenrow, 20 )
' NOTE: We read 20 characters from the screen 'readline' may contain 
' trailing whitespace if the data was less than 20 characters wide.
' Write the line out with an appended 'rn'
file.Write readline & vbCrLf
Wend
Loop
crt.screen.synchronous = false
EndSub


VbsEdit provides Intellisense for SecureCRT!

Securecrt Script While Loop

Securecrt script loop syntax
Securecrt Script Loop

This package includes VbsEdit 32-bit, VbsEdit 64-bit, HtaEdit 32-bit and HtaEdit 64-bit.
The evaluation version never expires.


Script

Home
Copyright © 2001-2020 adersοft

*Note: I ran this on Version 6.2.2 (build 263), but I will test it Monday on the latest version to verify it does not need any tweaks.

Just a quick and dirty tip here. Scenario: You need to collect the output of a bunch of operational commands in Junos (or any other remote or console connected command line environment) – but you don’t want to sit in front of the screen and wait for the output of each command to complete before entering the next command. If you’re using SecureCRT, you’re in luck.

Solution: Modify and run the script outlined below to auto-magically do the waiting for you. It will send the first command, wait for it to complete, and then send the next, and so on, until there are no more commands to loop through. Technically, you could do this with an op script, but that’s only for Junos, and it would take longer to setup and modify when needed. This tool is more versatile and can be modified and used quickly – and for whatever type of device you’re connecting to.

Step 1

Create a text file with each operational command listed (one per line). *Don’t forget the “| no-more” to ensure all of the output is sent to the screen without user interaction. This is obvious, but:

Then save it as text.

Step 2

Script

Modify the script below as follows:

  1. Rename the path to point to the text file you created above. Look for the following on line 19 and change the part I bolded here: Set file = fso.OpenTextFile(“C:pathtoyourfile.txt“, ForReading, False)
  2. Determine what your command prompt looks like and modify the part of the script that looks for it. If you’re going to run this script on multiple devices as the same user, you may want to just put “user@” find the command prompt. That way you don’t have to modify the hostname on each device you run it on. Look for the following on line 35 and changes the bolded part: crt.Screen.WaitForString “[email protected]>
  3. Save the file as whatever.vbs – make sure it doesn’t save as a text file though (whatever.vbs.txt). If you’re unsure, open a command prompt and cd to the directory it’s in, then do a “dir” command to see the actual filename. If it’s .vbs.txt, issue: rename whatever.vbs.txt whatever.vbs. I don’t know if SecureCRT runs on Linux, but ls and mv instead of dir and rename if you don’t know already.

Step 3

In SecureCRT:

Securecrt Script Loop Example

  1. Transfer -> Receive ASCII — then choose a name for the file the screen output will save to.
  2. Script -> Run — then find the script and run it.
  3. Transfer -> Receive ASCII — disable the receive
Securecrt Script Loop

I know that this could be done a million other ways and/or could be enhanced to be more powerful, but I ran into a situation today where I needed something quick and dirty to grab the output of a bunch of operational commands with extensive output and this was easy to do and got the job done quickly.

Hope it helps!