Created
November 23, 2025 11:44
-
-
Save marco79cgn/3a368993031384e51af37859629fcdbd to your computer and use it in GitHub Desktop.
Check if return value is a valid ipv4 address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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