Files

29 lines
1.1 KiB
Python

from typing import Tuple, Optional, List, Dict
from .client import ComboCoreClient
def get_routes(host: str, user: str, pwd: str, verify_ssl=False, timeout=10.0) -> List[Dict]:
cli = ComboCoreClient(host, user, pwd, verify_ssl, timeout)
return cli.get("/core/ncm/api/1/status/routes")
def derive_eth0_cidr_gw(routes: List[Dict]) -> Tuple[str, Optional[str]]:
gw = None
ip = None
masklen = None
for rt in routes:
if rt.get("family") == "inet" and rt.get("dst") == "default" and rt.get("dev") == "eth0":
gw = rt.get("gateway")
for rt in routes:
if rt.get("family") == "inet" and rt.get("dev") == "eth0":
if not ip and rt.get("prefsrc"):
ip = rt["prefsrc"]
dst = rt.get("dst", "")
if "/" in dst:
try:
masklen = int(dst.split("/", 1)[1])
except Exception:
pass
if ip and masklen is not None:
break
if not ip or masklen is None:
raise RuntimeError("Could not derive eth0 IP/prefix from routes.")
return f"{ip}/{masklen}", gw