#!/usr/bin/awk -f # # USAGE: # make this script executable with `chmod +x dhcpparse.awk` and then # # cat dhcpd.conf|./dhcpparse.awk|sort # set the RECORD SEPARATOR, RS, to "}" ... records span multiple lines BEGIN {RS="}"} # TODO: figure out how to skip comment lines without having to use sed first length($0) > 5 { total++ for(i=1;i<=NF;i++) { counter[total] = total # if this field matches the word "host" if($i ~ /^host$/) { type[total] = "host" hostname[total]=$(i+1) # Remove the trailing { that might be there gsub(/{/, "", hostname[total]) } # if this field matches the word "hardware" else if($i ~ /^hardware$/) { # get rid of the trailing semi-colon split($(i+2),arr,";") mac[total]=arr[1] } # if this field matches the word "fixed-address" else if($i ~ /^fixed-address$/) { # get rid of the trailing semi-colon split($(i+1),arr,";") ip[total]=arr[1] } # if this field matches the word "subnet-mask" else if($i ~ /^subnet-mask$/) { # get rid of the trailing semi-colon split($(i+1),arr,";") subnet[total]=arr[1] } # if this field matches the word "routers" else if($i ~ /^routers$/) { # get rid of the trailing semi-colon split($(i+1),arr,";") routers[total]=arr[1] } # if this field matches the word "domain-name-servers" else if($i ~ /^domain-name-servers$/) { # get rid of the trailing semi-colon split($(i+1),arr,";") domains[total]=arr[1] } # if this field matches the word "boot-file-name" else if($i ~ /^bootfile-name$/) { # get rid of the trailing semi-colon split($(i+1),arr,";") boot[total]=arr[1] } } } # for every entry we captured, display its appropriate info END { for(entry in counter) { if(type[entry] == "host") { printf("ip=%s mac=%s subnet=%s hostname=%s routers=%s domains=%s boot=%s \n",\ ip[entry],mac[entry],subnet[entry],hostname[entry],routers[entry],domains[entry],boot[entry]) } } }