Skip to content

Instantly share code, notes, and snippets.

@compor
Created February 25, 2013 14:50
Show Gist options
  • Select an option

  • Save compor/5030293 to your computer and use it in GitHub Desktop.

Select an option

Save compor/5030293 to your computer and use it in GitHub Desktop.
check ipv4 validity
# the argument must in a dot decimal ipv4 format
function is_valid_ipv4() {
if [ -z "$1" ]; then
return 0;
fi
IFS="."
local isvalid=1
local bytectr=0
for byte in $1; do
bytectr=$(( $bytectr + 1 ))
# check if the 1st byte is greater than 0
if [ ${bytectr} -eq 1 ] && [ ${byte} -le 0 ]; then
isvalid=0
break
fi
if [ ${byte} -lt 0 ] || [ ${byte} -gt 255 ]; then
isvalid=0
break
fi
done
if [ ${bytectr} -ne 4 ]; then
isvalid=0
fi
unset IFS
return ${isvalid}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment