Skip to content

Instantly share code, notes, and snippets.

@jpmens

jpmens/discussion.md

Last active Dec 22, 2020
Embed
What would you like to do?
Determine the number of delegated ccTLDs from a root zone transfer

Students should determine the number of delegated two-letter country code Top-Level Domains (ccTLDs) from a root zone transfer. They will actually copy/paste the command into a terminal but should be able to explain what the command does. The current answer is: 248

The challenge is finding a version which is simple enough for people with "not very much" Unix experience to understand what the pipeline does. The pipeline must be portable across Unix, Linux, macOS.

The source of data is a full zone transfer from the F-root server:

dig @f.root-servers.net . AXFR

(1)

| awk -F '[\t]+' '$1 ~ /^[a-z][a-z]\.$/ { print $1 }' | sort -u | wc -l

(2)

| awk '$1 ~ /^[a-z][a-z]\.$/ { if ($4 == "NS") print $1 }'  | sort -u | wc -l

(3)

| awk '{ if (length($1) == 3 && $4 == "NS") print $1 }' | sort -u | wc -l

(4)

still uses awk, but is maybe easier to understand?

| awk '{print $1,$4}' |grep '^[a-z][a-z]\..*NS$' | sort -u |wc -l

(5)

the grep(1) used solely to speed things up:

| grep '^[a-z][a-z]\.' | while read domain ttl class rtype rdata; do [ $rtype == NS ] && echo $domain; done | sort -u | wc -l

(6)

using cut(1) only won't cut it because some lines have multiple tabs; tr -s to the rescue

| tr -s '\t' | cut -f1,4 | grep '^[a-z][a-z]\..*NS$' | sort -u | wc -l

(7)

From the not-so-simple dept, via @fanf: 😂

| perl -ne '$cc{$1} = () if m{^(\w\w)\.\s}; END { print "$_\n" for sort keys %cc }' | wc -l

(8)

again via @fanf:

| tr -c '[a-z].\n' = | grep '^[a-z][a-z][.]=' | sed 's/\..*//' | sort -u | wc -l

(9)

| awk '{ if (length($1) == 3 && $4 == "NS") arr[$1] = 1 } END { for (a in arr) { print a } }'  | wc -l
| awk '{ if (length($1) == 3 && $4 == "NS") arr[$1] = 1 } END { print length(arr) }'

(10)

from Phil, who says: grep -o is not POSIX but is widespread, works on FreeBSD, Linux (with GNU utils) and macOS ... and should even work with busybox grep.

| grep -Eo '^[a-z]{2}\.[[:space:]]' | sort -u | wc -l

non DNS

(a)

curl -sf https://data.iana.org/TLD/tlds-alpha-by-domain.txt | grep '^[A-Z][A-Z]$' | wc -l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment