Initial commit
This commit is contained in:
37
snippets/is_valid_ipv4.sh
Executable file
37
snippets/is_valid_ipv4.sh
Executable file
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
## - Test of valid IPv4 Address
|
||||
## -
|
||||
## - Returns 0 if valid, > 0 otherwise
|
||||
## -
|
||||
is_valid_ipv4() {
|
||||
local -a octets=( ${1//\./ } )
|
||||
local RETURNVALUE=0
|
||||
|
||||
# return an error if the IP doesn't have exactly 4 octets
|
||||
[[ ${#octets[@]} -ne 4 ]] && return 1
|
||||
|
||||
for octet in ${octets[@]}
|
||||
do
|
||||
if [[ ${octet} =~ ^[0-9]{1,3}$ ]]
|
||||
then # shift number by 8 bits, anything larger than 255 will be > 0
|
||||
((RETURNVALUE += octet>>8 ))
|
||||
else # octet wasn't numeric, return error
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
return ${RETURNVALUE}
|
||||
}
|
||||
|
||||
if [ "X$@" = "X" ]; then
|
||||
echo -e "\n\t\033[33m\033[1mNo argumnet given!\033[m\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if is_valid_ipv4 $@ ; then
|
||||
echo -e "\n\t\033[32m\033[1m$@ is a valid IPv4 Address\033[m\n"
|
||||
else
|
||||
echo -e "\n\t\033[31m\033[1m$@ is NOT a valid IPv4 Address\033[m\n"
|
||||
fi
|
||||
|
||||
exit
|
||||
Reference in New Issue
Block a user