In this lesson, we will cover:

  • The main Scrapli components.
  • How to connect to a device.
  • How to interact with a device.
  • How to close a device connection.
  • The Scrapli context manager.

You can find the scripts and code for this lesson within the Scrapli repo under the directory: 002_scrapli_core/001_basics/.

Scrapli Core Components

Scrapli Core is built upon 3 key components – driver, channel and transport.

  • Driver – provides the API/interface to the user. In other words, it is the driver that the user interacts with via a set of common (vendor agnostic) methods.
  • Channel – resides between the driver and transport components. Performs operations such as sending commands and prompting handling.
  • Transport – provides a common interface for which the channel can read/write based on the implemented transport type (ssh2, paramiko, etc). The transport type is configured using transport plugins.
Scrapli Components

Connecting to a Device

Let’s now dive into some code and connect to one of the devices within the lab. First we define the connection variables in a dict(), which we then pass to instantiate our driver class.

The platform specifies the driver that will be used. The available platforms are:

  • arista_eos
  • cisco_iosxe
  • cisco_iosxr
  • cisco_nxos
  • juniper_junos

Once done, we open a connection to the device via open(). Here is an example:

Note: Once you enter the following code, no output will be returned. This is expected. How we work with our device connection will make more sense as we work through the following sections.

import os

from dotenv import load_dotenv
from rich import print
from scrapli import Scrapli

load_dotenv()

# Create device dict()
device = {
    "host": "172.29.151.1",
    "auth_username": os.getenv("LAB_USERNAME"),
    "auth_password": os.getenv("LAB_PASSWORD"),
    "auth_strict_key": False,
    "platform": "cisco_nxos",
}

# Instantiate Scrapli driver with device dict()
conn = Scrapli(**device)

# Open connection to device
conn.open()
Ready to Master Network Automation? Start Your Journey Today!
Our membership provides:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Live monthly tech sessions
  • Access to tech session library

Join Now ➜
Close You've successfully subscribed to Packet Coders.
Close Success! Your account is fully activated, you now have access to all content.
Close Welcome back! You've successfully signed in.
Close Nearly there! To activate your account, please click the link in the email we just sent you.