Python scripting UCS

Hi,

I want to automate DNS updates using a Python script. What’s the best way to procede? By re-using existing Univention Python modules below /usr/lib/python3/dist-packages/univention/admin or by exec’ing the command line tools and parsing the output? And if the former: how? The latter seems a little primitive to me.

At the moment, I get the necessary data as a JSON but I could also provide it as a CSV table.

Bests,
Masin

Hello Masin,

using the interface of univention.admin is very error prone. Instead use the univention.udm package: https://docs.software-univention.de/ucs-python-api/univention.udm.html

>>> from univention.udm import UDM
>>> dns_forward_mod = UDM.admin().version(1).get("dns/forward_zone")
>>> for obj in dns_forward_mod.search():
...   print(obj)
... 
GenericObject('dns/forward_zone', 'zoneName=uni.dtr,cn=dns,dc=uni,dc=dtr')

>>> dns_host_mod = UDM.admin().version(1).get("dns/host_record")
>>> for obj in dns_host_mod.search("relativeDomainName=*7*"):
...   print(obj)
... 
GenericObject('dns/host_record', 'relativeDomainName=m70,zoneName=uni.dtr,cn=dns,dc=uni,dc=dtr')
GenericObject('dns/host_record', 'relativeDomainName=b71,zoneName=uni.dtr,cn=dns,dc=uni,dc=dtr')

Object values are what you see on the cmdline:

obj.dn: 'relativeDomainName=b71,zoneName=uni.dtr,cn=dns,dc=uni,dc=dtr'
obj.options: ['default']
obj.policies: []
obj.position: 'zoneName=uni.dtr,cn=dns,dc=uni,dc=dtr'
obj.props.a: ['10.200.3.71']
obj.props.mx: []
obj.props.name: 'b71'
obj.props.txt: []
obj.props.zonettl: None
obj.superordinate: 'zoneName=uni.dtr,cn=dns,dc=uni,dc=dtr'

After changing a property run obj.save() to write the changes to LDAP.

Greetings
Daniel Tröder

1 Like

That looks very promising! Thank you very much!

Mastodon