summaryrefslogtreecommitdiff
path: root/genrdns
diff options
context:
space:
mode:
Diffstat (limited to 'genrdns')
-rwxr-xr-xgenrdns48
1 files changed, 48 insertions, 0 deletions
diff --git a/genrdns b/genrdns
new file mode 100755
index 0000000..14d0be6
--- /dev/null
+++ b/genrdns
@@ -0,0 +1,48 @@
+#!/usr/bin/env ruby
+
+def ip2num(ip)
+ ipa = ip.split(".").map(&:to_i)
+ return (ipa[0] * (256 ** 3)) + (ipa[1] * (256 ** 2)) + (ipa[2] * 256) + ipa[3]
+end
+
+def num2ip(ipi)
+ outa = []
+ outa[3] = ipi % 256
+ outa[2] = (ipi / 256) % 256
+ outa[1] = (ipi / (256 ** 2)) % 256
+ outa[0] = (ipi / (256 ** 3)) % 256
+ return outa.map(&:to_s).join(".")
+end
+
+def boundarycheck(ip, cidr)
+ bound = 2**(32-cidr)
+ return (ip2num(ip) % bound) == 0
+end
+
+def addip(ip, x)
+ ipi = ip2num(ip)
+ ipi += x
+ return num2ip(ipi)
+end
+
+def reverseip(ip)
+ return ip.split('.').reverse.join('.')
+end
+
+if ARGV.length == 0 then
+ puts "Usage: genrdns ip[/cidr]"
+ exit
+end
+
+IPCIDR = ARGV.shift.split("/")
+IP = IPCIDR[0]
+CIDR = IPCIDR[1].nil? ? 32 : IPCIDR[1].to_i
+
+unless boundarycheck(IP, CIDR) then
+ puts "Invalid CIDR Boundary"
+ exit 1
+end
+
+(2**(32-CIDR)).times do |x|
+ puts "#{reverseip(addip(IP, x))}.in-addr.arpa"
+end