Edits history of script submission #23914 for ' Run Commands (arista_eos)'

  • python3
    One script reply has been approved by the moderators
    Ap­pro­ved
    # py312
    # requirements:
    # pyeapi==1.0.4
    
    import ssl
    from typing import Literal, TypedDict
    from urllib.parse import urlsplit
    
    import pyeapi
    
    
    class arista_eos(TypedDict):
        host: str
        username: str
        password: str
        port: int
        verify_tls: bool
    
    
    def main(
        auth: arista_eos, commands: list[str], format: Literal["json", "text"] = "json"
    ):
        """
        Run Commands
        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.
        """
        return node(auth).run_commands(commands, format or "json")
    
    
    # host is an address or a URL (https://10.0.0.11:8443). pyeapi skips certificate checks unless it is given a context,
    # so one is always passed: verifying, or not when the resource unticks verify_tls (EOS ships a self-signed cert).
    def node(auth: arista_eos, timeout: float = 60):
        url = urlsplit(auth["host"] if "://" in auth["host"] else f"https://{auth['host']}")
        context = ssl.create_default_context()
        if auth.get("verify_tls") is False:
            context.check_hostname = False
            context.verify_mode = ssl.CERT_NONE
        return pyeapi.connect(
            transport=url.scheme,
            host=url.hostname,
            port=url.port or auth.get("port") or None,
            username=auth["username"],
            password=auth["password"],
            context=context,
            timeout=timeout,
            return_node=True,
        )
    

    Submitted by hugo989 3 hours ago