eulaurarien/feed_dns.py

60 lines
1.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import database
import argparse
import sys
import logging
if __name__ == '__main__':
# Parsing arguments
log = logging.getLogger('feed_dns')
parser = argparse.ArgumentParser(
description="TODO")
parser.add_argument(
# '-i', '--input', type=argparse.FileType('rb'), default=sys.stdin.buffer,
'-i', '--input', type=argparse.FileType('r'), default=sys.stdin,
help="TODO")
args = parser.parse_args()
2019-12-13 12:35:05 +01:00
DB = database.Database(write=True)
try:
DB.enter_step('iowait')
# line: bytes
line: str
for line in args.input:
DB.enter_step('feed_json_parse')
# split = line.split(b'"')
split = line.split('"')
try:
updated = int(split[3])
name = split[7]
dtype = split[11]
value = split[15]
except IndexError:
log.error("Invalid JSON: %s", line)
continue
# DB.enter_step('feed_json_assert')
# data = json.loads(line)
# assert dtype == data['type']
# assert name == data['name']
# assert value == data['value']
DB.enter_step('feed_switch')
if dtype == 'a':
for rule in DB.get_ip4(value):
DB.set_hostname(name, source=rule, updated=updated)
elif dtype == 'cname':
for rule in DB.get_domain(value):
DB.set_hostname(name, source=rule, updated=updated)
elif dtype == 'ptr':
for rule in DB.get_domain(value):
DB.set_ip4address(name, source=rule, updated=updated)
DB.enter_step('iowait')
except KeyboardInterrupt:
log.warning("Interupted.")
pass
DB.close()