{"id":919,"date":"2016-09-22T16:02:44","date_gmt":"2016-09-22T14:02:44","guid":{"rendered":"http:\/\/dety.net.ua\/?p=919"},"modified":"2016-09-22T16:03:20","modified_gmt":"2016-09-22T14:03:20","slug":"pfsense-import-dhcp-maps-from-dhcpd-conf","status":"publish","type":"post","link":"https:\/\/dety.net.ua\/?p=919","title":{"rendered":"PFSense + import DHCP-mappings from dhcpd.conf"},"content":{"rendered":"<p>I wanted migrate my static mapping from my dhcpd.conf to PFSense.<\/p>\n<p>Script to parse data from dhcpd.conf to xml-text:<\/p>\n<blockquote><p>#!\/usr\/bin\/awk -f<br \/>\n#<br \/>\n# Author: Matt Pascoe &#8211; matt@opennetadmin.com<br \/>\n# Forked: Konstantin Shalygin &#8211; kostya@opentech.ru<br \/>\n# Forked2: Rexxer, for PFSense<\/p>\n<p># USAGE:<br \/>\n# .\/dhcpparse.awk \/etc\/dhcpd.conf<\/p>\n<p># Note that for hosts, it will try a reverse lookup on the IP address<br \/>\n# to determine an appropriate dns name<\/p>\n<p># set the RECORD SEPARATOR, RS, to &#8220;}&#8221; &#8230; records span multiple lines<br \/>\nBEGIN {RS=&#8221;}&#8221;}<\/p>\n<p>length($0) &gt; 5 { total++<\/p>\n<p>for(i=1;i&lt;=NF;i++) {<\/p>\n<p>counter[total] = total<\/p>\n<p>if($i ~ \/^host$\/) {<br \/>\ntype[total] = &#8220;host&#8221;<br \/>\nhostname[total]=$(i+1)<br \/>\n# Remove the trailing { that might be there<br \/>\ngsub(\/{\/, &#8220;&#8221;, hostname[total])<br \/>\n}<br \/>\nelse if($i ~ \/^hardware$\/) {<br \/>\nsplit($(i+2),arr,&#8221;;&#8221;)<br \/>\nmac[total]=arr[1]<br \/>\n}<br \/>\nelse if($i ~ \/^fixed-address$\/) {<br \/>\nsplit($(i+1),arr,&#8221;;&#8221;)<br \/>\nip[total]=arr[1]<br \/>\n}<br \/>\nelse if($i ~ \/^subnet$\/) {<br \/>\ntype[total] = &#8220;subnet&#8221;<br \/>\nsplit($(i+1),arr,&#8221;\\&#8221;&#8221;)<br \/>\nsubnetip[total]=arr[1]<br \/>\n}<br \/>\nelse if($i ~ \/^netmask$\/) {<br \/>\nsubnetmask[total]=$(i+1)<br \/>\n}<br \/>\nelse if($i ~ \/^routers$\/) {<br \/>\nsplit($(i+1),arr,&#8221;;&#8221;)<br \/>\nrouters[total]=arr[1]<br \/>\n}<br \/>\nelse if($i ~ \/^range$\/) {<br \/>\nrangestart[total]=$(i+1)<br \/>\nsplit($(i+2),arr,&#8221;;&#8221;)<br \/>\nrangeend[total]=arr[1]<br \/>\n}<br \/>\nelse if($i ~ \/^domain-name-servers$\/) {<br \/>\nsplit($(i+1),arr,&#8221;;&#8221;)<br \/>\nnameservers[total]=arr[1]<br \/>\n}<br \/>\nelse if($i ~ \/^domain-name$\/) {<br \/>\nsplit($(i+1),arr,&#8221;;&#8221;)<br \/>\ndomain[total]=arr[1]<br \/>\ngsub(\/&#8221;|&#8221;\/, &#8220;&#8221;, domain[total])<br \/>\n}<br \/>\nelse if($i ~ \/^ntp-servers$\/) {<br \/>\nsplit($(i+1),arr,&#8221;;&#8221;)<br \/>\nntpservers[total]=arr[1]<br \/>\n}<br \/>\n}<br \/>\nif( length(ip[total]) &gt; 0 ) {<br \/>\ncommand = (&#8220;host &#8221; ip[total])<br \/>\ncommand | getline tmpname<br \/>\nclose(command)<br \/>\nsplit(tmpname,n,&#8221;pointer&#8221;)<\/p>\n<p>gsub(\/^[ \\t]+\/, &#8220;&#8221;, n[2])<br \/>\nname[total] = substr(n[2],0,length(n[2])-1)<br \/>\n}<br \/>\nif( length(name[total]) == 0 ) {<br \/>\nname[total]=&#8221;dhcpload-&#8221; ip[total]<br \/>\n}<br \/>\n}<\/p>\n<p>END {<br \/>\nfor(entry in counter) {<br \/>\nif(type[entry] == &#8220;host&#8221;) {<br \/>\nprint(&#8220;&lt;staticmap&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;mac&gt;&#8221; mac[entry] &#8220;&lt;\/mac&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;cid\/&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;ipaddr&gt;&#8221; ip[entry] &#8220;&lt;\/ipaddr&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;hostname&gt;&#8221; hostname[entry] &#8220;&lt;\/hostname&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;descr&gt;&lt;![CDATA[&#8221; hostname[entry] &#8220;]]&gt;&lt;\/descr&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;filename\/&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;rootpath\/&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;defaultleasetime\/&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;maxleasetime\/&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;gateway\/&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;domain\/&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;domainsearchlist\/&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;ddnsdomain\/&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;ddnsdomainprimary\/&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;ddnsdomainkeyname\/&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;ddnsdomainkey\/&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;tftp\/&gt;&#8221;)<br \/>\nprint(&#8220;\\011&lt;ldap&gt;&lt;\/ldap&gt;&#8221;)<br \/>\nprint(&#8220;&lt;\/staticmap&gt;&#8221;)<br \/>\n}<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<p>Download config.xml from PFSense &#8211; Backup\/Restore menu.<\/p>\n<p>Insert in this config the generated xml-text in the appropriate section.<\/p>\n<p>Restore the configuration for DHCP-Server section only.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8211; matt@opennetadmin.com # Forked: Konstantin Shalygin &#8211; kostya@opentech.ru # Forked2: Rexxer, for PFSense # USAGE: # .\/dhcpparse.awk \/etc\/dhcpd.conf # Note that for hosts, it will try a reverse lookup [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,13,17],"tags":[],"class_list":["post-919","post","type-post","status-publish","format-standard","hentry","category-freebsd","category-novosti","category-pfsense"],"_links":{"self":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=919"}],"version-history":[{"count":2,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/919\/revisions"}],"predecessor-version":[{"id":921,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/919\/revisions\/921"}],"wp:attachment":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}