initial commit

This commit is contained in:
mortzu 2014-09-19 19:43:11 +02:00
commit fa1302675d
3 changed files with 173 additions and 0 deletions

65
rzonegen.py Executable file
View File

@ -0,0 +1,65 @@
#! /usr/bin/env python
import sys
import json
import codecs
import re
from datetime import datetime
def str_to_domainlabel(s):
label = re.sub("[^0-9a-zA-Z-]", "-", s)
label = re.sub("-+", "-", label)
label = re.sub("^-*", "", label)
label = re.sub("-*$", "", label)
if not re.match("^[a-zA-Z][a-zA-Z0-9-]*[a-zA-Z0-9]$", label):
raise RuntimeError("Not convertable to a domain label: %s" % s)
return label
def ipv6_addr_to_rdns(addr):
rdns = ""
counter = 4
for char in reversed(addr):
if char == ':':
rdns += counter * '0.'
counter = 4
else:
rdns += char + '.'
counter -= 1
rdns += 'ip6.arpa.'
return rdns
data = json.load(sys.stdin)
domain = sys.argv[1]
if not domain.startswith("."):
domain = "." + domain
if not domain.endswith("."):
domain = domain + "."
print("""$TTL 1h
@ IN SOA ns.ffhb. liste.bremen.freifunk.net. (
%s ; serial
1h ; refresh
30m ; retry
2d ; expiration
1h ; caching
)
@ NS ns01.ffhb.
""" % datetime.now().strftime("%Y%m%d%H%M"))
for node in data.values():
try:
for address in node['network']['addresses']:
if address.startswith("fe80:"):
continue
rdns = ipv6_addr_to_rdns(address)
if rdns.endswith(domain):
print "%s PTR %s.nodes.ffhb." % (ipv6_addr_to_rdns(address)[0:-len(domain)], str_to_domainlabel(node['hostname']))
except:
pass

65
update-dns.sh Executable file
View File

@ -0,0 +1,65 @@
#! /usr/bin/env bash
# getting workingdir of scripts
WORK_DIR="$(dirname $(readlink -nf $0))"
# set safe path
PATH="${WORK_DIR}:/sbin:/usr/sbin:/bin:/usr/bin"
# define variable to count loops
declare -i NUM=0
# tmp file
TMP_FILE="$(mktemp)"
# if creation of tmp file failed
# exit
if [ -z "$TMP_FILE" ]; then
exit 1
fi
# names of zones
ZONEFILE=/var/cache/bind/ffhb.nodes.zone
RZONEFILE=/var/cache/bind/arpa.ip6.f.d.2.f.5.1.1.9.0.f.2.c.zone
# loop until data received
while true; do
# increment counter
NUM=$(($NUM+1))
# get data from alfred
alfred-json -z -r 158 > "$TMP_FILE" 2>/dev/null
# on success leave loop
if [ $? -eq 0 ]; then
break
fi
# if the 120th run has reached kill script
if [ $NUM -gt 240 ]; then
# remove tmp file
rm -f "$TMP_FILE"
# exit with error code
exit 1
fi
# sleep to be safe CPU load don't getting higher
sleep 1
done
# generate forward zone
if zonegen.py < "$TMP_FILE" > "${ZONEFILE}.new"; then
mv "${ZONEFILE}.new" "${ZONEFILE}"
fi
# generate reverse zone
if rzonegen.py 0.0.0.0.c.2.f.0.9.1.1.5.f.2.d.f.ip6.arpa < "$TMP_FILE" > "${RZONEFILE}.new"; then
mv "${RZONEFILE}.new" "${RZONEFILE}"
fi
# reload nameserver
rndc reload >/dev/null
# remove tmp file
rm -f "$TMP_FILE"

43
zonegen.py Executable file
View File

@ -0,0 +1,43 @@
#! /usr/bin/env python
import sys
import json
import codecs
import re
from datetime import datetime
def str_to_domainlabel(s):
label = re.sub("[^0-9a-zA-Z-]", "-", s)
label = re.sub("-+", "-", label)
label = re.sub("^-*", "", label)
label = re.sub("-*$", "", label)
if not re.match("^[a-zA-Z][a-zA-Z0-9-]*[a-zA-Z0-9]$", label):
raise RuntimeError("Not convertable to a domain label: %s" % s)
return label
data = json.load(sys.stdin)
print """$TTL 1h
@ IN SOA ns.ffhb. liste.bremen.freifunk.net. (
%s ; serial
1h ; refresh
30m ; retry
2d ; expiration
1h ; caching
)
NS ns.ffhb.
""" % datetime.now().strftime("%Y%m%d%H%M")
for node in data.values():
try:
for address in node['network']['addresses']:
if address.startswith("fe80:"):
continue
print "%-15s AAAA %s" % (str_to_domainlabel(node['hostname']), address)
except:
pass