cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
10742
Views
0
Helpful
3
Replies

How to run script in SecurityCRT to send commands line by line to router

wfqk
Level 5
Level 5

Hi When I send a batch of commands to router, the some commands cannot be sent to router correctly. SecurityCRT has some suggestion for this. As it suggested, i did all steps as the below. but my question is how to use it and how to associate it with my purpose, which is to send commands line by line? Anyone can give some suggestion? Thank you

https://www.vandyke.com/support/tips/echoflowctrl.html

Create a button command to run a script

The Run Script command lets you employ a powerful script in VisualBasic, Jscript, Python, or other scripting language, with full access to the SecureCRT interface and shell functionality. SecureCRT 6.6 and later allows arguments to be passed to the script being launched. If you don't have scripts of your own, first download a script from the Support Scripting FAQ section on the VanDyke Software website to your local SecureCRT Scripts folder – or you can choose one of the three sample scripts installed with SecureCRT for Windows. Then you will attach the script to a SecureCRT button command.

  1. Download a script from Example Scripts for Windows (we suggest SendData.vbs or SendFile.vbs)or Example Python Scripts for Windows, Mac, and Linux.
  2. Save the file to a local folder.

Now you are ready to map the script to a button.

  1. Right-click on an empty area of the button bar and select New Button... to bring up the Map Button dialog.
  2. Choose Run Script from the Function drop-down list.
  3. Click on the center Browse '...' file selector button and navigate to your script file. Click Open.
  4. Enter a button label and click OK.

3 Replies 3

Philip D'Ath
VIP Alumni
VIP Alumni

I think you mean SecureCRT.

Perhaps post a section of script with the issue, and state what platform you are using (Windows, Linux, etc) as SecureCRT uses a different scripting engine on different platforms.

CSCO12157625
Level 1
Level 1

This is an extremely old thread, but I'd like to share.  Here is how I paste to a device:

# $language = "Python"
# $interface = "1.0"

import re

lines = []
timeout = 90
prompt = ""
command_send_suffix = "\r"  # Default suffix
abort_send = False

def main():
    global lines, prompt, command_send_suffix, abort_send
    if not crt.Session.Connected:
        crt.Dialog.MessageBox("NOT CONNECTED")
        return
    tab = crt.GetScriptTab()
    tab.Screen.Synchronous = True
    tab.Screen.IgnoreEscape = True
    clipboard_text = crt.Clipboard.Text.strip()
    if not clipboard_text:
        crt.Dialog.MessageBox("NOTHING TO SEND")
        return
    clipboard_text = clipboard_text.replace("\r\n", "\n").replace("\r", "\n")
    lines = clipboard_text.split("\n")
    prompt = tab.Screen.Get(tab.Screen.CurrentRow, 0, tab.Screen.CurrentRow, crt.Screen.Columns).strip()[-2:]
    #crt.Dialog.MessageBox("|" + prompt + "|")
    for line_number, line in enumerate(lines):
        crt.Session.SetStatusText("Sending line {} of {}".format(line_number + 1, len(lines)))
        if line.startswith("#>") or line.startswith("!>"):
            crt.Sleep(500)
        if abort_send:
            break
        line = line.strip()
        if not line:
            continue
        if line.startswith("#") or line.startswith("!"):
            comment_line = (line + "=" * 125)[:125]
            tab.Screen.Send(comment_line + command_send_suffix)
        else:
            hash_index = line.find("#")
            exclam_index = line.find("!")
            if hash_index > 0 or exclam_index > 0:
                comment_index = min(index for index in [hash_index, exclam_index] if index != -1)
                line = line[:comment_index].strip()
            tab.Screen.Send(line + command_send_suffix)
        wait_for_command_end(tab)
        handle_response(tab)
    crt.Session.SetStatusText("Ready")
    crt.Dialog.MessageBox("PASTE COMPLETE")

def wait_for_command_end(tab):
    global command_send_suffix
    tab.Screen.WaitForString(command_send_suffix, timeout)

def handle_response(tab):
    global prompt, abort_send
    shell_prompts = [
        "[confirm]",
        "authorization failed",
        "Incomplete command.",
        "syntax error",
        "contains STBY module",
        "^",
        "more",
        "More",
        prompt
    ]
    match_index = tab.Screen.WaitForStrings(shell_prompts, timeout)
    if match_index == 0:  # Timeout
        crt.Dialog.MessageBox("TIMEOUT SENDING COMMAND.")
        abort_send = True
    elif match_index == 1:  # [confirm] detected
        tab.Screen.Send(command_send_suffix)
        wait_for_command_end(tab)
        handle_response(tab)        
    elif match_index in [2, 3, 4, 5, 6]:  # Error conditions
        tab.Screen.Send("\x15") # ctrl-u
        crt.Dialog.MessageBox("ERROR DETECTED")
        abort_send = True
    elif match_index in [7, 8]:  # 'more' prompt
        tab.Screen.Send(" ") # continue
        handle_response(tab)

main()

It is indeed an old post. The basic problem is that the Cisco device assumes that a person is entering text and sets up a buffer to accept and process the input. When a person is indeed typing the commands it works quite well. But if you have the commands in a file and are transmitting the file, and if the file is greater than some value, it is easy to overrun the buffer and your transmission fails. 

I like your python script. But the OP was about SecurCRT, and there is a simple solution using that tool. In settings there are entries for character delay and for line delay. They default to fairly low values (which increase the efficiency of the transmission but which introduce the possibility of over running the buffer). Change one or both of the settings to some higher value (may take some experimentation to determine the optimum value) and the problem will be solved. 

HTH

Rick