03 / ROUTING AND RECORDED DIAGNOSTICS · LESSON 10

Compare DNS answer sets

Normalise DNS names and compare A-record sets without losing multiple answers.

18 min · learn at your pace

A name can have several addresses

A DNS A-record export can contain several addresses for one name. Loading a single value per name would overwrite earlier answers and create false differences. Compare a set of name/address pairs instead, deduplicating repeated rows within each input.

These inputs contain validated ASCII DNS names and canonical IPv4 addresses. Normalise names by lowercasing them and removing one optional trailing root dot. That makes EDGE.EXAMPLE. and edge.example equivalent for this exercise. It is not a general internationalised-domain parser and does not follow CNAME chains.

Describe exactly what was compared

The expected file describes required A records. The observed file is a supplied collector result. A pair in both is present; a pair only in expected is missing; a pair only in observed is unexpected. Retain every distinct pair in the union, including multiple addresses for one name.

These labels compare two exports. They do not prove authoritative DNS state, caching behaviour or current reachability. Real collection should record its server, query type, response code and time. No DNS requests leave this lab; the aim is a repeatable comparison that stays useful when answer order changes or rows appear more than once.

Try an example.sh
awk -F '\t' 'NR>1 {name=tolower($1);sub(/\.$/,"",name);print name,$2}' expected.tsv
YOUR TURN

Put it into practice.

Compare expected.tsv and observed.tsv, each name<TAB>address. Names are validated ASCII DNS names with an optional trailing dot; addresses are canonical IPv4. Lowercase names and remove one trailing dot, deduplicate pairs, and print name<TAB>address<TAB>state for the union, sorted lexically by name then address under LC_ALL=C. State is present when both contain the pair, missing when only expected does, and unexpected when only observed does. Preserve multiple A records per name.

See an explained solution
One way to solve it.sh
export LC_ALL=C
printf 'name\taddress\tstate\n'
awk -F '\t' 'FNR==1{next}{name=tolower($1);sub(/\.$/,"",name);k=name SUBSEP $2;names[k]=name;ips[k]=$2;if(FILENAME==ARGV[1])expected[k]=1;else observed[k]=1}
END{for(k in names){s=(k in expected)?((k in observed)?"present":"missing"):"unexpected";printf "%s\t%s\t%s\n",names[k],ips[k],s}}' expected.tsv observed.tsv | sort

Canonical names and pair membership remove harmless spelling, ordering and duplicate-row differences. The three labels describe evidence from the two supplied exports without inferring a live DNS state.