mietzen / porkbun-ddns

porkbun-ddns is an unoffical DDNS-Client for Porkbun Domains.
MIT License
119 stars 9 forks source link

How do you update only sub-domains instead of the root domain in Python? #65

Closed Azuriye closed 2 hours ago

Azuriye commented 5 hours ago

So far this package works fine for updating my A/AAAA records for my root domain, but how do I use it only for updating certain sub-domain A/AAAA records?

mietzen commented 4 hours ago

Can you give an example how you use porkbun-ddns?

Do you use docker? Docker compose? Or the CLI Client?

Azuriye commented 3 hours ago

Currently I am using porkbun-ddns in python with cron.

from requests import get
from socket import getaddrinfo, AF_INET, AF_INET6
from porkbun_ddns import PorkbunDDNS
from porkbun_ddns.config import Config, DEFAULT_ENDPOINT

API_KEY = ''
SECRET_API_KEY = ''

query4_address = getaddrinfo('domain.com', None, AF_INET)[0][4][0]
query6_address = getaddrinfo('domain.com', None, AF_INET6)[0][4][0]

my_ipv4 = get('https://api.ipify.org').text
my_ipv6 = get('https://api6.ipify.org').text

if (query4_address != my_ipv4) or (query6_address != my_ipv6):
    config = Config(DEFAULT_ENDPOINT, API_KEY, SECRET_API_KEY)
    porkbun_ddns_ip = PorkbunDDNS(config, 'domain', public_ips=[my_ipv4, my_ipv6])

    porkbun_ddns_ip.update_records()
else:
    print("DNS records are up to date.")
mietzen commented 2 hours ago

Currently I am using porkbun-ddns in python with cron.

from requests import get
from socket import getaddrinfo, AF_INET, AF_INET6
from porkbun_ddns import PorkbunDDNS
from porkbun_ddns.config import Config, DEFAULT_ENDPOINT

API_KEY = ''
SECRET_API_KEY = ''

query4_address = getaddrinfo('domain.com', None, AF_INET)[0][4][0]
query6_address = getaddrinfo('domain.com', None, AF_INET6)[0][4][0]

my_ipv4 = get('https://api.ipify.org').text
my_ipv6 = get('https://api6.ipify.org').text

if (query4_address != my_ipv4) or (query6_address != my_ipv6):
    config = Config(DEFAULT_ENDPOINT, API_KEY, SECRET_API_KEY)
    porkbun_ddns_ip = PorkbunDDNS(config, 'domain', public_ips=[my_ipv4, my_ipv6])

    porkbun_ddns_ip.update_records()
else:
    print("DNS records are up to date.")

To set subdomains you can do the following:

subdomains = ['subdomain1', 'subdomain2', 'subdomain3']

for subdomain in subdomains:
    porkbun_ddns.set_subdomain(subdomain)
    porkbun_ddns.update_records()

If you want to set the root domain in the same loop use:

subdomains = ['@', 'subdomain1', 'subdomain2', 'subdomain3']

for subdomain in subdomains:
    porkbun_ddns.set_subdomain(subdomain)
    porkbun_ddns.update_records()

Also have a look at the entry point of the docker file:

https://github.com/mietzen/porkbun-ddns/blob/616e16dae84a4ffe3f4aadbcdfd08254e2f1ba9c/Docker/entrypoint.py#L46

I close the issue but feel free to leave another comment if you got further questions.