#!/bin/bash
# Request reverse-records (PTR) for the entire Internet (-:
# By Joachim Tingvold <joachim@tingvold.com>

A=1
B=0
C=0
D=0

is_interesting_ptr_zone(){
	# Check (with regex) if a PTR-record is interesting
	ptr="`dig -x $A.$B.$C.$D +short`"
	
	if [ ! -z $ptr ]; then
		# $ptr-value is not zero, we have a PTR-record
		
		if echo "$ptr"|grep -qvE "^.*(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\.|\-)){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).*$"; then
			# We're not interested in boring PTRs
			
			print_to_file "$ptr"
		fi
	fi
}

print_to_file(){
	echo "$A.$B.$C.$D: $1" >> $A-0-0-0.txt
}

current_progress(){
	echo "`date`: Processing $A.$B.$C.$D/24..."
}

current_progress

while [ $A -le 223 ]; do
	is_interesting_ptr_zone

	let "D+=1"
	if [ $D -eq 256 ]; then
		D=0
		let "C+=1"
		current_progress
	fi
	if [ $C -eq 256 ]; then
		C=0
		let "B+=1"
	fi
	if [ $B -eq 256 ]; then
		B=0
		let "A+=1"
	fi
	
	## Skip reserved ranges
	# 10.0.0.0/8	Private network	RFC 1918
	# 127.0.0.0/8	Loopback	RFC 5735
	if [ $A -eq 10 ] || [ $A -eq 127 ]; then
		let "A+=1"
	fi
	
	# 169.254.0.0/16	Link-Local	RFC 3927
	if [ $A -eq 169 ] && [ $B -eq 254 ]; then
		let "B+=1"
	fi
	
	# 172.16.0.0/12	Private network	RFC 1918
	if [ $A -eq 172 ] && [ $B -eq 16 ]; then
		B=32
	fi
	
	# 192.0.0.0/24	Reserved (IANA)	RFC 5735
	if [ $A -eq 192 ] && [ $B -eq 0 ] && [ $C -eq 0 ]; then
		let "C+=1"
	fi
	
	# 192.0.2.0/24	TEST-NET-1, Documentation and example code	RFC 5735
	if [ $A -eq 192 ] && [ $B -eq 0 ] && [ $C -eq 2 ]; then
		let "C+=1"
	fi
	
	# 192.88.99.0/24	IPv6 to IPv4 relay	RFC 3068
	if [ $A -eq 192 ] && [ $B -eq 88 ] && [ $C -eq 99 ]; then
		let "C+=1"
	fi
	
	# 192.168.0.0/16	Private network	RFC 1918
	if [ $A -eq 192 ] && [ $B -eq 168 ]; then
		let "B+=1"
	fi
	
	# 198.18.0.0/15	Network benchmark tests	RFC 2544
	if [ $A -eq 192 ] && [ $B -eq 18 ]; then
		B=20
	fi
	
	# 198.51.100.0/24	TEST-NET-2, Documentation and examples	RFC 5737
	if [ $A -eq 198 ] && [ $B -eq 51 ] && [ $C -eq 100 ]; then
		let "C+=1"
	fi
	
	# 203.0.113.0/24	TEST-NET-3, Documentation and examples	RFC 5737
	if [ $A -eq 203 ] && [ $B -eq 0 ] && [ $C -eq 113 ]; then
		let "C+=1"
	fi
done