#!/bin/bash

# DNS helper
#
# enumerate all IP addresses configured on this system
# in a format suitable for inclusion in a BIND9 DNS zone

#CHANGELOG
#0.50	initial
#0.51	ignore "@main" component in interface name
#0.52	add missing root domain dots in PTR output
#0.53	RFC4255 SSHFP DNS records
#0.54	restyle according to https://kb.clearcable.ca/KB/ProgrammingStyleStandards

TMP=$(mktemp)

LINKs=$(ip -oneline link show | awk '{sub(":","",$2);sub("@.+$","",$2);print $2}')

for DEV in $LINKs; do
	IPV4_As=$(
		ip -oneline -family inet address show dev $DEV|
		awk '{if($4!~/^127[.]/){gsub("/[0-9]+","",$4);print $4}}')

	IPV6_As=$(
		ip -oneline -family inet6 address show dev $DEV|
		awk '{if($4!~/^(fe80)?::/){gsub("/[0-9]+","",$4);print $4}}')

	for IPV4_A in $IPV4_As; do
		echo "$DEV.$HOSTNAME.	A	$IPV4_A"

		#$ host 24.215.0.24
		#24.0.215.24.in-addr.arpa is an alias for 24.0.215.24.in-addr.arpa.rafal.ca.
		#24.0.215.24.in-addr.arpa.rafal.ca domain name pointer mirror.mountaincable.net.
		#$ host 24.215.0.244
		#Host 244.0.215.24.in-addr.arpa. not found: 3(NXDOMAIN)
		if host $IPV4_A > $TMP; then
			REV4=$(awk '{if (NR==1) {printf "%s.",$1}}' $TMP)
		else
			REV4=$(awk '{if (NR==1) {printf "%s",$2}}' $TMP)
		fi
		echo "$REV4	PTR	$DEV.$HOSTNAME."
	done

	for IPV6_A in $IPV6_As; do
		echo "$DEV.$HOSTNAME.	AAAA	$IPV6_A"

		#$ host 2607:f2c0:f00e:c901:20d:b9ff:fe19:a6f4
		#4.f.6.a.9.1.e.f.f.f.9.b.d.0.2.0.1.0.9.c.e.0.0.f.0.c.2.f.7.0.6.2.ip6.arpa domain name pointer alix2.
		#$ host 2607:f2c0:f00e:c901:20d:b9ff:fe19:a6f5
		#5.f.6.a.9.1.e.f.f.f.9.b.d.0.2.0.1.0.9.c.e.0.0.f.0.c.2.f.7.0.6.2.ip6.arpa domain name pointer node-77b0sibj81rvaa4d52t.ipv6.teksavvy.com.
		#$ host 1607:f2c0:f00e:c901:20d:b9ff:fe19:a6f4
		#Host 4.f.6.a.9.1.e.f.f.f.9.b.d.0.2.0.1.0.9.c.e.0.0.f.0.c.2.f.7.0.6.1.ip6.arpa not found: 3(NXDOMAIN)
		if host $IPV6_A > $TMP; then
			REV6=$(awk '{if (NR==1) {printf "%s.",$1}}' $TMP)
		else
			REV6=$(awk '{if (NR==1) {printf "%s.",$2}}' $TMP)
		fi
		echo "$REV6	PTR	$DEV.$HOSTNAME."
	done
done
ssh-keygen -r $HOSTNAME.

rm $TMP

# vim: cindent:shiftwidth=4:tabstop=4:smarttab:textwidth=100
