Skip to content

Instantly share code, notes, and snippets.

@mattpascoe
Created August 29, 2022 21:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpascoe/99b2f6e6950f19f97a11c05139f6084e to your computer and use it in GitHub Desktop.
Save mattpascoe/99b2f6e6950f19f97a11c05139f6084e to your computer and use it in GitHub Desktop.
Parse bind zone files to produce dcm.pl output.
#!/usr/bin/awk -f
#
# Author: Matt Pascoe - matt@opennetadmin.com
#
# This awk script is used to extract relevant information from a bind zone
# file and build the appropriate fields for passing into
# a dcm.pl module. This can be used to bootstrap a new database from existing
# site data.
#
# You can simply cat a file and pipe it to this script
#
# cat bindzone|awk -f bindparse.awk
#
BEGIN {} {
total++
count[total]=total
recordtype[total]="unknown"
# Gather origin so we can use it later to build names
if ($_ ~ /.*ORIGIN.*/) {
origin=$2
}
# A records
if ($_ ~ /.*IN .*A/) {
recordtype[total]="a"
if ($1 ~ /\./) {
fqdn[total]=$1
} else{
fqdn[total]=$1 "." origin
}
ip[total]=$4
makeptr[total]="N"
}
# CNAME records
if ($_ ~ /.*IN .*CNAME/) {
recordtype[total]="cname"
if ($1 ~ /\./) {
fqdn[total]=$1
} else{
fqdn[total]=$1 "." origin
}
if ($4 ~ /\./) {
target[total]=$4
} else{
target[total]=$4 "." origin
}
}
# TXT record
if ($_ ~ /.*IN .*TXT/) {
recordtype[total]="txt"
if ($1 ~ /\./) {
fqdn[total]=$1
} else{
fqdn[total]=$1 "." origin
}
text[total]=$4
gsub(/=/, "\\=", text[total]);
}
# SRV record
if ($_ ~ /.*IN .*SRV/) {
recordtype[total]="srv"
if ($1 ~ /\./) {
fqdn[total]=$1
} else{
fqdn[total]=$1 "." origin
}
pri[total]=$4
weight[total]=$5
port[total]=$6
if ($7 ~ /\./) {
target[total]=$7
} else{
target[total]=$7 "." origin
}
}
# TODO: PTR records
# For now, not doing it.. its better to let the A record create
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]
}
# TODO: NS record
# not super needed as ONA does this part for you.
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]
# 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 ($_ ~ /.*IN .*MX/) {
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]
}
}
# skip SOA for now
if ($_ ~ /.*IN .*SOA/) {
recordtype[total]="soa"
}
# skip comments
if ($_ ~ /^;.*/) {
recordtype[total]="comment"
}
# If the location was blank, make it default
if (length(loc[total]) == 0) {
loc[total] = "default"
}
#cleanup trailing dot
gsub(/\.$/, "", fqdn[total]);
gsub(/\.$/, "", target[total]);
}
END {
sort = "sort -k 2nr"
dcmvar = "dcm.pl -r dns_record_add"
#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 (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 (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] == "a") {
printf("%s type=%s name=%s ip=%s addptr=%s\n", dcmvar,recordtype[entry],fqdn[entry],ip[entry],makeptr[entry])
}
if (recordtype[entry] == "cname") {
printf("%s type=%s name=%s pointsto=%s\n", dcmvar,recordtype[entry],fqdn[entry],target[entry])
}
if (recordtype[entry] == "txt") {
printf("%s type=%s name=%s txt=%s\n", dcmvar,recordtype[entry],fqdn[entry],text[entry])
}
if (recordtype[entry] == "srv") {
printf("%s type=%s name=%s srv_pri=%s srv_weight=%s srv_port=%s pointsto=%s\n", dcmvar,recordtype[entry],fqdn[entry],pri[entry],weight[entry],port[entry],target[entry])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment