Skip to content

Instantly share code, notes, and snippets.

@marco79cgn
Created November 23, 2025 11:44
Show Gist options
  • Select an option

  • Save marco79cgn/3a368993031384e51af37859629fcdbd to your computer and use it in GitHub Desktop.

Select an option

Save marco79cgn/3a368993031384e51af37859629fcdbd to your computer and use it in GitHub Desktop.
Check if return value is a valid ipv4 address
#!/bin/bash
myAddrUrl="REPLACE.myaddr.io"
# max 6 tries every 5 seconds (if connection is currently offline)
if ! currentDynDns=$(dig +time=5 +tries=6 +short $myAddrUrl @86.54.11.100); then
echo "Problem: dig network error or timeout."
exit 1
fi
# check if ip syntax is correct
case "$currentDynDns" in
''|*[!.0-9]*) echo "Keine IPv4-Adresse" ; exit ;;
esac
# check if ipv4 has 4 blocks and only digits between 1-255
set -- $(echo "$currentDynDns" | tr '.' ' ')
[ $# -ne 4 ] && echo "Keine IPv4-Adresse" && exit
for octet in "$@"; do
[ "$octet" -ge 0 ] 2>/dev/null && [ "$octet" -le 255 ] || {
echo "Keine gültige IPv4-Adresse"
exit
}
done
echo "Gültige IPv4-Adresse: $currentDynDns"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment