ScrapliGo

repository·main·Indexed 43 days ago

https://github.com/scrapli/scrapligo

A Go implementation of the Scrapli library for interacting with network devices via SSH, Telnet, or HTTP. It provides capabilities for managing device connections using the Cli object, handling interactive prompts, implementing Proxy Jump behavior, and performing NETCONF operations including edit-config, get operations, and subscriptions via raw_rpc. It supports custom platform definitions via YAML and unstructured text parsing using TextFSM.

Tokens
9.5K
Snippets
29
Records
50
Agent score
54%

What's inside ScrapliGo

  1. Use read_with_callbacks for long-running outputs and event-driven interactions

    main

    The read_with_callbacks method allows you to handle device interactions, tail logs, or manage long-running outputs by triggering specific function calls based on the content received. This is particularly useful for:

    • Connecting to device consoles during boot-up.
    • Zero-touch-provisioning (ZTP) workflows.
    • Tailing logs or other continuous streams where specific patterns should trigger actions.

    Instead of a standard read that waits for a specific prompt or EOF, read_with_callbacks enables an event-driven approach to reading device output.

    // Note: The actual implementation is found in the example code file, 
    // but the concept involves passing callback functions to the read_with_callbacks method.
  2. Parse unstructured text output using TextFSM

    main

    You can use textfsm (and optionally ntc-templates) to convert unstructured text output from a network device into a structured object.

    Important Note for Go Users: Unlike the Python version (scrappy), scrapligo does not perform automatic template lookups with ntc-templates. You must explicitly provide the template path or URL when performing the parse operation.

  3. Use device input modes to handle privilege levels

    main
    Many network devices require interacting with different privilege levels or 'modes' (e.g., configuration mode vs. exec mode). Scrapli allows you to define these modes within your device definition. You can then use these definitions to send inputs at specific modes, ensuring the correct context for your commands.
  4. Override example connection settings via environment variables

    main

    When running the provided examples, you can override the default connection parameters using environment variables. This is useful if you are targeting a different device or a different network configuration.

    You can override the following parameters:

    • Platform (e.g., srlinux)
    • Host
    • Port
    • Username
    • Password

    Note: Changing the target platform type may cause examples to fail if the underlying logic is specific to the default platform.

  5. Implement Netconf subscriptions using raw_rpc

    main

    Because Netconf subscription implementations vary significantly depending on the RFC supported by the server, scrapligo does not provide a high-level abstraction for subscriptions. Instead, you should implement subscriptions manually using the raw_rpc method.

    Depending on the device/server type, you will use different methods to retrieve notifications:

    1. For RFC-compliant servers (e.g., Netopeer) using create-subscription: Use the get_next_notification method to fetch notification messages.
    2. For devices (e.g., Cisco IOS-XE) using establish-subscription: Use the get_next_subscription method, as these subscriptions are associated with a specific subscription ID.
  6. Send inputs to a device using the Cli object

    main

    The Cli object provides three primary methods for sending data (inputs) to a device. Use these methods depending on whether you are sending a single input, multiple inputs, or inputs loaded from a file:

    • SendInput: Sends a single input string to the device.
    • SendInputs: Sends a slice of input strings to the device.
    • SendInputsFromFile: Reads inputs from a specified file and sends them to the device.
  7. Set up the Example Lab environment with Containerlab

    main

    The examples in this repository are designed to run against an srlinux device within a scrapli_clab containerlab topology (specifically the ci variant). This environment can be run on Linux or Darwin (macOS) using Docker.

    To spin up the test topology from the repository root, use the following command:

    make run-clab-ci

    Requirements:

    • Docker installed and running.
    • The scrapli_clab topology (which includes an srlinux device, a Netopeer NETCONF server, and a dummy Linux container for testing proxy-jump SSH behavior).

    Topology Details:

    • The ci variant uses a launcher pod with the Docker socket bound to it, allowing Containerlab to run on Darwin hosts.
    • Device Credentials: admin / NokiaSrl1!
    • Port Mapping (Darwin/macOS): Because of NAT, Darwin users should use ports 21022 (SSH), 21023 (Telnet), and 21830 (Netconf). Standard ports (22, 23, 830) are used on Linux.
  8. Handle simple interactive prompts in ScrapliGo

    main

    For simple semi-interactive device prompts—such as confirming a configuration write or a file deletion—use the basic interaction handling capabilities provided by ScrapliGo. This approach is intended for straightforward prompts where the user or automation needs to respond to a specific device request.

    For more complex or elaborate interactive scenarios (e.g., multi-step dialogues or conditional logic based on prompt content), use the read_with_callbacks method instead.

  9. Use custom platform definitions via YAML

    main

    Scrapli uses "platform definitions" to determine how to interact with specific device types, including the expected prompt shape and available device modes. While many definitions are bundled with Scrapli, you can provide your own custom definition by pointing Scrapli to a YAML file. This is useful for unsupported platforms, new releases not yet packaged, or custom device configurations.

    To implement a custom definition, you must follow the platform definition schema. The schema is defined by the core libscrapli types or the Pydantic schema used in the scrapli_definitions test suite.

  10. Configure logging in Scrapli (Go)

    main

    Scrapli (Go) provides flexibility in how logs are handled. You can use any of the following three methods to configure logging:

    1. Standard log.Logger: Use the standard library's logging implementation.
    2. Structured log/slog.Logger: Use the modern structured logging library introduced in Go.
    3. Custom Callback: Provide your own callback function to handle log messages according to your specific requirements.

    The choice of logger depends on your application's needs for simplicity, structured data, or custom routing.

  11. Configure Input Handling modes

    main

    When performing CLI operations, you can specify how the driver handles input using the InputHandling type. This determines how the driver reads characters from the connection before proceeding.

    Available modes:

    • InputHandlingExact: The driver reads the exact inputs from the connection before continuing or sending the return character.
    • InputHandlingFuzzy: The driver reads all input characters in the correct order, allowing for interruptions like backspaces or newline indicators.
    • InputHandlingIgnore: The driver simply continues to send a return character without attempting to read or consume input. Note: This is generally not recommended.

    Use WithInputHandling(i InputHandling) to apply these modes to SendInput, SendInputs, or SendPromptedInput operations.

    // Example of setting input handling
    // err := client.SendInput(ctx, "command", cli.WithInputHandling(cli.InputHandlingFuzzy))