Skip to content

Instantly share code, notes, and snippets.

@gretel
Created Oct 17, 2020
Embed
What would you like to do?
nanoservice "api" which listens to "dyndns" requests from a router to update records on route53 (via shellscript) to be used in conjunction with https://gist.github.com/gretel/8bb0c2936ae5cda12ae5f0bf381dd3f0
#!/usr/bin/env python3
#
from flask import Flask, request
#from waitress import serve
from pid.decorator import pidfile
import ipaddress
import setproctitle
import subprocess
PIDDIR='.'
PORT=5001
SCRIPT='./upd_dyndns.sh'
TIMEOUT=60
app = Flask(__name__)
@app.route('/nonstatic', methods=['GET', 'POST'])
def nonstatic():
try:
arg_v4 = request.args.get('ipaddr', '')
arg_v6 = request.args.get('ip6addr', '')
except flask.KeyError:
return 'nokey'
try:
ip_v4 = ipaddress.ip_address(arg_v4)
ip_v6 = ipaddress.ip_address(arg_v6)
except ValueError:
return 'invalid'
try:
assert ip_v4.is_global
except AssertionError:
return 'invalid'
finally:
app.logger.debug('ip_v4: %s' % ip_v4)
app.logger.debug('ip_v6: %s' % ip_v6)
cmdline = "%s %s %s" % ( SCRIPT, str(ip_v4), str(ip_v6) )
app.logger.debug('cmdline: %s' % cmdline)
try:
result = subprocess.run([cmdline], shell=True, check=True, close_fds=True, timeout=TIMEOUT, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as cpe:
print(cpe)
if cpe.returncode > 1:
return 'error'
except subprocess.TimeoutExpired as te:
print('subprocess timeout (%s' % te.timeout)
return 'timeout'
finally:
print(result.stdout.decode("utf-8", errors="ignore"))
if result.returncode == 1:
return 'changed'
return 'nochange'
@pidfile('nonstatic.pid', piddir=PIDDIR)
def main():
setproctitle.setproctitle('nonstatic')
app.run(host='127.0.0.1', port=PORT, debug=False) # flask
#serve(app, host='127.0.0.1', port=PORT) # waitress
if __name__ == '__main__':
main()
#!/bin/sh
#
echo "$@"
# virtualenv
. bin/activate
# update records
./course53.py --domain yhamster.us --subdomain vidsrv --zone DKS03KALD120SK --ipaddress "${2}" --record_type AAAA
./course53.py --domain yhamster.us --subdomain vidsrv --zone DKS03KALD120SK --ipaddress "${1}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment