From a1af0c7f6c68540cd69f8d81852988b28ce216c5 Mon Sep 17 00:00:00 2001 From: jon r Date: Sun, 28 Mar 2021 00:29:23 +0100 Subject: [PATCH 1/2] add README, gitignore and env example --- .env.example | 4 ++++ .gitignore | 5 +++++ README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 README.md diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..333b42d --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +MODOBOA_API_BASE= +PDNS_API_BASE= +MODOBOA_TOKEN= +PDNS_TOKEN= \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a4393b --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.env +.vscode +.tmp +venv + diff --git a/README.md b/README.md new file mode 100644 index 0000000..26d28f3 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# DNS IP changer + +## Preparations + +Explain how to set + +``` +MODOBOA_API_BASE= +PDNS_API_BASE= +``` + +``` +MODOBOA_TOKEN= +PDNS_TOKEN= +``` + +in `.env` from copying `.env.example`, and why. + +Eventually create a venv, pyenv, pipenv, ... + +Install requirements + +``` +pip3 install -U pip +pip3 install -U requests python-dotenv +``` + +## Usage + +This tool helps to migrate DNS A records pointing at a certain IP to another one. + +``` +Usage: +./pdns-api.py from-ip to-ip +``` + +## Related + +This work lives on in [modoboa-pdns-bridge.py · ecobytes / lab / mx](https://lab.allmende.io/ecobytes/lab/mx/-/blob/master/modoboa-pdns-bridge.py) + +## Authors + +- Johannes Winter + +## License + +© Ecobytes e.V. -- GitLab From 7e2deb9da0e46b7d39bdfd88774da2216afd268e Mon Sep 17 00:00:00 2001 From: jon r Date: Sun, 28 Mar 2021 00:30:26 +0100 Subject: [PATCH 2/2] Python 3 port Basically switching from json.dumps to pp.pprint. Incidentally the initial naming already anticipates the new Python 3 method well. --- pdns-api.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) mode change 100644 => 100755 pdns-api.py diff --git a/pdns-api.py b/pdns-api.py old mode 100644 new mode 100755 index 5dd2001..3eed750 --- a/pdns-api.py +++ b/pdns-api.py @@ -1,11 +1,13 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ## imports -import json import requests import os, sys +import pprint ## loaders +pp = pprint.PrettyPrinter(sort_dicts=True, indent=4) + from dotenv import load_dotenv env_path = os.path.abspath ( os.path.join ( os.curdir, os.path.relpath('.env') ) ) @@ -41,14 +43,11 @@ def set_record(zone, name, type, record): ] } print ('payload') - pprint(payload) + pp.pprint(payload) p = requests.patch(PDNS_API_BASE + 'zones/' + zone, headers={'X-API-KEY': PDNS_TOKEN}, json=payload) print (p.text) -def pprint(text): - print json.dumps(text, sort_keys=True, indent=4, separators=(',', ': ')) - def change_ip(record,ip): record['content']=ip set_record(record['zone'],record['name'],record['type'], record) @@ -60,7 +59,7 @@ def change_records(a, b): if len(sys.argv) > 1: - [pprint(result) for result in search_pdns(sys.argv[1])] + [pp.pprint(result) for result in search_pdns(sys.argv[1])] if len(sys.argv) > 2: change_records(sys.argv[1], sys.argv[2]) else: -- GitLab