summaryrefslogtreecommitdiff
path: root/python/vyos/utils/network.py
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2023-08-23 20:14:14 +0200
committerChristian Breunig <christian@breunig.cc>2023-08-23 20:18:23 +0200
commitec23c669710a1f98fd1bd2095ffb861007374bda (patch)
tree92bdcf009a1720ea3934b38343e46d832ff975ae /python/vyos/utils/network.py
parentb88b11ee73601155a485956be80971a697a7f4d6 (diff)
downloadvyos-1x-ec23c669710a1f98fd1bd2095ffb861007374bda.tar.gz
vyos-1x-ec23c669710a1f98fd1bd2095ffb861007374bda.zip
vrf: T5428: move helpers to common vyos.utils.network module
Helper functions can and will be re-use din different code places.
Diffstat (limited to 'python/vyos/utils/network.py')
-rw-r--r--python/vyos/utils/network.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/python/vyos/utils/network.py b/python/vyos/utils/network.py
index 3f9a3ef4b..2f181d8d9 100644
--- a/python/vyos/utils/network.py
+++ b/python/vyos/utils/network.py
@@ -36,6 +36,10 @@ def get_protocol_by_name(protocol_name):
except socket.error:
return protocol_name
+def interface_exists(interface) -> bool:
+ import os
+ return os.path.exists(f'/sys/class/net/{interface}')
+
def interface_exists_in_netns(interface_name, netns):
from vyos.utils.process import rc_cmd
rc, out = rc_cmd(f'ip netns exec {netns} ip link show dev {interface_name}')
@@ -43,6 +47,24 @@ def interface_exists_in_netns(interface_name, netns):
return True
return False
+def get_vrf_members(vrf: str) -> list:
+ """
+ Get list of interface VRF members
+ :param vrf: str
+ :return: list
+ """
+ import json
+ from vyos.utils.process import cmd
+ if not interface_exists(vrf):
+ raise ValueError(f'VRF "{vrf}" does not exist!')
+ output = cmd(f'ip --json --brief link show master {vrf}')
+ answer = json.loads(output)
+ interfaces = []
+ for data in answer:
+ if 'ifname' in data:
+ interfaces.append(data.get('ifname'))
+ return interfaces
+
def get_interface_vrf(interface):
""" Returns VRF of given interface """
from vyos.utils.dict import dict_search