OPNsense Forum

English Forums => General Discussion => Topic started by: migrator on August 23, 2018, 09:34:58 am

Title: [SOLVED] dhcp static mappings import using modified backup-file
Post by: migrator on August 23, 2018, 09:34:58 am
Hello,

i'm trying to import static DHCP entries into OPNsense since i have i.e. lots of printers in various subnets.
Since there seems to be no official way, nore did i found anything except for pfsense, itried to modify the related sections of a downloaded backup (.xml) and reimport that again.
I added entries  for <ipaddr>,  <mac>, <decr> and <hostname> in the xml-code, so it loooks identically to entries done by the GUI
Code: [Select]
  <dhcpd>
    <opt3>
      <enable>1</enable>
      <numberoptions/>
      <range>
        <from>192.168.110.50</from>
        <to>192.168.110.55</to>
      </range>
      <staticmap>
        <mac>00:25:90:cd:0d:98</mac>
        <ipaddr>192.168.110.100</ipaddr>
        <hostname>110-100</hostname>
        <descr>Backup</descr>
      </staticmap>
    </opt3>
    <opt7>
      <enable>1</enable>
      <numberoptions/>
      <range>
        <from>192.168.245.46</from>
        <to>192.168.245.56</to>
      </range>
      <staticmap>
        <mac>c8:5b:76:4f:ae:a0</mac>
        <ipaddr>192.168.245.34</ipaddr>
        <hostname>245-34</hostname>
        <descr> laptop 28.11.2016</descr>
        <mac>c8:5b:76:4f:a4:f3</mac>
        <ipaddr>192.168.245.35</ipaddr>
        <hostname>245-35</hostname>
        <descr> laptop 28.11.2016</descr>
        <mac>50:7B:9D:E4:7B:F0</mac>
        <ipaddr>192.168.245.36</ipaddr>
        <hostname>245-36</hostname>
        <descr> laptop 20.12.2016</descr>
      </staticmap>
    </opt7>
When importing the XML-file i receive the following error in one line for all the added entires (existing mappings, done via the GUI remain and stay the same):
Code: [Select]
Warning: htmlspecialchars() expects parameter 1 to be string, array given in /usr/local/www/services_dhcp.php on line 1121 / 1124 /1127  /1130.
Reviewing te code of services_dhcp.php doesn't help, i don't find an error on my site - did i miss anything here?
Is the back.xml-file secured via checksum or can't be modified at all?
Did anyone tried to import static DHCP mappings or has a better guess how to do this?

Thank you for reading and any ideas.
Title: Re: dhcp static mappings import using modified backup-file
Post by: migrator on August 28, 2018, 08:18:16 am
It works now. I missed the <staticmaps>-tags for each host :o
If anyone needs to read out host entries from an isc-dhcp-server, too, here is my script ...

Code: [Select]
#!/bin/bash

# read active ip-addr from isc-dhcp-server and write xml-structure for opnsense
# etries in dhcpd.conf looks like:
# host 110-100 { hardware ethernet 00:25:90:cd:0d:98; fixed-address 192.168.110.100; } # my comment

dhcpin=/etc/dhcp/dhcpd.conf
dhcpout=$(hostname)-dhcp-4-opnsense.xml

grep '^host' $dhcpin | while read line
do
echo "      <staticmap>" >> $dhcpout
mac=$(echo $line | cut -d '{' -f 2 | cut -d ' ' -f 4 | cut -d';' -f 1)
echo "        <mac>$mac</mac>" >> $dhcpout
ip=$(echo $line | cut -d ';' -f 2 | cut -d ' ' -f 3)
echo "        <ipaddr>$ip</ipaddr>" >> $dhcpout
hn=$(echo $line | cut -d ' ' -f 2)
echo "        <hostname>$hn</hostname>" >> $dhcpout
beschr=$(echo $line | cut -d '#' -f 2)
echo "        <descr>$beschr</descr>" >> $dhcpout
echo "      </staticmap>" >> $dhcpout
done

I copied the scripts output into the special sections of the OPNsense-backup-file an reimported that. To be sure about the needed structure it is useful to create some entries before. At least the script saved me  a lot of time ...

