0

Confirm Commit

by
Published today

Confirm a session committed with a commit timer, so it is kept, and save the running config to startup-config.

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, session_name: str, save: bool = True):
21
    """
22
    Confirm Commit
23
    Confirm a session committed with a commit timer, so it is kept, and save the running config to startup-config.
24
    """
25
    node(auth).run_commands(
26
        [
27
            f"configure session {session_name} commit",
28
            *(["write memory"] if save else []),
29
        ],
30
    )
31
    return {"session": session_name, "confirmed": True, "saved": save}
32

33

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