How to query macOS DNS resolver from Terminal?

Solution 1:

It seems taking the effort to formulate the question I understood the problem better to perform more precise google searches and found the answer.

On macOS querying DNS is achieved by:

$ dscacheutil -q host -a name example.com
name: example.com
ipv6_address: 2606:2800:220:1:248:1893:25c8:1946

name: example.com
ip_address: 93.184.216.34

Like most macOS shell commands it is not quite as nice as the unix equivalent, but it gives me consistent results with native mac apps.

I found this from https://random.ac/cess/2018/04/12/macos-dig-vs-dscacheutil-while-using-split-dns-with-viscosity-vpn-client/

Solution 2:

In addition to the great dig (pun intended) of the dscacheutil usage that's not covered by the man documentation, I'd like to add that at the core of the macOs name resolution system sits the mDNSresponder service, and it seems to be gathering all different sources of the lookup information under the common interface.

The tool for interacting with the mDNS subsystem is dns-sd, and as such, there is another way of querying for the DNS records that will honor all available resolvers:

~ » dns-sd -G v4v6 google.com 
DATE: ---Wed 04 Nov 2020---
19:05:47.081  ...STARTING...
Timestamp     A/R    Flags if Hostname                               Address                                      TTL
19:05:47.175  Add        2  0 google.com.                            2A00:1450:4001:081D:0000:0000:0000:200E%<0>  377
19:05:47.255  Add        2  0 google.com.                            172.217.22.110                               377

or

~ » dns-sd -q google.com   
DATE: ---Wed 04 Nov 2020---
20:27:50.834  ...STARTING...
Timestamp     A/R    Flags if Name                          Type  Class   Rdata
20:27:50.925  Add        2  0 google.com.                   Addr   IN     172.217.22.110

or even

~ » dns-sd -q google.com TXT
DATE: ---Wed 04 Nov 2020---
20:30:45.908  ...STARTING...
Timestamp     A/R    Flags if Name                          Type  Class   Rdata
20:30:48.831  Add        3  0 google.com.                   TXT    IN     36 bytes: 23 76 3D 73 70 66 31 20 69 6E 63 6C 75 64 65 3A 5F 73 70 66 2E 67 6F 6F 67 6C 65 2E 63 6F 6D 20 7E 61 6C 6C
20:30:48.831  Add        3  0 google.com.                   TXT    IN     46 bytes: 2D 64 6F 63 75 73 69 67 6E 3D 30 35 39 35 38 34 38 38 2D 34 37 35 32 2D 34 65 66 32 2D 39 35 65 62 2D 61 61 37 62 61 38 61 33 62 64 30 65
20:30:48.831  Add        3  0 google.com.                   TXT    IN     46 bytes: 2D 64 6F 63 75 73 69 67 6E 3D 31 62 30 61 36 37 35 34 2D 34 39 62 31 2D 34 64 62 35 2D 38 35 34 30 2D 64 32 63 31 32 36 36 34 62 32 38 39
20:30:48.831  Add        3  0 google.com.                   TXT    IN     60 bytes: 3B 66 61 63 65 62 6F 6F 6B 2D 64 6F 6D 61 69 6E 2D 76 65 72 69 66 69 63 61 74 69 6F 6E 3D 32 32 72 6D 35 35 31 63 75 34 6B 30 61 62 30 62 78 73 77 35 33 36 74 6C 64 73 34 68 39 35
20:30:48.831  Add        2  0 google.com.                   TXT    IN     65 bytes: 40 67 6C 6F 62 61 6C 73 69 67 6E 2D 73 6D 69 6D 65 2D 64 76 3D 43 44 59 58 2B 58 46 48 55 77 32 77 6D 6C 36 2F 47 62 38 2B 35 39 42 73 48 33 31 4B 7A 55 72 36 63 31 6C 32 42 50 76 71 4B 58 38 3D

Truth to be told, this is not the best command for scripting as it operates in an interactive continuous monitoring mode that would notify of any changes on the given address until one interrupts it. Although it allows for some fun stuff like watching a VPN-only path to disappear and then reappear when you reconnect:

~ » dns-sd -G v4v6 vpn.only.address.com    
DATE: ---Wed 04 Nov 2020---
19:10:01.672  ...STARTING...
Timestamp     A/R    Flags if Hostname                               Address                                      TTL
19:10:01.673  Add 40000002  0 vpn.only.address.com.                      10.100.100.100                                333
19:10:01.759  Add        2  0 vpn.only.address.com.                      0000:0000:0000:0000:0000:0000:0000:0000%<0>  323   No Such Record
19:10:07.703  Rmv        0  0 vpn.only.address.com.                      10.100.100.100                                0
19:10:10.783  Add        2  0 vpn.only.address.com.                      0.0.0.0                                      4502   No Such Record
19:10:10.905  Add        2  0 vpn.only.address.com.                      0000:0000:0000:0000:0000:0000:0000:0000%<0>  4502   No Such Record
19:10:32.669  Add        3  0 vpn.only.address.com.                      10.100.100.100                                298
19:10:32.670  Add        2  0 vpn.only.address.com.                      0000:0000:0000:0000:0000:0000:0000:0000%<0>  228   No Such Record