Created
November 6, 2012 18:02
-
-
Save mattpascoe/4026407 to your computer and use it in GitHub Desktop.
An AWK script to parse a tinydns data file into a CSV for later use by a dcm.pl script to load into opennetadmin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
# 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]) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment