Skip to content

Instantly share code, notes, and snippets.

@KevinRohn
Created November 3, 2023 15:06
Show Gist options
  • Save KevinRohn/ce824f7388dbe48a2bff8a6d998be515 to your computer and use it in GitHub Desktop.
Save KevinRohn/ce824f7388dbe48a2bff8a6d998be515 to your computer and use it in GitHub Desktop.
validate IPv4
#!/bin/bash
IP=$1
result=`echo $IP | awk -F"\." ' $0 ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 '` 2>/dev/null
if [ "$result" = "" ]; then
echo "IPv4 is invalid: $IP"
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment