Skip to content

Basic Commands

Matthew Spangler edited this page Sep 17, 2021 · 12 revisions

Sending commands to Cisco devices:

  • get_command_output(command)

This function sends a command to the current tab in SecureCRT, waits for CLI output, and then returns the output as a list, with each new line delimiting a new element in the list. This function automatically sends a return character, '\r'.

This function distinguishes input from output by waiting for a prompt, such as Router> or Switch(config)#. This function will not finish running until it sees a prompt. Some commands like #reload and #copy running-config startup-config will ask you a question before/instead of giving you a prompt. If you use send_receive() on commands like these, it will hang because it will still be waiting for a prompt. For these, use send() instead.

Parameters:

- command - the command to send
- skip_exceptions - if true, displays exception messages during errors. Cancels the script.
- timeout - the time in seconds to wait for command output

Example:

sh_run_output = xe_runner_instance.send_receive("sh run | b interface")

  • send(command)

Use this function when you need to send a command to IOS but there is no CLI output, or you don't need to return the output. Some Cisco commands like #no shut don't give any output.

Note: this command does not work for sending masked text, such as for passwords. Use crt.Screen.Send() instead.

This function is also useful when the command output will be anything other than the prompt. The prompt might be something like: Router> or Switch(config)#. Some commands like #reload and #copy running-config startup-config will ask you a question before/instead of giving you a prompt. If you use send_receive() on commands like these, it will hang because it cannot find a prompt.

Parameters:

- command - the command to send
- wait_for - a string to wait for before continuing with the script
- timeout - the time in seconds to wait for the wait_for string before continuing with the script

Example:

xe_runner_instance.send("no shut \r", "#")

  • send_wait_for_strings(command, wait_for_strings)

The same as send() but you can wait for multiple strings instead of just one.

Example:

wait_fors = ["!!!", "..."]
nxos_runner_instance.send_wait_for_strings("ping 192.168.1.1 \r", wait_fors):

Switching between modes:

Please use functions included in this library when switching between modes like "configure terminal" and "privileged exec". If you don't, scripts will hang because the prompt changes, and the scripts need to know what the prompt is to seperate input from output.

Examples:

nxos_runner_instance.user_exec()
nxos_runner_instance.priv_exec()
nxos_runner_instance.global_config()
nxos_runner_instance.goto_intf_config("Gi1/1")

Other useful functions:

save_changes()
- the equivalent of:
    #copy running-config startup-config
    
show_intf_brief()
- returns a brief summary of interfaces

get_intf_names()
- returns only the names of all interfaces, in a list

get_mgmt_vrf()
- return the vrf name for the management interface

write_output_to_file(command, file):
- save the output of a command to a file, uses send_receive()