diff --git a/data/helpers.d/ip b/data/helpers.d/ip deleted file mode 100644 index 2ca4053d9..000000000 --- a/data/helpers.d/ip +++ /dev/null @@ -1,76 +0,0 @@ -#!/bin/bash - -# Validate an IP address -# -# usage: ynh_validate_ip --family=family --ip_address=ip_address -# | ret: 0 for valid ip addresses, 1 otherwise -# -# example: ynh_validate_ip 4 111.222.333.444 -# -# Requires YunoHost version 2.2.4 or higher. -ynh_validate_ip() -{ - # http://stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python#319298 - - # Declare an array to define the options of this helper. - local legacy_args=fi - declare -Ar args_array=( [f]=family= [i]=ip_address= ) - local family - local ip_address - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - [ "$family" == "4" ] || [ "$family" == "6" ] || return 1 - - python /dev/stdin << EOF -import socket -import sys -family = { "4" : socket.AF_INET, "6" : socket.AF_INET6 } -try: - socket.inet_pton(family["$family"], "$ip_address") -except socket.error: - sys.exit(1) -sys.exit(0) -EOF -} - -# Validate an IPv4 address -# -# example: ynh_validate_ip4 111.222.333.444 -# -# usage: ynh_validate_ip4 --ip_address=ip_address -# | ret: 0 for valid ipv4 addresses, 1 otherwise -# -# Requires YunoHost version 2.2.4 or higher. -ynh_validate_ip4() -{ - # Declare an array to define the options of this helper. - local legacy_args=i - declare -Ar args_array=( [i]=ip_address= ) - local ip_address - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - ynh_validate_ip 4 $ip_address -} - - -# Validate an IPv6 address -# -# example: ynh_validate_ip6 2000:dead:beef::1 -# -# usage: ynh_validate_ip6 --ip_address=ip_address -# | ret: 0 for valid ipv6 addresses, 1 otherwise -# -# Requires YunoHost version 2.2.4 or higher. -ynh_validate_ip6() -{ - # Declare an array to define the options of this helper. - local legacy_args=i - declare -Ar args_array=( [i]=ip_address= ) - local ip_address - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - ynh_validate_ip 6 $ip_address -} diff --git a/data/helpers.d/network b/data/helpers.d/network index 4dc080203..d750d549f 100644 --- a/data/helpers.d/network +++ b/data/helpers.d/network @@ -101,3 +101,78 @@ ynh_webpath_register () { sudo yunohost app register-url $app $domain $path_url } + +# Validate an IP address +# +# usage: ynh_validate_ip --family=family --ip_address=ip_address +# | ret: 0 for valid ip addresses, 1 otherwise +# +# example: ynh_validate_ip 4 111.222.333.444 +# +# Requires YunoHost version 2.2.4 or higher. +ynh_validate_ip() +{ + # http://stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python#319298 + + # Declare an array to define the options of this helper. + local legacy_args=fi + declare -Ar args_array=( [f]=family= [i]=ip_address= ) + local family + local ip_address + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + [ "$family" == "4" ] || [ "$family" == "6" ] || return 1 + + python /dev/stdin << EOF +import socket +import sys +family = { "4" : socket.AF_INET, "6" : socket.AF_INET6 } +try: + socket.inet_pton(family["$family"], "$ip_address") +except socket.error: + sys.exit(1) +sys.exit(0) +EOF +} + +# Validate an IPv4 address +# +# example: ynh_validate_ip4 111.222.333.444 +# +# usage: ynh_validate_ip4 --ip_address=ip_address +# | ret: 0 for valid ipv4 addresses, 1 otherwise +# +# Requires YunoHost version 2.2.4 or higher. +ynh_validate_ip4() +{ + # Declare an array to define the options of this helper. + local legacy_args=i + declare -Ar args_array=( [i]=ip_address= ) + local ip_address + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_validate_ip 4 $ip_address +} + + +# Validate an IPv6 address +# +# example: ynh_validate_ip6 2000:dead:beef::1 +# +# usage: ynh_validate_ip6 --ip_address=ip_address +# | ret: 0 for valid ipv6 addresses, 1 otherwise +# +# Requires YunoHost version 2.2.4 or higher. +ynh_validate_ip6() +{ + # Declare an array to define the options of this helper. + local legacy_args=i + declare -Ar args_array=( [i]=ip_address= ) + local ip_address + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_validate_ip 6 $ip_address +}