brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: dig +yaml prints the whole reply in a format scripts can parse

Dig
~/tips/tip-dig-yaml-output

dig +yaml example.com A

Get dig's full answer as YAML, so a script can read fields instead of scraping text.

dig’s normal output is made for people. When a script needs the TTL, the flags and the answer, scraping that text is fragile. Newer dig versions can print YAML instead.

dig +yaml example.com A
dig +yaml +qr example.com MX @9.9.9.9

The reply comes out as a YAML document with the message type, the server address and port, the time, and a response_message_data block holding the opcode, status, flags, and the question and answer sections. Each answer record sits in its own list item. With +qr the outgoing query is printed as its own document too.

I like it combined with yq to pull out one field, for example the status, without fighting with grep and awk. It also makes a nice log format for queries you want to compare later.

Check your version first. The option arrived in BIND 9.16, and delv and mdig got it at the same time. macOS still ships dig 9.10, which rejects it, while a dig from Homebrew or a current Linux distribution has it. The layout has changed over time too: recent BIND versions split records and flags into structured fields, so a script written against an older dig’s YAML may need an update. For a single value, +short is still simpler, and for JSON you need another tool such as kdig or doggo.

Further reading: dig manual (BIND 9), BIND 9.16.0 release notes

Going deeper: Guide: dig for beginners, look up DNS records and read the answer

More Dig tips dig +noall +answer DS example.com dig nope.iana.org @9.9.9.9 | grep -E 'status|SOA' dig +search nas dig @8.8.8.8 www.google.com +subnet=203.0.113.0/24 dig +short +identify example.com @9.9.9.9 All Dig tips (36) All topics

Link