summaryrefslogtreecommitdiff
path: root/python/vyos/template.py
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2020-11-01 21:46:12 +0100
committerChristian Poessinger <christian@poessinger.com>2020-11-01 21:46:12 +0100
commitb279895fa34d51fe2a2a56449df564bc734d1db9 (patch)
tree4d1d89936a1f4adcb67e0b96e5618e360a5abaf6 /python/vyos/template.py
parenteb1ed5e518a08c488fd05ce9cf63b63a5a25c21a (diff)
downloadvyos-1x-b279895fa34d51fe2a2a56449df564bc734d1db9.tar.gz
vyos-1x-b279895fa34d51fe2a2a56449df564bc734d1db9.zip
vyos.template: consolidate is_ipv(4|6) functionality
Diffstat (limited to 'python/vyos/template.py')
-rw-r--r--python/vyos/template.py18
1 files changed, 9 insertions, 9 deletions
diff --git a/python/vyos/template.py b/python/vyos/template.py
index cc582bf79..389f6927f 100644
--- a/python/vyos/template.py
+++ b/python/vyos/template.py
@@ -146,20 +146,22 @@ def vyos_netmask_from_cidr(text):
def vyos_ipv4(text):
""" Filter IP address, return True on IPv4 address, False otherwise """
from ipaddress import ip_interface
- return ip_interface(text).version == 4
+ try: return ip_interface(text).version == 4
+ except: return False
@register_filter('ipv6')
def vyos_ipv6(text):
""" Filter IP address, return True on IPv6 address, False otherwise """
from ipaddress import ip_interface
- return ip_interface(text).version == 6
+ try: return ip_interface(text).version == 6
+ except: return False
@register_filter('first_host_address')
def vyos_first_host_address(text):
""" Return first usable (host) IP address from given prefix.
Example:
- 10.0.0.0/24 -> 10.0.0.1
- - 2001:db8::/64 -> 2001:db8::1
+ - 2001:db8::/64 -> 2001:db8::
"""
from ipaddress import ip_interface
from ipaddress import IPv4Network
@@ -174,8 +176,8 @@ def vyos_first_host_address(text):
def vyos_last_host_address(text):
""" Return first usable IP address from given prefix.
Example:
- - 10.0.0.0/24 -> 10.0.0.1
- - 2001:db8::/64 -> 2001:db8::1
+ - 10.0.0.0/24 -> 10.0.0.254
+ - 2001:db8::/64 -> 2001:db8::ffff:ffff:ffff:ffff
"""
from ipaddress import ip_interface
from ipaddress import IPv4Network
@@ -183,11 +185,9 @@ def vyos_last_host_address(text):
addr = ip_interface(text)
if addr.version == 4:
- addr = IPv4Network(addr)
- else:
- addr = IPv6Network(addr)
+ return str(IPv4Network(addr).broadcast_address - 1)
- return str(addr.broadcast_address -1)
+ return str(IPv6Network(addr).broadcast_address)
@register_filter('inc_ip')
def vyos_inc_ip(text, increment):