How to write Command Portion in Template
NCCM supports two ways of writing commands in Template
1) Plain command format (Writing Device Command as it is)
In plain command format, the user writes the device commands defined by the vendor. This format will be used only inside the Command Execution & Network Diagnosis template type but not in Download Job Profiles, OS Upgrade Job, Configuration Trigger, Policy Rules, and Remedy.
Though it is simple to write the template in this format, it is not recommended since it does not allow additional information to be added to the command, such as command timeout, response prompt, and error check condition on response.
The timeout for each command using plain command format is always 30 seconds, and each command takes the full 30 seconds even if the execution has been completed before.
2) XML Command Format
In XML command format, each command is enclosed in an XML node, and additional input to the command, like command timeout, prompt, expected pattern, previous match, and action, is added to the XML node properties.
The XML command format is recommended for all NCCM features, including Download, Upload, Upgrade, and Diagnosis.
Sample command portion for changing Device hostname in plain text format and XML format:
Plain Format
XML Format
conf t
hostname
newname
exit
<command prompt=”#” timeout=”10”>conf t</command> <command prompt=”#” timeout=”10”>hostname newname</command>
<command prompt=”#” timeout=”10” action=”exit”>exit</command>
In the above example, the plain format takes the device command as it is the same way the command is executed using Putty or extreme application. Still, in XML Format, each device command will be placed inside the XML node “Data” section, and other information in the XML node property section.
XML Command Syntax
XML Command Sample
The device command for every device will be inside the Data portion of the XML Node, and the additional properties or information will be inside XML’s property portion. The property value must always be inside the Double quotes character.
NCCM supports the following properties:
timeout - value (in seconds). Every command execution is considered complete until the prompt pattern value is matched or till the timeout second count is reached.
prompt - is generally the last character of the command response that informs the command execution completion of a Device. When the response from the Device does not match the prompt, command execution is considered as COMMAND ERROR.
a. The prompt can be a single character, a word, or a line.
b. The prompt value is always a regex pattern, and it can be escaped using \ to make an exact match. Below example. (Dot) regex character is escaped with \ to consider it literally as ‘.’ (Dot) and not as a regex pattern.
Follow the URL https://regex101.com/ to verify or check the regex pattern before saving the template.
c. The prompt also supports multiple patterns (multiple single characters or multiple words) to match the command execution completion
d. When the given prompt is not matched within the specified timeout seconds, NCCM will declare it a Command error and stop or continue the execution based on Task IP/Command continuation input from Upload Job task input.
“action” property is used to
a. Inform NCCM that the exit command is executed and to not wait for a prompt.
b. Inform NCCM to store the result of the command for storing the device's configuration output and copy the command output for Trigger parsing.
The “shell” property is used to Inform NCCM to open a remote session (TELNET or SSH) from a Device for further command executions.
The “error_pattern” property checks the command response; if the pattern values match the command response, command execution is considered COMMAND ERROR. Similar to the prompt property, error_pattern can take multiple values.
Note: The prompt property checks for command completion; however, the error_pattern property checks whether the response is as expected.
Example: when the “copy tftp” command is not supported by a device, the response will be %Error opening tftp, and the error_pattern to catch the error will be
The properties below also follow the same principle as the error_pattern.
The “expected_pattern” property checks the command response; if the pattern value does not match the command response, command execution is considered a COMMAND ERROR.
The “expected_any_response” property checks the command response; if the device does not respond to any data, command execution is considered a COMMAND ERROR. The value of property is not required, and hence input can be empty double quotes
The “expected_empty_response” property checks the command response; if the device responds with any data, command execution is considered a COMMAND ERROR. The value of a property is not required, and hence, the input can be empty double-quotes
The “expected_count_response” property checks the command response; if the device response line does not equal the count value data, command execution is considered a COMMAND ERROR. The value of the property is the response line count. The count can be any number
NCCM expects a 5-line response.
The “expected_count_response” property checks the command response; if the device response line does not equal the count value data, command execution is considered COMMAND ERROR. The value of the property is the response line count. The count can be any number.
NCCM expects the response to be anything other than 5 lines.
The “expected_count_response” property checks the command response; if the device response line is less than 6, command execution is considered COMMAND ERROR. The value of the property is the count of lines. The count can be any number.
NCCM expects the response to be greater than 5 lines.
The “expected_count_response” property checks the command response; if the device response line exceeds 4, command execution is considered COMMAND ERROR. The value of the property is the count of lines. The count can be any number.
NCCM expects the response to be more than 4 lines.
The “expected_count_response” property checks the command response; if the device response line exceeds 5, command execution is considered COMMAND ERROR. The value of the property is the count of lines. The count can be any number.
NCCM expects the response to be less than 6 lines
The “type” property stores the command response under property value. NCCM stores the command output in the Operation Data store.
For example:
If the output of the command shows that the IP interface brief is required to be stored in NCCM as an Interface Brief, the XML command should be written as
Sample command to shut down an interface in plain text and XML format.
Plain Format
XML Format
conf t
int Gi 0/0
shutdown
exit
<command prompt=”#” timeout=”10”>conf t</command> <command prompt=”#” timeout=”10”> int Gi 0/0 </command> <command prompt=”#” timeout=”10”>shutdown</command> <command prompt=”#” timeout=”10” action=”exit”>exit</command>
Sample command to enable syslog in plain text format and XML format.
Plain Format
XML Format
conf t logging source-
interface Loopback100 end write memory
<command prompt=”#” timeout=”10”>conf t</command> <command prompt=”#” timeout=”10”> logging sourceinterface Loopback100 </command> <command prompt=”#” timeout=”10”>end</command> <command prompt=”#” timeout=”10” action=”exit”>write memory</command>
Below are some sample commands to replace the Device configuration file from the NCCM server.
Note: Plain text commands cannot be written since the timeout of some commands is more than 30 seconds.
NCCM also supports writing Comments inside the command portion to understand commands better. To define a line as a comment, add # character at the beginning of a line.
Example for writing Comments inside commands:
Note: At the time of execution, NCCM ignores all lines starting with # (comment lines)
NCCM Variable Substitution
NCCM follows the Jinja2 Template engine for converting command templates into actual commands. Jinja2 Template engine provides features like
Variable substitution
For substituting specific values for specific Devices
Variable declaration
Data structures like Integer, Boolean, List, and Dictionary
Loop Statements like
For o While
Do while
Conditional Statements like
If
If else
If elif else
Conditional operators like
o =
o !=
o in
o not in
o > and >=
o < and <=
Variable Substitution:
Variables are command inputs given by a user dynamically during the execution time.
For example, if the user wants to change the hostname in Cisco devices, the command syntax will be
Through variable substitution, the single template is enough to change the hostname of all devices with the same Vendor and OS Type configured in the template; otherwise, each device requires a separate template.
To substitute a variable, follow the below steps based on the condition applicable:
1) Use “Double Curly Brackets” before and after the variable {{ }}, only if the variable is not inside Jinja2 statements
Runtime is a substitution object.
NCCM Substitution Objects in Template:
NCCM supports 10 types of substitution objects for Variable substitution within the configuration template
1) Runtime object
The runtime object will be used in Configuration Upload and Network Diagnosis activities. Runtime object variables will be converted into user input form to get values while configuring the upload task or Network Diagnosis creation.
2) Global object
All Global parameters configured in NCCM are available through the Global object for Variable substitution.
3) Type object
Defines the field based on the variable type specified, such as Text Area, Text field, DropDown, and Multi DropDown.
4) Default object
I defined the default value for Type Object here.
5) Remark object
Displays the Text on mouse hover on the Variable Name.
6) Optional object
If the variable is declared ‘Optional’, the input for the field is not mandatory.
7) Check object
Ensures that the Input format matches the defined format.
8) LOCAL_SHELL object
The LOCAL_SHELL object gets values from the LOCAL_ACCOUNT profile, configured in the Device credential for Variable substitution.
9) Device object
The device object gets values from the Device database of the corresponding Device where command execution occurs.
10) Interface object
The interface object gets values from the Device Interface database of the corresponding Device where command execution takes place.
11) Job object
Job object gets values from the Job Database (Upload or Download Job) of the corresponding Device where command execution occurs.
12) Profile object
Profile object gets values from Profile Database (Configuration Profile) of the corresponding Device where command execution occurs.
13) Trigger object:
Trigger object gets values from Configuration Trigger Database of corresponding Trigger name used in Configuration Template.
Note: A template can have more than one Trigger variable.
14) Profile object:
Profile object gets values from the Device Credential Database of the corresponding Device (Device Credential) where command execution will occur.
15) Time object:
Time object gets values from the NCCM server based on current time, which is for substituting time values in a template during execution
16) Directly writing variable, if a variable is inside Jinja2 expression statement {% %}
Last updated