OPNsense Forum

Archive => 21.1 Legacy Series => Topic started by: marius_siuram on April 08, 2021, 10:46:45 am

Title: Wanting to use an API for DHCP managing (failing with OMAPI with pypureomapi)
Post by: marius_siuram on April 08, 2021, 10:46:45 am
My goal was to automate certain IP management tasks, and discovered that OPNsense's DHCP supports OMAPI (since 20.1 if I'm not mistaken). I am using an up-to-date OPNsense 21.1 installation.

Once activated and configured, I can see that OMAPI port is working (netstat -an | grep 7911 shows it). However, after adding a lease from the web UI, if I try to get with pypureomapi, it fails:

In [5]: omapi.lookup_ip("de:ad:be:ef:ca:fe")
---------------------------------------------------------------------------
OmapiErrorNotFound                        Traceback (most recent call last)
<ipython-input-5-83d144de8394> in <module>
----> 1 omapi.lookup_ip("de:ad:be:ef:ca:fe")

~...python3.8/site-packages/pypureomapi.py in lookup_ip(self, mac)
   1101                 @raises socket.error:
   1102                 """
-> 1103                 res = self.lookup_by_lease(mac=mac)
   1104                 try:
   1105                         return res["ip-address"]

~...python3.8/site-packages/pypureomapi.py in lookup_by_lease(self, **kwargs)
   1181
   1182         def lookup_by_lease(self, **kwargs):
-> 1183                 return self.__lookup("lease", **kwargs)
   1184
   1185         def __lookup(self, ltype, **kwargs):

~...python3.8/site-packages/pypureomapi.py in __lookup(self, ltype, **kwargs)
   1216                 response = self.query_server(msg)
   1217                 if response.opcode != OMAPI_OP_UPDATE:
-> 1218                         raise OmapiErrorNotFound()
   1219                 if "raw" in kwargs and kwargs["raw"]:
   1220                         return dict(response.obj)

OmapiErrorNotFound: not found


The connection seems to be correctly established, as I was receiving a different error when it was wrong (the fact that the OMAPI key name is omapi_key was a mysterious fact, as I could not find that on the documentation).

Something is working, because a double add fails on the second one:

In [7]: omapi.add_host("172.21.132.123", "be:ef:00:00:be:ef")

In [8]: omapi.add_host("172.21.132.123", "be:ef:00:00:be:ef")
---------------------------------------------------------------------------
OmapiError                                Traceback (most recent call last)
<ipython-input-8-6cf1f93d7c17> in <module>
----> 1 omapi.add_host("172.21.132.123", "be:ef:00:00:be:ef")

~...python3.8/site-packages/pypureomapi.py in add_host(self, ip, mac)
   1253                 response = self.query_server(msg)
   1254                 if response.opcode != OMAPI_OP_UPDATE:
-> 1255                         raise OmapiError("add failed")
   1256
   1257         def add_host_supersede_name(self, ip, mac, name):  # pylint:disable=E0213

OmapiError: add failed


... which is expected. But that lease does not appear on the web UI, nor can it be retrieved with the pypureomapi library.

This is my first time working with OMAPI, so I may have some misconception on how it works or how it shoulbe be used. I want a mechanism to add/sync static leases from a Python codebase, and OMAPI seemed the path to walk (is there another mechanism? maybe there is an OPNsense API endpoint for managing that? I could not find it. Maybe there is a simpler & easier way to achieve my goal?)
Title: Re: Wanting to use an API for DHCP managing (failing with OMAPI with pypureomapi)
Post by: opns_neuling on April 20, 2021, 08:31:00 am
Hi!
Look the native opnsense api.
The opnsense dhcp implementation limitation, only direct connected interfaces(  subnets) are served.
Cheers
Title: Re: Wanting to use an API for DHCP managing (failing with OMAPI with pypureomapi)
Post by: marius_siuram on November 27, 2021, 09:10:46 pm
@opns_neuling thanks for the pointer, but AFAICT, the native OPNsense API does NOT support DHCP management.

For more information check this GitHub issue:
https://github.com/opnsense/core/issues/4062

It has been timed-out for a year now. Nobody was ready to step in.