bundlewrap/bundles/pppd/files/dyndns_periodic

51 lines
1.5 KiB
Python

#!/usr/bin/env python3
import logging
from ipaddress import ip_address
from json import loads
from subprocess import check_output, run
DOMAIN = '${domain}'
# <%text>
logging.basicConfig(level=logging.INFO)
LOG = logging.getLogger('DynDNS checker')
try:
iproute = loads(check_output(['ip', '-json', 'address', 'show', 'scope', 'global']))
resolved_ipv4 = check_output(['dig', '+short', DOMAIN, 'A']).decode().strip()
resolved_ipv6 = check_output(['dig', '+short', DOMAIN, 'AAAA']).decode().strip()
LOG.info(f'resolved ipv4 is "{resolved_ipv4}"')
LOG.info(f'resolved ipv6 is "{resolved_ipv6}"')
needs_changing = False
for iface in iproute:
if not iface['ifname'].startswith('ppp'):
LOG.debug(f'ignoring {iface["ifname"]}')
continue
LOG.info(f'working on {iface["ifname"]}')
for ip in iface['addr_info']:
try:
addr = ip_address(ip['local'])
LOG.info(f'{iface["ifname"]} has ip {addr.compressed}')
if (
(addr.version == 4 and addr.compressed != resolved_ipv4)
or (addr.version == 6 and addr.compressed != resolved_ipv6)
):
needs_changing = True
except Exception:
continue
if needs_changing:
LOG.warning('addresses have changed, calling update script!')
run(['/etc/ppp/ip-up.d/dyndns'])
else:
LOG.info('everything is fine')
except Exception as e:
logging.exception(e)
# </%text>