Initial commit of AthonetTools
This commit is contained in:
29
services/combocore/ncm.py
Normal file
29
services/combocore/ncm.py
Normal file
@@ -0,0 +1,29 @@
|
||||
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
|
||||
Reference in New Issue
Block a user