scrapli

2026.2.20 · active · verified Tue Apr 14

Scrapli (scrape cli) is a fast, flexible, Python 3.9+ library designed for connecting to and interacting with network devices (routers, switches, firewalls) via SSH or Telnet. It supports both synchronous and asynchronous operations, offering a consistent API across different connection types. The library is actively developed, with frequent releases including release candidates for a major v2.0.0 update, indicating ongoing enhancements and maintenance.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a synchronous connection to a Cisco IOS-XE device, sending a 'show version' command and a configuration. It uses the `IOSXEDriver` with a context manager for automatic connection management. Device credentials are retrieved from environment variables for security. Replace `IOSXEDriver` with `AsyncIOSXEDriver` and `conn.send_command` with `await conn.send_command` within an `async def` function if using asynchronous operations.

import os
from scrapli.driver.core import IOSXEDriver
from scrapli.exceptions import ScrapliException

def get_device_info():
    device = {
        "host": os.environ.get("SCRAPLI_HOST", "your_device_ip"),
        "auth_username": os.environ.get("SCRAPLI_USERNAME", "admin"),
        "auth_password": os.environ.get("SCRAPLI_PASSWORD", "password"),
        "auth_strict_key": False, # Often needed for labs or unknown hosts
        "transport": "system", # 'system', 'paramiko', 'ssh2', 'telnet'
        "port": 22 # Default for SSH, 23 for Telnet
    }
    return device

def main_sync():
    device_info = get_device_info()
    try:
        # Using a context manager for automatic open/close
        with IOSXEDriver(**device_info) as conn:
            print(f"Connected to {conn.host} successfully.")
            response = conn.send_command("show version")
            if response.failed:
                print(f"Command failed: {response.result}")
                return
            print("--- Show Version Output ---")
            print(response.result)

            response_config = conn.send_configs(["interface Loopback0", "description Test Loopback"]) 
            if response_config.failed:
                print(f"Config failed: {response_config.result}")
                return
            print("--- Configuration Output ---")
            print(response_config.result)

    except ScrapliException as e:
        print(f"Scrapli error: {e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    # Example usage: Set environment variables or update `get_device_info` with actual values
    # os.environ['SCRAPLI_HOST'] = '172.18.0.11'
    # os.environ['SCRAPLI_USERNAME'] = 'vrnetlab'
    # os.environ['SCRAPLI_PASSWORD'] = 'VR-netlab9'
    main_sync()

view raw JSON →