I can manually create a DHCP static lease or a KEA reservation per device. But how do I avoid entering each device's MAC address etc via the GUI? I already have that info in a pfsense config .xml file. Yes pfsense. In addition, opnsense's .xml KEA DHCP section also needs a reservation and subnet uuid per device. Is there a tool to generate these values?
Hi. Did you figured it out already?
I'm at the same point like you. Searching too for the config file, to avoid entering all reservations in the web gui.
I can give also an update, if I figured it out.
There's a uuid command line utility in e.g. MacPorts or Homebrew:
$ uuid
1f876390-cd8e-11ee-a1ba-6b8af6d72721
$ uuid
2015d0c6-cd8e-11ee-b340-032d755e306c
$ uuid
2077689a-cd8e-11ee-a8eb-2bc263ba9a06
I use it frequently for scripting.
http://www.ossp.org/pkg/lib/uuid/
I wrote a python program that takes my pfsense isc-dhcp static lease and converts it into a opnsense kea dhcp reservations xml format. I had 40+ pfsense static leases that I didn't want to re-enter into the opnsense gui. The python script uses uuid4 , a random generated uuid and has a hyphen format. I will try to upload to a GitHub repo. Stay tune.
@miracuru
I wrote python code (windows) that will generate random uuid per device and saves the output to abc.txt
Just modify the the directory path of the output_file
>>> python3 uuid3.py
import uuid
output_file = open(r'C:\Users\patri\Documents\python\abc.txt', 'w')
devices = ["dev1", "dev2","dev3"]
for x in devices:
u = uuid.uuid4()
print(x, ": ",u)
#output_file.writelines(x, ": ",u)
output_file.writelines(f"{'':>10}{x}{": "}{u}{"\n"}")
# Close file
output_file.close()
# Checking if the data is written to file or not
output_file = open(r'C:\Users\patri\Documents\python\abc.txt', 'r')
print("Read the saved file: \n")
print(output_file.read())
output_file.close()
abc.txt
dev1: 298007e3-2244-42cc-a9d7-bcf15b6d6de0
dev2: 8b59170a-b013-46d5-bb3e-31c6529b7299
dev3: a22907f6-0e6c-4779-bf2a-7794c72f1c8f
Here is the link to GitHub for the .xml conversion of pfsense isc-dhcp static lease to opnsense kea-dhcp reservation info.
https://github.com/patrick0525/Python-Pf-isc-Opn-kea
Run the Python code with sample pfsense .xml data.
@miracuru
I finally got the conversion of opnsense isc static lease to opnsense kea reservation working.
It was a one line change to my original posted script.
I will post it tomorrow on GitHub.
From the opnsense config .xml
The python script reads a cut&paste that contains the isc dhcp static lease. Need to add ur subnet uuid to the python code prior to execution.
<dhcpd>
<lan>
<enable>1</enable>
<ddnsdomainalgorithm>hmac-md5</ddnsdomainalgorithm>
<numberoptions>
<item/>
</numberoptions>
<range>
<from>10.59.11.200</from>
<to>10.59.11.245</to>
</range>
<winsserver/>
<dnsserver/>
<ntpserver/>
<staticmap>
<mac>a1:b1:c1:d1:e1:f1</mac>
<ipaddr>10.59.11.100</ipaddr>
<hostname>DAD-DESKTOP</hostname>
<descr>DAD-DESKTOP</descr>
<winsserver/>
<dnsserver/>
<ntpserver/>
</staticmap>
<staticmap>
<mac>a2:b2:c2:d2:e2:f2</mac>
<ipaddr>10.59.11.101</ipaddr>
<hostname>MOM-DESKTOP</hostname>
<descr>MOM-DESKTOP</descr>
<winsserver/>
<dnsserver/>
<ntpserver/>
</staticmap>
</lan>
</dhcpd>
And writes a file that contains the conversion to kea dhcp reservations:
<reservation uuid="c7495e59-000b-4bf3-b083-0d35f099e946">
<subnet>4e3016b1-b603-44bd-a361-b33c44333c98</subnet>
<ip_address>10.59.11.100</ip_address>
<hw_address>a1:b1:c1:d1:e1:f1</hw_address>
<hostname>DAD-DESKTOP</hostname>
<description>DAD-DESKTOP</description>
</reservation>
<reservation uuid="c82430df-45c7-4d9b-b11b-22f228af7e02">
<subnet>4e3016b1-b603-44bd-a361-b33c44333c98</subnet>
<ip_address>10.59.11.101</ip_address>
<hw_address>a2:b2:c2:d2:e2:f2</hw_address>
<hostname>MOM-DESKTOP</hostname>
<description>MOM-DESKTOP</description>
</reservation>
Done.
Converts all opnense isc-dhcp static lease devices to an opnsese kea-dhcp reservations format
It will read/process an existing config-OPNsense.xml that contains isc-dhcp data and output a kea-dhcp .xml that needs to be cut and paste into ur existing config-OPNsense.xml
A sample pre-populated isc-dhcp config-OPNsense.xml is provided for testing.
https://github.com/patrick0525/Python-Opn-isc-kea
Hey cool. Thank you so much.
Sorry for my late answer. Was a few days off due to migraine.
@miracuru
I am putting the final touches to the "fully automatic" isc-to-kea (static leases to reservations) conversion. From a confg-OPNsenseIsc.xml that has all your isc-dhcp static leases,
you need to do the following:
disable isc-dhcp,
enable kea-dhcp (make sure it works),
Create one valid working kea reservation (only one)
and save as config-OPNsenseKea,xml.
Modify and run the *,py program to read config-OPNsenseKea,xml.
(I edit and run the windows version of python312 on notepad++)
The result is a merge.xml. Restore opnsense to merge.xml. It has ISC-dhcp is disabled, the original static leases, KEA-dhcp is enabled and the new pool of reservations merged/translated from ISC-dhcp static leases.
Last night, I restored to the merge.xml and there were new kea reservations.
Stay tune for the new github repo.
pew-pew!
Thank you very much.
Hmm, I tried out KEA only for a very short time. It didn't worked correctly for me.
My devices received correctly IP addresses from dhcp. But no internet connection was possible.
But I don't want to go offtopic. If the problem after the last updates persist, I dig deeper or raise a separate thread for this.
I updated https://github.com/patrick0525/Python-Opn-isc-kea
The merged config is called merge.xml
The original config is never touched.
# OPNsense admin: copy OPNsense_isc_to_kea_reservations.py and the two input_file
# into a directory
# input_file: current set to config-OPNsense.localdomain-20240218111111.xml
# input_file: config-OPNsense.localdomain-20240218000000.xml
#
# >>>> python3 OPNsense_isc_to_kea_reservations.py
#
# >>>>>> TO MODIFY YOUR OPNsense CONFIG <<<<<<
# OPNsense admin: Search [ADD YOUR CONFIG] and change input_file and add
# your config-OPNsense.localdomain-2024*.xml
#
# merge_file: merge.xml
@ miracuru
A new release. Put the config-OPNsense*.xml and *.py in the same directory and run. No more more cutting and pasting files. It is converted automatically by the Python script. I also will call u out if your config-OPNsense*.xml has no valid kea reservation or a missing kea subnet uuid.
Updated the repo to support a command line argument.
Just copy the *.py into the config directory and
run on your config-OPNsense*.xml
python opnsense_isc_to_kea_reservations.py [config-OPNsense*.xml]
Are you searching the config-OPNsense*.xml for which interfaces use DHCP or are you just converting DHCP for the lan interface?
I have no lan network but I do have a number of Vlans which use DHCP and was unable to get to a merge.xml
I don't code in python and so far my attempts at modifying your script have yet to be successful from my linux workstation.
The script looks at the contents of
['dhcpd']['lan']['staticmap'] and essentially copies the contents into the kea section
['reservations'] starting as ['reservation uuid']
U will see what I migrated by running:
opnsense_isc_to_kea_reservations.py config-OPNsense.localdomain-20240218111111.xml
Diff the merge.xml and config-OPNsense.localdomain-20240218111111.xml
It does not do vlans but it something I can look into.
I am not familiar with vlans but tell me what you want to migrate from and to.
I need xml tags or gators🐊 and your sanitized config-OPNsense*.xml file.
Also, I need to know what the final populated xml should look like.
I did look at the contents of your merge output.
This will be the first time I've used Kea, so I'm not sure what the final result will need to be.
When I get time I'll run another machine with my firewall config on it and add additional subnets into Kea. Then see what the xml looks like.
I'll also dig out some old bash scripts I wrote which I used to migrate in ISC DHCP configs when I moved some clients from an external ISC DHCP server to OPNsense.
If u enable kea, move one of the vlans to a kea reservation, save the new config and then diff the orig config vs the new kea config , u will see what actually moved with kea enabled.
curious if/how this script might accommodate the below 24.1.3 change:
o kea-dhcp: add import/export as CSV on reservations
If export/import is added to the ISC DHCPv4/v6 using the same file format as the existing Kea export/import, then migration of reservations between the two becomes trivial.
@randyg503
It does not. I wrote the script on 24.1.2 before they released kea csv import/export functionality. My use case was to move the current isc static leases into kea reservations. It assumes u have a working kea dhcp with one kea reservation created. No export/import needed since it automatically does the merges into a newly created merge.xml. Ur original config-OPNsense*.xml is never modified.
On 02252024, I made changes and now it supports a command line argument.
Install Python3 ( I installed python 3.12.1 on my W11 desktop)
>>> python3 opnsense_isc_to_kea_reservations.py <your_config-OPNsense*.xml>
Note: I regex check ur opnsense config file name format.
The migrated changes are in merge.xml
https://github.com/patrick0525/Python-Opn-isc-kea
Thank you for your feedback regarding my question and for your work on this script.
@randyg53
No problem. It really added to my Python, xml, ElementTree, Python dictionary, json object, json array, regex understanding and knowledge. It was challenging but it is not my day job.