scrapli
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
- breaking Scrapli v2.0.0, currently in Release Candidate, introduces significant architectural changes ('scrapli2'). While the core API aims for consistency between sync/async, users should review the changelog for specific breaking changes in import paths, class structures, or method signatures upon the final v2.0.0 release.
- deprecated Support for Python 3.6 was dropped in scrapli core (v2022.01.30) and related libraries like `scrapli_netconf` and `scrapli_community`.
- deprecated In `scrapli_netconf`, `NetconfScrape` and `AsyncNetconfScrape` classes were renamed to `NetconfDriver` and `AsyncNetconfDriver`, respectively, and subsequently removed.
- gotcha By default, scrapli uses 'system' transport, leveraging the local SSH binary. While this has zero external Python dependencies, it may require `auth_strict_key=False` in device arguments to bypass strict host key checking, especially in lab environments or when `known_hosts` is not managed.
- gotcha Scrapli's core library supports a limited set of 'NAPALM-like' platforms (Cisco IOS-XE, IOS-XR, NX-OS, Arista EOS, Juniper JunOS). For other network operating systems, you will need `scrapli_community` or to use `GenericDriver`/`NetworkDriver` with custom privilege levels and on-open/on-close functions.
- gotcha Using Telnet as a transport requires explicit configuration. The default port is 22 (SSH), even if the Telnet driver is implicitly selected by an older method.
Install
-
pip install scrapli -
pip install scrapli[paramiko,ssh2,asyncssh,textfsm,genie,netconf,community,full]
Imports
- Scrapli
from scrapli import Scrapli
- IOSXEDriver
from scrapli.driver.core import IOSXEDriver
- AsyncIOSXEDriver
from scrapli.driver.core import AsyncIOSXEDriver
- NetconfDriver
from scrapli_netconf import NetconfScrape
from scrapli_netconf.driver import NetconfDriver
Quickstart
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()