Skip to content

Instantly share code, notes, and snippets.

@mattpascoe
Created November 6, 2012 18:02

Revisions

  1. mattpascoe revised this gist Nov 8, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions tinydnsparse.awk
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    #!/usr/bin/awk -f
    #
    # Author: Matt Pascoe - [email protected]
    #
    # This awk script is used to extract relevant information from a tinydns data
    # config file and build a csv with appropriate fields for passing into
    # a dcm.pl module. This can be used to bootstrap a new database from existing
  2. mattpascoe created this gist Nov 6, 2012.
    156 changes: 156 additions & 0 deletions tinydnsparse.awk
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,156 @@
    #!/usr/bin/awk -f
    #
    # This awk script is used to extract relevant information from a tinydns data
    # config file and build a csv with appropriate fields for passing into
    # a dcm.pl module. This can be used to bootstrap a new database from existing
    # site data.
    #
    # This script assumes the data to be reasonably correct as it is expected that
    # you are running the data file in production and the tinydns-data program
    # has already parsed it with no errors.
    #
    # TODO: Output in a more direct dcm.pl format
    # TODO: make sections for the following record types:
    # Z
    # '
    # .
    #
    # You can simply cat a file and pipe it to this script
    #
    # cat tinydatafile|awk -f tinydnsparse.awk
    #
    BEGIN {} {
    total++
    count[total]=total
    recordtype[total]="unknown"

    # process + type tinydns records, A only
    if ($_ ~ /^\+/) {
    split($1,arr,":")
    gsub(/^\+/, "", arr[1]);
    recordtype[total]="a"
    fqdn[total]=arr[1]
    ip[total]=arr[2]
    ttl[total]=arr[3]
    loc[total]=arr[5]
    makea[total]="Y"
    makeptr[total]="N"
    }

    # process = type tinydns records, A and PTR
    if ($_ ~ /^\=/) {
    split($1,arr,":")
    gsub(/^\=/, "", arr[1]);
    recordtype[total]="aptr"
    fqdn[total]=arr[1]
    ip[total]=arr[2]
    ttl[total]=arr[3]
    loc[total]=arr[5]
    makea[total]="Y"
    makeptr[total]="Y"
    }

    # process ^ type tinydns records, PTR only
    if ($_ ~ /^\^/) {
    split($1,arr,":")
    gsub(/^\^/, "", arr[1]);
    recordtype[total]="ptr"
    fqdn[total]=arr[1]
    ip[total]=arr[2]
    ttl[total]=arr[3]
    loc[total]=arr[5]
    makea[total]="N"
    makeptr[total]="Y"
    }

    # process C type tinydns records, CNAME
    if ($_ ~ /^\C/) {
    split($1,arr,":")
    gsub(/^\C/, "", arr[1]);
    recordtype[total]="cname"
    fqdn[total]=arr[1]
    ip[total]=arr[2]
    ttl[total]=arr[3]
    loc[total]=arr[5]
    makea[total]="N"
    makeptr[total]="N"
    }

    # process & type tinydns records, NS record
    if ($_ ~ /^\&/) {
    split($1,arr,":")
    gsub(/^\&/, "", arr[1]);
    recordtype[total]="ns"
    fqdn[total]=arr[1]
    ip[total]=arr[2]
    srvname[total]=arr[3]
    ttl[total]=arr[4]
    loc[total]=arr[6]
    makea[total]="Y"
    makeptr[total]="N"

    # If the server was blank, format it to tinydns default x.ns.fqdn
    if (srvname[total] !~ /\./) {
    srvname[total] = srvname[total]".ns."fqdn[total]
    }
    }

    # process @ type tinydns records, MX record
    if ($_ ~ /^\@/) {
    distance[total]="0"
    split($1,arr,":")
    gsub(/^\@/, "", arr[1])
    recordtype[total]="mx"
    fqdn[total]=arr[1]
    ip[total]=arr[2]
    srvname[total]=arr[3]
    distance[total]=arr[4]
    ttl[total]=arr[5]
    loc[total]=arr[7]
    makea[total]="Y"
    makeptr[total]="N"

    # If the server was blank, format it to tinydns default x.ns.fqdn
    if (srvname[total] !~ /\./) {
    srvname[total] = srvname[total]".mx."fqdn[total]
    }
    }

    # If the location was blank, make it default
    if (length(loc[total]) == 0) {
    loc[total] = "default"
    }
    }

    END {
    sort = "sort -k 2nr"
    #sort = "sort -k 2nr| column -t -s,"
    for(entry in count) {
    if (recordtype[entry] == "ns") {
    printf("%s,%s,%s,%s,%s,%s\n", recordtype[entry],loc[entry],fqdn[entry],ip[entry],ttl[entry],srvname[entry])
    # if an IP was supplied, make an A record entry as well
    if (length(ip[entry]) != 0) {
    recordtype[entry] = "a"
    }
    }
    if (recordtype[entry] == "mx") {
    printf("%s,%s,%s,%s,%s,%s,%s\n", recordtype[entry],loc[entry],fqdn[entry],ip[entry],ttl[entry],srvname[entry],distance[entry])
    # if an IP was supplied, make an A record entry as well
    if (length(ip[entry]) != 0) {
    recordtype[entry] = "a"
    }
    }
    if (recordtype[entry] == "a") {
    printf("%s,%s,%s,%s,%s,%s,%s\n", recordtype[entry],loc[entry],fqdn[entry],ip[entry],ttl[entry],makea[entry],makeptr[entry])
    }
    if (recordtype[entry] == "aptr") {
    printf("%s,%s,%s,%s,%s,%s,%s\n", recordtype[entry],loc[entry],fqdn[entry],ip[entry],ttl[entry],makea[entry],makeptr[entry])
    }
    if (recordtype[entry] == "ptr") {
    printf("%s,%s,%s,%s,%s,%s,%s\n", recordtype[entry],loc[entry],fqdn[entry],ip[entry],ttl[entry],makea[entry],makeptr[entry])
    }
    if (recordtype[entry] == "cname") {
    printf("%s,%s,%s,%s,%s,%s,%s\n", recordtype[entry],loc[entry],fqdn[entry],ip[entry],ttl[entry],makea[entry],makeptr[entry])
    }
    }
    }