0

Get Routes

by
Published today

IPv4 routing table of a VRF, optionally for one prefix (show ip route).

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, vrf: str = "default", prefix: str = ""):
21
    """
22
    Get Routes
23
    IPv4 routing table of a VRF, optionally for one prefix (show ip route).
24
    """
25
    [result] = node(auth).run_commands(
26
        [f"show ip route vrf {vrf or 'default'}" + (f" {prefix}" if prefix else "")],
27
    )
28
    return result["vrfs"]
29

30

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