OPNsense Forum

Archive => 24.1, 24.4 Legacy Series => Topic started by: Monju0525 on February 05, 2024, 04:47:17 PM

Title: [Solved]Python script converts Opnsense configuration file into kea reservations
Post by: Monju0525 on February 05, 2024, 04:47:17 PM
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?
Title: Re: Opnsense configuration file for KEA DHCP reservation leases
Post by: miracuru on February 17, 2024, 01:10:13 PM
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.
Title: Re: Opnsense configuration file for KEA DHCP reservation leases
Post by: Patrick M. Hausen on February 17, 2024, 01:16:27 PM
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/
Title: Re: Opnsense configuration file for KEA DHCP reservation leases
Post by: Monju0525 on February 17, 2024, 02:35:39 PM
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.
Title: Re: Opnsense configuration file for KEA DHCP reservation leases
Post by: Monju0525 on February 17, 2024, 03:27:48 PM
@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

Title: Re: Opnsense configuration file for KEA DHCP reservation leases
Post by: Monju0525 on February 17, 2024, 06:51:42 PM
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.
Title: Re: Opnsense configuration file for KEA DHCP reservation leases
Post by: Monju0525 on February 17, 2024, 10:34:20 PM
@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>


Title: Re: Opnsense configuration file for KEA DHCP reservations
Post by: Monju0525 on February 18, 2024, 06:05:30 PM
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
Title: Re: Done-Python script converts Opnsense configuration file into kea reservations
Post by: miracuru on February 20, 2024, 10:48:40 PM
Hey cool. Thank you so much.
Sorry for my late answer. Was a few days off due to migraine.
Title: Re: Done-Python script converts Opnsense configuration file into kea reservations
Post by: Monju0525 on February 21, 2024, 01:34:59 PM
@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!
Title: Re: Done-Python script converts Opnsense configuration file into kea reservations
Post by: miracuru on February 21, 2024, 07:51:54 PM
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.
Title: Re: Done-Python script converts Opnsense configuration file into kea reservations
Post by: Monju0525 on February 23, 2024, 02:24:38 PM
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
Title: Re: Done-Python script converts Opnsense configuration file into kea reservations
Post by: Monju0525 on February 23, 2024, 05:21:53 PM
@ 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.
Title: Re: [Solved]Python script converts Opnsense configuration file into kea reservations
Post by: Monju0525 on February 25, 2024, 05:17:40 PM
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]
Title: Re: [Solved]Python script converts Opnsense configuration file into kea reservations
Post by: MoonbeamFrame on February 25, 2024, 06:14:58 PM
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.
Title: Re: [Solved]Python script converts Opnsense configuration file into kea reservations
Post by: Monju0525 on February 25, 2024, 07:51:44 PM
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.









Title: Re: [Solved]Python script converts Opnsense configuration file into kea reservations
Post by: MoonbeamFrame on February 25, 2024, 09:17:10 PM
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.
Title: Re: [Solved]Python script converts Opnsense configuration file into kea reservations
Post by: Monju0525 on February 26, 2024, 01:17:05 AM
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.
Title: Re: [Solved]Python script converts Opnsense configuration file into kea reservations
Post by: randyg503 on March 09, 2024, 06:10:53 PM
curious if/how this script might accommodate the below 24.1.3 change:

     o kea-dhcp: add import/export as CSV on reservations
Title: Re: [Solved]Python script converts Opnsense configuration file into kea reservations
Post by: MoonbeamFrame on March 09, 2024, 10:16:02 PM

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.
Title: Re: [Solved]Python script converts Opnsense configuration file into kea reservations
Post by: Monju0525 on March 10, 2024, 12:50:53 AM
@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

Title: Re: [Solved]Python script converts Opnsense configuration file into kea reservations
Post by: randyg503 on March 10, 2024, 02:52:20 AM
Thank you for your feedback regarding my question and for your work on this script.
Title: Re: [Solved]Python script converts Opnsense configuration file into kea reservations
Post by: Monju0525 on March 10, 2024, 03:20:15 AM
@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.