summaryrefslogtreecommitdiff
path: root/geneni
blob: b1175d6953c4e6eb0fd2536b75074bf23664a1fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/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

if ARGV.length == 0 then
  puts "Usage: geneni ip[/cidr] iface [alias start]"
  exit
end

IPCIDR = ARGV.shift.split("/")
IP = IPCIDR[0]
CIDR = IPCIDR[1].nil? ? 32 : IPCIDR[1].to_i
IFACE = ARGV.shift
START = (ARGV.shift or "1").to_i

unless boundarycheck(IP, CIDR) then
  puts "Invalid CIDR Boundary"
  exit 1
end

(2**(32-CIDR)).times do |x|
  puts "auto #{IFACE}:#{x+START}"
  puts "iface #{IFACE}:#{x+START} inet static"
  puts "\taddress #{addip(IP, x)}"
  puts "\tnetmask #{num2ip(("1"*CIDR + "0"*(32-CIDR)).to_i(2))}"
  #puts "\tnetmask 255.255.255.255"
end