0

Get Running Config

by
Published today

The running configuration as text, or only one section of it (e.g. "router bgp", "interface Ethernet1", "mlag").

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 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(auth: arista_eos, section: str = ""):
21
    """
22
    Get Running Config
23
    The running configuration as text, or only one section of it (e.g. "router bgp", "interface Ethernet1", "mlag").
24
    """
25
    cmd = f"show running-config section {section}" if section else "show running-config"
26
    [result] = node(auth).run_commands([cmd], "text")
27
    return result["output"]
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