Rexxer

Some tips for me and other

PFSense + import DHCP-mappings from dhcpd.conf

I wanted migrate my static mapping from my dhcpd.conf to PFSense.

Script to parse data from dhcpd.conf to xml-text:

#!/usr/bin/awk -f
#
# Author: Matt Pascoe – matt@opennetadmin.com
# Forked: Konstantin Shalygin – kostya@opentech.ru
# Forked2: Rexxer, for PFSense

# USAGE:
# ./dhcpparse.awk /etc/dhcpd.conf

# Note that for hosts, it will try a reverse lookup on the IP address
# to determine an appropriate dns name

# set the RECORD SEPARATOR, RS, to “}” … records span multiple lines
BEGIN {RS=”}”}

length($0) > 5 { total++

for(i=1;i<=NF;i++) {

counter[total] = total

if($i ~ /^host$/) {
type[total] = “host”
hostname[total]=$(i+1)
# Remove the trailing { that might be there
gsub(/{/, “”, hostname[total])
}
else if($i ~ /^hardware$/) {
split($(i+2),arr,”;”)
mac[total]=arr[1]
}
else if($i ~ /^fixed-address$/) {
split($(i+1),arr,”;”)
ip[total]=arr[1]
}
else if($i ~ /^subnet$/) {
type[total] = “subnet”
split($(i+1),arr,”\””)
subnetip[total]=arr[1]
}
else if($i ~ /^netmask$/) {
subnetmask[total]=$(i+1)
}
else if($i ~ /^routers$/) {
split($(i+1),arr,”;”)
routers[total]=arr[1]
}
else if($i ~ /^range$/) {
rangestart[total]=$(i+1)
split($(i+2),arr,”;”)
rangeend[total]=arr[1]
}
else if($i ~ /^domain-name-servers$/) {
split($(i+1),arr,”;”)
nameservers[total]=arr[1]
}
else if($i ~ /^domain-name$/) {
split($(i+1),arr,”;”)
domain[total]=arr[1]
gsub(/”|”/, “”, domain[total])
}
else if($i ~ /^ntp-servers$/) {
split($(i+1),arr,”;”)
ntpservers[total]=arr[1]
}
}
if( length(ip[total]) > 0 ) {
command = (“host ” ip[total])
command | getline tmpname
close(command)
split(tmpname,n,”pointer”)

gsub(/^[ \t]+/, “”, n[2])
name[total] = substr(n[2],0,length(n[2])-1)
}
if( length(name[total]) == 0 ) {
name[total]=”dhcpload-” ip[total]
}
}

END {
for(entry in counter) {
if(type[entry] == “host”) {
print(“<staticmap>”)
print(“\011<mac>” mac[entry] “</mac>”)
print(“\011<cid/>”)
print(“\011<ipaddr>” ip[entry] “</ipaddr>”)
print(“\011<hostname>” hostname[entry] “</hostname>”)
print(“\011<descr><![CDATA[” hostname[entry] “]]></descr>”)
print(“\011<filename/>”)
print(“\011<rootpath/>”)
print(“\011<defaultleasetime/>”)
print(“\011<maxleasetime/>”)
print(“\011<gateway/>”)
print(“\011<domain/>”)
print(“\011<domainsearchlist/>”)
print(“\011<ddnsdomain/>”)
print(“\011<ddnsdomainprimary/>”)
print(“\011<ddnsdomainkeyname/>”)
print(“\011<ddnsdomainkey/>”)
print(“\011<tftp/>”)
print(“\011<ldap></ldap>”)
print(“</staticmap>”)
}
}
}

Download config.xml from PFSense – Backup/Restore menu.

Insert in this config the generated xml-text in the appropriate section.

Restore the configuration for DHCP-Server section only.

 

Comments are currently closed.