[Solved]Python script converts Opnsense configuration file into kea reservations

Started by Monju0525, February 05, 2024, 04:47:17 PM

Previous topic - Next topic
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/
Deciso DEC750
People who think they know everything are a great annoyance to those of us who do. (Isaac Asimov)

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>



February 18, 2024, 06:05:30 PM #7 Last Edit: February 18, 2024, 06:26:25 PM by Monju0525
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.