Skip to content

Instantly share code, notes, and snippets.

@wernersbacher
Created March 31, 2020 08:43
Show Gist options
  • Save wernersbacher/23889ac05fab4b786ddf3afe87104b6e to your computer and use it in GitHub Desktop.
Save wernersbacher/23889ac05fab4b786ddf3afe87104b6e to your computer and use it in GitHub Desktop.
Bash: Check if IP-Address is valid
function valid_ip()
{
local ip=$1
local stat=1
regexv6='^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$'
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
elif [[ $ip =~ $regexv6 ]]; then
stat=0
fi
return $stat
}
cat my_ip_list.txt | while read ipadr
do
if valid_ip $ipadr; then
echo IP $ipadr is a valid IP!
else
echo IP $ipadr is a not a IP, skipping!.
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment