CLI/SSH Development Best Practices

To ensure highest efficiency, there are a series of developer best practices recommended.

Data collection, snippet capability, connection, and code simplicity are just some of the areas that benefit from the following Development Best Practices.

Best Practices

  • Keep remote commands simple. In turn, the remote device’s processing power reduces. The Snippet Framework will massage the data as needed.

    • Configure the parser to format results. Then, use selectors to extract data needed for collection.

    • For example, Use the netstat -nt simple command and then the parser and selector to receive the foreign address.

    • Another example that uses three additional commands:

    netstat -nt | grep tcp | awk -e '{ print $5 }' | cut -d: -f1

  • Maintain a constant connection to the remote device.

    • Latency reduces due to protocol negotiations on the initial connection. This allows multiple calls to be made per session.

  • Test your Unix command on the monitored device.

    • Ensure that the command works.

    • Make certain that you understand the results and their format. This is needed to properly parse and select pertinent data.

  • Use tech-specific packs as opposed to broader packs.

    • For example, use a RedHat pack over a Linux Pack`

  • Use key-base authentication.

  • Avoid the use of interactive mode.

    • The CLI library execute commands as batches; therefore unable to reply.

  • Configure sudo for password-less authentication for specific commands.

Design Checklist for CLI Dynamic Applications

Before you start to write a list of commands that are piping the results into other commands, first think through your process.

  1. Model

    1. What does the data that you want to collect look like?

  2. Collection Objects

    1. What data do you want to collect?

  3. Command

    1. What is the command that you need to run?

    2. Ensure to use appropriate flags to get to only the data you need.

    3. Avoid stringing together multiple commands together.

    4. If you find an opportunity to adjust the command to reduce the amount of data being returned, adjust it, then adjust the parsing.

  4. Parsing

    1. Are there ways to transform the data from its raw form to a structured format like a dictionary?

    2. Look to using existing Snippet Framework parsers.

    3. Write pure functions. This will keep logic simple and allow for code reuse.

  5. Select

    1. Once the data is in the structure you want, then it is time to select the values that are relevant to your data model stated above.

    2. Remember to look for existing Snippet Framework selectors.

    3. If you find that you are struggling to select the data that you need for your collection, revisit your parsing step and possibly your command step to determine if there is a better way to gather the data needed.

Preferred Commands

There are efficiencies with command configurations in the Snippet Framework. A more efficient way to gather ipv4 addresses can be applied to the ifconfig command.

This below example is the preferred YAML format for the ifconfig command:

low_code:
  id: ifconfig_ipv4_names
  version: 2
  steps:
    - ssh:
        command: /usr/sbin/ifconfig
    - parse_ifconfig
    - jsonpath:
        value: $..ipv4_addr
        index: $..name

A command that uses several commands that run on a remote device is not preferred. An outer ifconfig command contains a loops that calls ifconfig a second time. A few values may return, but the text parsing to get those values is superfluous.

This below example is the discouraged YAML format for the ifconfig command:

for i in `ifconfig | awk -e '{ print $1 }' | grep : | cut -d: -f1`; do echo -n $i=; ifconfig $i | grep "inet " | awk -e '{ print $2 }'; done