Write an SIE script with Scrapli
Scrapli is a Python library for SSH and Telnet to network devices, like Netmiko, with a different design: by default it drives the server’s own ssh program, so it reads standard SSH config files and known_hosts files and supports anything OpenSSH does, including ProxyJump. It has core drivers for Cisco IOS-XE, IOS XR and NX-OS, Arista EOS and Juniper Junos, and community drivers for more.
Good for
- SSH CLIs on Scrapli’s supported platforms.
- Networks where you already depend on OpenSSH features, such as jump hosts in an SSH config file.
Set it up
sudo /opt/rconfig-sie/venv/bin/pip install scraplisudo -u apache /opt/rconfig-sie/venv/bin/python -c "import scrapli" # www-data on UbuntuThe command
The first argument is the Scrapli platform name, such as cisco_iosxe, cisco_nxos, cisco_iosxr, arista_eos or juniper_junos:
/opt/rconfig-sie/venv/bin/python {script_path}my-scrapli.py cisco_iosxe {device_ip} {device_username} {device_password} {device_enable_password}A minimal script
#!/usr/bin/env python3"""Minimal SIE script with Scrapli.
Usage: scrapli-skeleton.py <platform> <ip> <username> <password> [enable_password]"""
import sys
from scrapli import Scraplifrom scrapli.exceptions import ScrapliException
KEYS = "/var/www/html/rconfig8/current/storage/app/rconfig/scripts/keys"COMMANDS = { "cisco_iosxe": "show running-config", "cisco_nxos": "show running-config", "cisco_iosxr": "show running-config", "arista_eos": "show running-config", "juniper_junos": "show configuration | display set",}
def fail(message): print(f"ERROR: {message}", file=sys.stderr) sys.exit(1)
def main(): if len(sys.argv) < 5: fail("usage: scrapli-skeleton.py <platform> <ip> <username> <password> [enable_password]") platform, host, username, password = sys.argv[1:5] secondary = sys.argv[5] if len(sys.argv) > 5 else ""
command = COMMANDS.get(platform) if command is None: fail(f"no config command defined for platform '{platform}'")
device = { "platform": platform, "host": host, "auth_username": username, "auth_password": password, "auth_secondary": secondary, "auth_strict_key": True, "ssh_known_hosts_file": f"{KEYS}/known_hosts", "ssh_config_file": f"{KEYS}/ssh_config", "timeout_socket": 15, "timeout_ops": 90, }
try: with Scrapli(**device) as conn: response = conn.send_command(command) except ScrapliException as exc: fail(f"{type(exc).__name__}: {exc}")
if response.failed: fail(f"'{command}' failed on {host}") config = response.result if not config.strip(): fail(f"no output from {host}")
sys.stdout.write(config.rstrip("\n") + "\n") print(f"SIE-OK: scrapli {len(config.splitlines())} lines from {host}", file=sys.stderr) return 0
if __name__ == "__main__": sys.exit(main())ssh_config_file points at the same keys/ssh_config the jump host recipe uses. Create an empty file there if you don’t need one.
Habits that matter with Scrapli
- Check
response.failed. Scrapli marks a response failed when the device prints a known error for that platform, without raising an exception. timeout_opsfor long output. It’s how long one command may take.- Keep
auth_strict_keyon. Withssh_known_hosts_fileset, an unknown or changed host key fails the run.
Related
- Netmiko for a wider list of platforms
- Through a jump host