Cheers
Title: Re: [SOLVED] dhcp static mappings import using modified backup-file
Post by: Snowmanut on February 12, 2019, 12:39:57 am
Trying to figure out how to use this to get my PFsense dhcp mappings into Opnsense. Newb to scripts of any sort. Tried in windows but doesn't like this. Any help for a newb on how to use this? I got my Pfsense dhcpd.conf file and tried running it a as scrip i windows but doesn't like grep.

Another way to import dhcp mappings as I have alot.
Title: Re: dhcp static mappings import using modified backup-file
Post by: alexdelprete on November 20, 2020, 05:56:53 am
It works now. I missed the <staticmaps>-tags for each host :o
If anyone needs to read out host entries from an isc-dhcp-server, too, here is my script ...

Code: [Select]
#!/bin/bash

# read active ip-addr from isc-dhcp-server and write xml-structure for opnsense
# etries in dhcpd.conf looks like:
# host 110-100 { hardware ethernet 00:25:90:cd:0d:98; fixed-address 192.168.110.100; } # my comment

dhcpin=/etc/dhcp/dhcpd.conf
dhcpout=$(hostname)-dhcp-4-opnsense.xml

grep '^host' $dhcpin | while read line
do
echo "      <staticmap>" >> $dhcpout
mac=$(echo $line | cut -d '{' -f 2 | cut -d ' ' -f 4 | cut -d';' -f 1)
echo "        <mac>$mac</mac>" >> $dhcpout
ip=$(echo $line | cut -d ';' -f 2 | cut -d ' ' -f 3)
echo "        <ipaddr>$ip</ipaddr>" >> $dhcpout
hn=$(echo $line | cut -d ' ' -f 2)
echo "        <hostname>$hn</hostname>" >> $dhcpout
beschr=$(echo $line | cut -d '#' -f 2)
echo "        <descr>$beschr</descr>" >> $dhcpout
echo "      </staticmap>" >> $dhcpout
done

I copied the scripts output into the special sections of the OPNsense-backup-file an reimported that. To be sure about the needed structure it is useful to create some entries before. At least the script saved me  a lot of time ...

Cheers

Thanks a lot for the script. I had my static mapping list in an excel file, I recreated your source structure in excel, pasted in a dhcpd.conf file, tweaked it a little bit to make sure it was perfect, and then ran the script and I created 250 entries that were imported perfectly. Manually would have been a nightmare. :)
Title: Re: [SOLVED] dhcp static mappings import using modified backup-file
Post by: userbenutzer on June 27, 2023, 08:03:30 pm
Hi,
does not work with ipfire for me.
But we find a file named fixleases and the following script works for that.

Code: [Select]
#!/bin/bash

# read active fixleases from /var/ipfire/dhcp/fixleases and write xml-structure for opnsense
# entries in fixleases look like:
# aa:00:aa:00:aa:00,192.168.0.31,on,,,,edv-pc20

dhcpin=/var/ipfire/dhcp/fixleases
dhcpout=$(hostname)-dhcp-4-opnsense.xml

cat $dhcpin | while read line
do
echo "      <staticmap>" >> $dhcpout
mac=$(echo $line | cut -d ',' -f 1)
echo "        <mac>$mac</mac>" >> $dhcpout
ip=$(echo $line | cut -d ',' -f 2)
echo "        <ipaddr>$ip</ipaddr>" >> $dhcpout
descr=$(echo $line | cut -d ',' -f 7)
echo "        <descr>$descr</descr>" >> $dhcpout
echo "      </staticmap>" >> $dhcpout
done

Thx
Title: Re: [SOLVED] dhcp static mappings import using modified backup-file
Post by: moody-blue on September 24, 2023, 06:05:38 pm
This tool is also very usefull https://github.com/GeekVisit/uproot
Title: Re: [SOLVED] dhcp static mappings import using modified backup-file
Post by: yourfriendarmando on September 24, 2023, 06:50:18 pm
Here is also an alternative to loading a list.

We used a spreadsheet, which could be based on one's network inventory. Check it out and see if it's useful:
https://forum.opnsense.org/index.php?topic=33943.0