summaryrefslogtreecommitdiff
path: root/python/vyos/template.py
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2021-05-13 19:46:14 +0200
committerChristian Poessinger <christian@poessinger.com>2021-05-14 21:39:46 +0200
commitc5989a799856476ec45adf40c35df57dbf53a559 (patch)
treebc548c466fab18db0c34abd57ff09c7a2b37bfd8 /python/vyos/template.py
parentd96336a808e500934fc4fd9423345d0b965d35ac (diff)
downloadvyos-1x-c5989a799856476ec45adf40c35df57dbf53a559.tar.gz
vyos-1x-c5989a799856476ec45adf40c35df57dbf53a559.zip
vyos.template: T3535: add helper for IP address conversion
Support getting the IPv4 addresses from a given interface and support to strip CIDR mask from address.
Diffstat (limited to 'python/vyos/template.py')
-rw-r--r--python/vyos/template.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/python/vyos/template.py b/python/vyos/template.py
index 3fbb33acb..e1986b1e4 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):
@@ -361,3 +369,9 @@ def natural_sort(iterable):
return [convert(c) for c in re.split('([0-9]+)', str(key))]
return sorted(iterable, key=alphanum_key)
+
+@register_filter('get_ipv4')
+def get_ipv4(interface):
+ """ Get interface IPv4 addresses"""
+ from vyos.ifconfig import Interface
+ return Interface(interface).get_addr_v4()