Network automation with scrapli¶
The Python article listed
scrapli in the toolbox as "a fast, modern
screen-scraping SSH library, an alternative to Netmiko."
This article is that alternative in practice — what scrapli does differently, and why you'd reach for
it when Netmiko is the obvious first choice. The short version: it is built for speed, it hands you a
structured result instead of a bare string, and it does async natively.
A connection and a command¶
scrapli ships "core" drivers for the common Cisco platforms — importable from scrapli.driver.core as
IOSXEDriver, NXOSDriver, IOSXRDriver, EOSDriver, and JunosDriver (plus GenericDriver for
anything unmodelled, and the separate scrapli-community
project for non-core platforms). Pick the driver for your platform, pass a connection dict, and use it
as a context manager so the session always closes:
import os
from scrapli.driver.core import IOSXEDriver
device = {
"host": "192.0.2.11",
"auth_username": os.environ["NET_USER"], # from the environment, never hard-coded
"auth_password": os.environ["NET_PASS"],
"auth_strict_key": False, # see the note below
"transport": "system", # the default
}
with IOSXEDriver(**device) as conn:
resp = conn.send_command("show version")
print(resp.result)
Two things are worth stopping on. The default transport is system, which wraps the host's own
OpenSSH binary rather than a pure-Python SSH implementation — that's a large part of where scrapli's
speed comes from, and it means your ~/.ssh/config and keys apply as usual. And auth_strict_key
defaults to True: scrapli verifies the device's host key against your known-hosts file out of the
box, which is the secure default but will refuse an unknown device. Setting it to False (above) skips
that check for a lab box — in production, pre-populate known-hosts instead and leave it on.
The Response object, not a string¶
This is the ergonomic difference from Netmiko. send_command doesn't return the output text; it returns
a Response object that carries the output plus metadata:
resp = conn.send_command("show ip interface brief")
resp.result # the raw output as a string
resp.failed # False if the command ran cleanly, True if the device complained
resp.elapsed_time # how long it took, in seconds
resp.raise_for_status() # raises ScrapliCommandFailure if resp.failed is True
Because the result object already knows whether the command failed, your error handling gets a clean
seam. scrapli also raises its own exceptions (ScrapliException and subclasses such as
ScrapliAuthenticationFailed and ScrapliTimeout) for connection-level problems, the same way
Netmiko raises on a timeout or bad login.
The Response is also where structured parsing lives, so
you don't bolt it on separately:
# TextFSM / ntc-templates — scrapli finds the template from the platform + command for you
structured = resp.textfsm_parse_output()
# or Genie, if you installed the extra (pip install "scrapli[genie]")
structured = resp.genie_parse_output()
Pushing configuration¶
Config methods mirror the read methods and handle privilege escalation into config mode for you —
send_config for one line, send_configs for a list, and send_configs_from_file for a reviewable,
version-controlled file:
vlan_cfg = [
"vlan 10",
"name SALES",
"interface GigabitEthernet3",
"switchport mode access",
"switchport access vlan 10",
]
with IOSXEDriver(**device) as conn:
resp = conn.send_configs(vlan_cfg)
conn.send_command("write memory") # persist to startup
if resp.failed:
print("device rejected a line — config may be partial")
Here scrapli and Netmiko are in exactly the same boat, and it's worth being honest about it: this is still CLI screen-scraping. There is no candidate datastore, so commands apply line by line and a failure midway leaves a partial change live on the device, and there is no idempotency — re-running appends again. The discipline from the Netmiko article carries over unchanged: render the full stanza from a source of truth with Jinja2, diff before you change, prove it on a canary, and reach for NETCONF's candidate/commit or NAPALM when you genuinely need an all-or-nothing commit. If you want that transactional feel while staying in the scrapli family, the sibling scrapli-cfg package adds a NAPALM-style merge/replace/diff/commit workflow on top of the CLI, and scrapli-netconf speaks NETCONF directly.
Going async for real fan-out¶
scrapli's headline feature is first-class async. The concurrency article
reached for a ThreadPoolExecutor precisely because Netmiko and requests are synchronous; scrapli
gives you an async driver instead, so a single event loop can hold hundreds of sessions open at once
without a thread per device. Use the Async* driver and switch the transport to asyncssh (async
requires asyncssh or asynctelnet):
import asyncio
from scrapli.driver.core import AsyncIOSXEDriver
async def grab(host):
dev = {"host": host, "auth_username": USER, "auth_password": PW,
"auth_strict_key": False, "transport": "asyncssh"}
async with AsyncIOSXEDriver(**dev) as conn:
resp = await conn.send_command("show version")
return host, resp.result
async def main(inventory):
results = await asyncio.gather(*(grab(h) for h in inventory))
for host, output in results:
save(host, output)
asyncio.run(main(inventory))
The blast-radius warning applies with equal force: async makes
it trivial to hit the whole fleet at once, so reads can fan out freely but writes stay bounded — bound
the concurrency with a asyncio.Semaphore, canary first, and roll out in batches validated with
pyATS.
scrapli or Netmiko?¶
Both screen-scrape the CLI, so they solve the same problem; the trade-off is maturity versus modern ergonomics.
| scrapli | Netmiko | |
|---|---|---|
| Result | Response object (result, failed, timing, built-in parse) |
plain string |
| Async | first-class (Async* drivers) |
none (thread it yourself) |
| Default transport | system (host OpenSSH, fast) |
Paramiko (pure Python) |
| Host-key check | strict by default | off by default |
| Platform breadth | core Cisco + community plugins | the broadest, largest community |
Reach for scrapli when you want speed, structured results without extra wiring, or genuine async fan-out. Stay on Netmiko when you need its enormous device coverage or you're matching an existing codebase. Neither gives you a transaction — that is still the job of NETCONF or NAPALM.
Key takeaways¶
- scrapli is a fast, sync/async CLI client (Python 3.10+): pick a core driver like
IOSXEDriver, use it as a context manager, andsend_command/send_configsas you would with Netmiko. - You get a
Response, not a string —.result,.failed,.elapsed_time,raise_for_status(), and built-intextfsm_parse_output()/genie_parse_output()for structured data. - The default
systemtransport wraps OpenSSH (speed, and your SSH config applies), andauth_strict_keyisTrueby default — a secure default you turn off only for a lab. - It's still CLI screen-scraping: no candidate datastore and no idempotency, so render from a
source of truth, diff-before-change, and canary-then-batch — or use
NETCONF/NAPALM (or
scrapli-cfg) when you need a real commit. - Async is the reason to switch for large fleets —
Async*drivers with theasyncsshtransport replace the thread pool, but the blast-radius discipline is unchanged.
Sources: scrapli on GitHub, scrapli on PyPI, scrapli-cfg, scrapli-netconf, scrapli-community.