Edits history of script submission #23907 for ' Get MLAG (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 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):
        """
        Get MLAG
        MLAG state of the switch: state, negotiation, peer-link, config-sanity, reload-delay (seconds) and the status of every MLAG port (show mlag detail, show mlag config-sanity, show mlag interfaces).
        """
        detail, config_sanity, interfaces = node(auth).run_commands(
            ["show mlag detail", "show mlag config-sanity", "show mlag interfaces"]
        )
        return {
            **detail,
            "config_sanity": config_sanity,
            "interfaces": interfaces.get("interfaces"),
        }
    
    
    # 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