-
Notifications
You must be signed in to change notification settings - Fork 0
/
__ip_address.sh
executable file
·29 lines (29 loc) · 2.06 KB
/
__ip_address.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env bash
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __is_ipv4_address
# DESCRIPTION: Verifies whether the input is a valid IPv4 address
# USAGE: __is_ipv4_address "172.20.23.14"
#
# if __is_ipv4_address "172.20.23.14"; then
# ...do something...
# fi
# RETURNS: Status code '0' if valid, and '1' if invalid
#----------------------------------------------------------------------------------------------------------------------
__is_ipv4_address() {
ipv4pcre="((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}"
grep -E "^${ipv4pcre}$" <<< "$1" > /dev/null 2>&1
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __is_ipv6_address
# DESCRIPTION: Verifies whether the input is a valid IPv6 address
# USAGE: __is_ipv6_address "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
#
# if __is_ipv6_address "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; then
# ...do something...
# fi
# RETURNS: Status code '0' if valid, and '1' if invalid
#----------------------------------------------------------------------------------------------------------------------
__is_ipv6_address() {
ipv6pcre="(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))"
grep -E "^${ipv6pcre}$" <<< "$1" > /dev/null 2>&1
}