summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2021-07-02 16:50:33 +0200
committerGitHub <noreply@github.com>2021-07-02 16:50:33 +0200
commit21527ef4551613fe9b7eed9e4b2ce33ad46fe540 (patch)
tree59219bc423e0b53e5c39f697c5696d18d676e60b /python
parentcef8147afd1d031c91cc90b7642aad8ae372c81b (diff)
parent4e1a5c7cf4213fd5617e387a2d4a6d1e9a475c54 (diff)
downloadvyos-1x-21527ef4551613fe9b7eed9e4b2ce33ad46fe540.tar.gz
vyos-1x-21527ef4551613fe9b7eed9e4b2ce33ad46fe540.zip
Merge pull request #904 from sever-sever/T3535
conntrack-sync: T3535: Rewrite conf and op modes to XML python style
Diffstat (limited to 'python')
-rw-r--r--python/vyos/ifconfig/interface.py18
-rw-r--r--python/vyos/template.py14
2 files changed, 32 insertions, 0 deletions
diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py
index 40bff7c3d..fb658bd61 100644
--- a/python/vyos/ifconfig/interface.py
+++ b/python/vyos/ifconfig/interface.py
@@ -732,6 +732,24 @@ class Interface(Control):
"""
self.set_interface('proxy_arp_pvlan', enable)
+ def get_addr_v4(self):
+ """
+ Retrieve assigned IPv4 addresses from given interface.
+ This is done using the netifaces and ipaddress python modules.
+ Example:
+ >>> from vyos.ifconfig import Interface
+ >>> Interface('eth0').get_addr_v4()
+ ['172.16.33.30/24']
+ """
+ ipv4 = []
+ if AF_INET in ifaddresses(self.config['ifname']):
+ for v4_addr in ifaddresses(self.config['ifname'])[AF_INET]:
+ # we need to manually assemble a list of IPv4 address/prefix
+ prefix = '/' + \
+ str(IPv4Network('0.0.0.0/' + v4_addr['netmask']).prefixlen)
+ ipv4.append(v4_addr['addr'] + prefix)
+ return ipv4
+
def get_addr(self):
"""
Retrieve assigned IPv4 and IPv6 addresses from given interface.
diff --git a/python/vyos/template.py b/python/vyos/template.py
index 527384d0b..560483581 100644
--- a/python/vyos/template.py
+++ b/python/vyos/template.py
@@ -121,6 +121,14 @@ def render(
##################################
# Custom template filters follow #
##################################
+@register_filter('ip_from_cidr')
+def ip_from_cidr(prefix):
+ """ Take an IPv4/IPv6 CIDR host and strip cidr mask.
+ Example:
+ 192.0.2.1/24 -> 192.0.2.1, 2001:db8::1/64 -> 2001:db8::1
+ """
+ from ipaddress import ip_interface
+ return str(ip_interface(prefix).ip)
@register_filter('address_from_cidr')
def address_from_cidr(prefix):
@@ -316,3 +324,9 @@ def is_file(filename):
if os.path.exists(filename):
return os.path.isfile(filename)
return False
+
+@register_filter('get_ipv4')
+def get_ipv4(interface):
+ """ Get interface IPv4 addresses"""
+ from vyos.ifconfig import Interface
+ return Interface(interface).get_addr_v4()