0

Run Commands

by
Published today

Run EOS CLI commands over eAPI (runCmds) and return one result per command. Commands run in privileged mode; config commands need a leading "configure". JSON needs commands with a JSON model (most show commands); use text for the rest.

Script arista_eos Verified

The script

Submitted by hugo989 Python3
Verified 3 hours ago
1
# py312
2
# requirements:
3
# pyeapi==1.0.4
4

5
import ssl
6
from typing import Literal, TypedDict
7
from urllib.parse import urlsplit
8

9
import pyeapi
10

11

12
class arista_eos(TypedDict):
13
    host: str
14
    username: str
15
    password: str
16
    port: int
17
    verify_tls: bool
18

19

20
def main(
21
    auth: arista_eos, commands: list[str], format: Literal["json", "text"] = "json"
22
):
23
    """
24
    Run Commands
25
    Run EOS CLI commands over eAPI (runCmds) and return one result per command. Commands run in privileged mode; config commands need a leading "configure". JSON needs commands with a JSON model (most show commands); use text for the rest.
26
    """
27
    return node(auth).run_commands(commands, format or "json")
28

29

30
# host is an address or a URL (https://10.0.0.11:8443). pyeapi skips certificate checks unless it is given a context,
31
# so one is always passed: verifying, or not when the resource unticks verify_tls (EOS ships a self-signed cert).
32
def node(auth: arista_eos, timeout: float = 60):
33
    url = urlsplit(auth["host"] if "://" in auth["host"] else f"https://{auth['host']}")
34
    context = ssl.create_default_context()
35
    if auth.get("verify_tls") is False:
36
        context.check_hostname = False
37
        context.verify_mode = ssl.CERT_NONE
38
    return pyeapi.connect(
39
        transport=url.scheme,
40
        host=url.hostname,
41
        port=url.port or auth.get("port") or None,
42
        username=auth["username"],
43
        password=auth["password"],
44
        context=context,
45
        timeout=timeout,
46
        return_node=True,
47
    )
48