diff --git a/load/01-basic b/load/01-basic index 851cd5b..fdccf9c 100644 --- a/load/01-basic +++ b/load/01-basic @@ -59,7 +59,7 @@ rsynchg() { rsync --help | grep "\-$1"; } gip () { if which jq &> /dev/null; then - curl -s "https://dns.google/resolve?name=$1" | jq -r '.Answer[] | .data' + curl -s "https://dns.google/resolve?name=$1" | jq -r '.Answer[] | .data' | tail -n 1 return 0 else echo install jq to use this function @@ -72,6 +72,23 @@ gpip () { } -dip () { -/bin/drill $1 | grep -A1 'ANSWER SECTION' | grep -v "ANSWER SECTION" | rev | cut -f1 | rev +valid_ipv4() { + local ip="$1" + [[ "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] || return 1 + for i in ${ip//./ }; do + [[ "${#i}" -gt 1 && "${i:0:1}" == 0 ]] && return 1 + [[ "$i" -gt 255 ]] && return 1 + done + return 0 } + +dip () { + local ip4 + ip4=$(/bin/drill $1 | grep -A1 'ANSWER SECTION' | grep -v "ANSWER SECTION" | rev | cut -f1 | rev) + # echo first: $ip4 + if valid_ipv4 $ip4; then + echo $ip4 + else + /bin/drill $ip4 | grep -A1 'ANSWER SECTION' | grep -v "ANSWER SECTION" | rev | cut -f1 | rev + fi +} \ No newline at end of file