I have a domain name in a bash variable ($TARGET
), and I want to get the IPv4 (A
record) address of it in my bash script (I also want to get the IPv6 AAAA
record (if exists)), in a robust manner.
i.e. What bash/unix command will: (i) print the IP address (and only the IP address) of the DNS resolution (ii) print nothing to stdout (or non-zero exit) if there is no DNS record for that domain found (iii) allow me to choose to ask for A
or AAAA
record.
host "$TARGET"
prints lots of human readable stuff, and I'm reluctant to parse that.
dig +short
looked promising, but in some scenarios, it also prints other warning messages which I do not understand, or care about.
$ dig +short -t A "$TARGET"
;; Warning: Client COOKIE mismatch
1.2.3.4
Debian test/bookworm. Bash v5.2 (installed via apt)