Custom Device Config Profiles
NetCrunch allows creating custom profiles using simple YAML definitions, allowing executing commands using ssh or telnet, and processing output
NetCrunch config management engine has been inspired by the Oxidized open source project and uses similar concepts to describe session parameters and processing of command output.
Although there are important differences.
- We decided to describe device profiles in a declarative way, so no programming is needed
- Regular expressions are processed in JavaScript
The only knowledge needed to describe the device configuration profile is a strong understanding of regular expressions.
Setting prompt
The prompt format is needed to recognize when the terminal is ready to read the next command and when the previous command's output ends. You can set one or multiple prompt expressions.
Single prompt
prompt: "/^(\r*[\w\s.@()/:-]+[#>]\s?)$/m"
Multiple prompts
prompt: - /^(?:\x1b[..h)?[\w.-]+# $/m - /^\w+@\w+([-.]\w+)*>((?.+)?\s)?/m
Setting comment
This is a comment prefix that will be added to some command output, such as show version
comment: "# "
In the above case, we have defined a two-character prompt prefix # and a single space.
Output processing block
After each command, we can describe how to process the output.
lines: range: 1..-1 reject: - /^\r$/ - /time/ comment:
In the above example, the engine will:
- remove the first and last line (range: 1..-1)
- skip empty lines (reject) and the ones containing the time phrase
- comment out all output
Processing for all commands
It starts with all: and then you can describe the processing block for every command. It's common to remove the first and the last line.
all: lines: # always cut off first and last line range: 1..-1
Processing secrets
We can download the config as is or remove secrets such as passwords, security key, and others, by replacing them with some text.
The processing depends on the option state when configuring the Device Config sensor for a particular node.
secret: /^(create snmp community) \S+/gm: "$1 <removed>" /^(create snmp group) \S+/gm: "$1 <removed>"
Defining commands to be executed
It's simple. In commands: block add each command, and after it, describe processing if needed.
Command output commented
commands: show inventory: comment:
Command with processing block
commands: show version: lines: reject: - /([Ss]ystem [Uu]p\s?[Tt]ime|[Uu]p\s?[Tt]ime is \d)/ comment:
Regular command with full output
commands: "show running-config | nomore":
Session and protocol configuration
The configuration block begins with config:. Then the block is described by the protocol list it references.
Telnet
Telnet is sometimes the only way to reach the device. It requires setting regular expressions to detect prompts for username and password.
telnet: #set prompts for interactive telnet login login: username: /Username:/ password: /Password:/
SSH
SSH specific
ssh: #terminal options pty: charsWide: 1000
Exec mode
Commands can be executed in an interactive session or in exec mode. To enable exec mode, add
ssh: exec: true
Command to be executed at the beginning and the end of the session
ssh, telnet: # commands to be executed after login afterLogin: - "no page" # Commands to execute before logout beforeLogout: - logout y n
Handling interactive prompts
Many devices allow disabling paging by issuing a command after login or by adding a pipe to the command, but some lack this feature. For such an occasion, you can define regular expressions for watching the terminal output and the reaction that is usually sent for one character.
For example
expect: /Press any key to continue(\x1b[\??\d+(;\d+)[A-Za-z])$/m: send: " " replace: ""
If the message appears, the program will send a single space and then remove the prompt from the output.