In this lesson, we will cover:

  • The available send command methods.
  • The different Response types.
  • Response attributes.
  • The different ways to perform error handling.

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

Available Methods

Scrapli provides 3 methods for sending commands. They are:

  • send_command("command_1") – send a single command to the device.
  • send_commands(["command_1", "command_2"]) – send multiple commands to the device.
  • send_commands_from_file('commands.txt') – read commands from a file and send to the device.

When it comes to running these commands, we instantiate our driver as previously shown, then use the necessary send command method. Here are examples of the different methods.

Send Command

with Scrapli(**device) as conn:
    # Send command to device
    response = conn.send_command("show version")

Send Commands

with Scrapli(**device) as conn:
    # Send commands to device
    commands = ["show version", "show ip interface brief"]
    multiresponse = conn.send_commands(commands)

Send Commands from File

with Scrapli(**device) as conn:
    # Get absolute path to command file
    absolute_path = os.path.dirname(os.path.realpath(__file__))
    commands_file = f"{absolute_path}/send_commands.txt"

    # Send commands to device
    multiresponse = conn.send_commands_from_file(commands_file)

Running these commands is pretty self-explanatory. We will now focus our attention on the response object that we get back.

Response Objects

When we run our commands against our device using any of the 3 methods previously mentioned, a Response object is returned. In this section, we will look at the different object types that can be returned, as well as the attributes that the response object provides.

Response Attributes

First, let’s use send_command() to obtain a response:

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

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()

# Send command to device
response = conn.send_command("show version")

If we print our response we will see that it was successful.

print(response)
===
Response <Success: True>

However, let’s explore some more and inspect the object using the inspect feature of Rich, to see the available attributes.

inspect(response)
╭───────────────────────────────────────────────────────────────── <class 'scrapli.response.Response'> ──────────────────────────────────────────────────────────────────╮
│ ╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│ │ Response <Success: True>                                                                                                                                           │ │
│ ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                                                                                                        │
│        channel_input = 'show version'                                                                                                                                  │
│         elapsed_time = 0.940077                                                                                                                                        │
│               failed = False                                                                                                                                           │
│ failed_when_contains = ['% Ambiguous command', '% Incomplete command', '% Invalid input detected', '% Invalid command at']                                             │
│          finish_time = datetime.datetime(2021, 6, 14, 15, 49, 26, 546455)                                                                                              │
│       genie_platform = 'nxos'                                                                                                                                          │
│                 host = 'nebula.packetflow.co.uk'                                                                                                                                  │
│           raw_result = b'\nCisco Nexus Operating System (NX-OS) Software\nTAC support: http://www.cisco.com/tac\nDocuments:                                            │
│                        http://www.cisco.com/en/US/products/ps9372/tsd_products_support_series_home.html\nCopyright (c) 2002-2019, Cisco Systems, Inc. All rights       │
│                        reserved.\nThe copyrights to certain works contained herein are owned by\nother third parties and are used and distributed under license.\nSome │
│                        parts of this software are covered under the GNU Public\nLicense. A copy of the license is available                                            │
│                        at\nhttp://www.gnu.org/licenses/gpl.html.\n\nNexus 9000v is a demo version of the Nexus Operating System\n\nSoftware\n  BIOS: version \n NXOS:  │
│                        version 9.2(3)\n  BIOS compile time:  \n  NXOS image file is: bootflash:///nxos.9.2.3.bin\n  NXOS compile time:  2/17/2019 5:00:00 [02/17/2019  │
│                        15:07:27]\n\n\nHardware\n  cisco Nexus9000 9000v Chassis \n   with 8159564 kB of memory.\n  Processor Board ID 9LJH5PONTOQ\n\n  Device name:    │
│                        leaf1-nxosv\n  bootflash:    3509454 kB\nKernel uptime is 89 day(s), 21 hour(s), 39 minute(s), 47 second(s)\n\nLast reset \n  Reason: Unknown\n │
│                        System version: \n  Service: \n\nplugin\n  Core Plugin, Ethernet Plugin\n\nActive Package(s):\n        \nleaf1-nxosv# '                         │
│               result = 'Cisco Nexus Operating System (NX-OS) Software\nTAC support: http://www.cisco.com/tac\nDocuments:                                               │
│                        http://www.cisco.com/en/US/products/ps9372/tsd_products_support_series_home.html\nCopyright (c) 2002-2019, Cisco Systems, Inc. All rights       │
│                        reserved.\nThe copyrights to certain works contained herein are owned by\nother third parties and are used and distributed under license.\nSome │
│                        parts of this software are covered under the GNU Public\nLicense. A copy of the license is available                                            │
│                        at\nhttp://www.gnu.org/licenses/gpl.html.\n\nNexus 9000v is a demo version of the Nexus Operating System\n\nSoftware\n  BIOS: version\n NXOS:   │
│                        version 9.2(3)\n  BIOS compile time:\n  NXOS image file is: bootflash:///nxos.9.2.3.bin\n  NXOS compile time:  2/17/2019 5:00:00 [02/17/2019    │
│                        15:07:27]\n\n\nHardware\n  cisco Nexus9000 9000v Chassis\n   with 8159564 kB of memory.\n  Processor Board ID 9LJH5PONTOQ\n\n  Device name:     │
│                        leaf1-nxosv\n  bootflash:    3509454 kB\nKernel uptime is 89 day(s), 21 hour(s), 39 minute(s), 47 second(s)\n\nLast reset\n  Reason: Unknown\n  │
│                        System version:\n  Service:\n\nplugin\n  Core Plugin, Ethernet Plugin\n\nActive Package(s):'                                                    │
│           start_time = datetime.datetime(2021, 6, 14, 15, 49, 25, 606378)                                                                                              │
│     textfsm_platform = 'cisco_nxos'                                                                                                                                    │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

The attributes to make note of, and that you will use most often, are:

  • result – the result of the sent command.
  • failed – a Boolean result for whether the command has failed.
  • elapsed_time – the amount of time taken to execute the given command.
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.