mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
[enh] Add helper for IP address validation
This commit is contained in:
parent
7729d91e78
commit
dfd8780a10
1 changed files with 52 additions and 0 deletions
52
data/apps/helpers.d/ip
Normal file
52
data/apps/helpers.d/ip
Normal file
|
@ -0,0 +1,52 @@
|
|||
# Validate an IP address
|
||||
#
|
||||
# example: ynh_validate_ip 4 111.222.333.444
|
||||
#
|
||||
# usage: ynh_validate_ip <family> <ip_address>
|
||||
#
|
||||
# exit code : 0 for valid ip addresses, 1 otherwise
|
||||
ynh_validate_ip()
|
||||
{
|
||||
# http://stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python#319298
|
||||
|
||||
IP_ADDRESS_FAMILY=$1
|
||||
IP_ADDRESS=$2
|
||||
|
||||
[ "$IP_ADDRESS_FAMILY" == "4" ] || [ "$IP_ADDRESS_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["$IP_ADDRESS_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>
|
||||
#
|
||||
# exit code : 0 for valid ipv4 addresses, 1 otherwise
|
||||
ynh_validate_ip4()
|
||||
{
|
||||
ynh_validate_ip 4 $1
|
||||
}
|
||||
|
||||
|
||||
# Validate an IPv6 address
|
||||
#
|
||||
# example: ynh_validate_ip6 2000:dead:beef::1
|
||||
#
|
||||
# usage: ynh_validate_ip6 <ip_address>
|
||||
#
|
||||
# exit code : 0 for valid ipv6 addresses, 1 otherwise
|
||||
ynh_validate_ip6()
|
||||
{
|
||||
ynh_validate_ip 6 $1
|
||||
}
|
Loading…
Add table
Reference in a new issue