The dig +short
command (such as described in "dig show only answer") is great for batch processing names into IP addresses. It does a simple job and does it well.
Unfortunately when there's a CNAME even +short
isn't short enough. For example:
$ dig +short docs.sbonds.org
ghs.google.com.
173.194.69.121
I've tried +noall
but it doesn't seem like it changes the behavior of +short
. I've also tried specifying -t a
just to ensure it didn't think I meant an A record or CNAME, but that (unsurprisingly) changes nothing.
$ dig +noall +short docs.sbonds.org
ghs.google.com.
173.194.69.121
I'm using RedHat 7's dig
:
# dig -v
DiG 9.9.4-RedHat-9.9.4-73.el7_6
I can filter out the CNAMEs with trusty grep
, but it seems like dig should have some way to give "Just the IP, ma'am."
What is that way?
Both the CNAME and the corresponding A (or whatever type you asked for) belong to the answer-section. As such there is no dig option (at least not in RHEL7's dig) that could filter out the CNAME response.
I think you will have to rely on
dig +short [...] | grep -v '\.$'
to remove the CNAME responses.dig
is a troubleshooting tool, so it sends DNS queries and receives DNS answers and as Andreas said the answer is both CNAME and A records, as designed. Your wish is to give "Just the IP, ma'am.", so that is not a DNS troubleshooting, it is "just" the resolution, for whichdig
is too much.nslookup
nslookup
is inferior todig
but will still give you too much:host
host
is simpler but will still return "too much" for you (but note that it returns also the IPv6 address which is good):getent
Depending on your Unix system,
getent
can be used. Note however that this may or may not do a DNS query because you configure in/etc/nsswitch.conf
the source of data per service, and forhosts
it will probably be a mix of bothfiles
(which is the venerable/etc/hosts
) and the DNS.Observe also that on a proper Unix setup it will favor IPv6 over IPv4 so that may be a problem for you (this should depend on the configuration in
/etc/gai.conf
)In fact
hosts
does not honor/etc/gai.conf
, you need to useahosts
instead, which will usegetaddrinfo
and hencegai.conf
. Observe that you get a list (whose order depends ongai.conf
configuration):Perl
If you are allowed to write a simple script, you have many solutions, like:
DOH
Or use any DOH (DNS over HTTPS) endpoint (or similar) with any HTTP client. Examples:
Systemd
Systemd has its own resolver application:
You still need to parse it in some way, but it does give the direct final IP addresses (there is a flag when you invoke it to make it not follow
CNAME
records for whatever use cases that need that)