Class: SubnetCalc
- Inherits:
-
Object
- Object
- SubnetCalc
- Defined in:
- lib/subnet_calc.rb
Instance Attribute Summary collapse
-
#octets ⇒ Object
readonly
Returns the value of attribute octets.
-
#to_h ⇒ Object
readonly
Returns the value of attribute to_h.
Instance Method Summary collapse
-
#initialize(inputs = {}) ⇒ SubnetCalc
constructor
A new instance of SubnetCalc.
- #to_s ⇒ Object
Constructor Details
#initialize(inputs = {}) ⇒ SubnetCalc
Returns a new instance of SubnetCalc.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/subnet_calc.rb', line 27 def initialize(inputs={}) @inputs = inputs default_inputs = {hosts: nil, ip: nil, prefix: nil}.merge(inputs) # Using the input(s) supplied: hosts, prefix = %i(hosts prefix).map {|x| default_inputs[x]} @prefixes = 1.upto(32).each_slice(8).to_a =begin #=> [ [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] ] =end octets = @prefixes.map.with_index do |octet, j| a = octet.map.with_index do |prefix, i| decimal = 2 ** (octet.length - 1 - i ) h = { prefix: prefix, decimal: decimal, hosts_per_subnet: (2 ** 8) ** (@prefixes.length - 1 - j) * decimal } OpenStruct.new h end OpenStruct.new mask: nil, bits: a end @octets = octets # ------------------------------------ octet_n, bit_n, class_type = if hosts then hosts_per_subnet = octets.flat_map.with_index do |octet,i| octet.bits.map.with_index {|y,j| [i, j, y.hosts_per_subnet] } end # find the smallest decimal value to match the # required number of hosts on a network *r, _ = hosts_per_subnet.reverse.detect {|x| x.last > hosts + 1} network = case hosts when 1..254 'c' when 255..65534 'b' else 'a' end (r + [network]) elsif prefix prefixes = @prefixes.flat_map.with_index do |octet,i| octet.map.with_index {|x,j| [i, j, x]} end *r, _ = prefixes.detect {|x| x.last == prefix} network = case prefix when 24..32 'c' when 16..23 'b' else 'a' end (r + [network]) end # Identify the network bits and the host bits classes = ('a'..'d') # identify the initial network bits for a class *a,b or c* class_n = (classes.to_a.index(class_type) + 1) network_bits = class_n.times.map {|x| Array.new(8, 1)} host_bits = (classes.to_a.length - class_n).times.map {|x| Array.new(8, 0)} address_bits = network_bits + host_bits # add the mask to each octet octets.each.with_index do |octet, i| octet.mask = address_bits[i].join.to_i(2) end bit = octets[octet_n].bits[bit_n] magic_number, hosts_per_subnet, prefix = bit.decimal, bit.hosts_per_subnet, bit.prefix # add the new mask to the octet octets[octet_n].mask = octets[octet_n].bits.map(&:decimal)[0..bit_n].inject(:+) subnet_mask = octets.map(&:mask).join('.') subnet_bits = (subnet_mask.split('.')[class_n]).to_i.to_s(2).count("1") no_of_subnets = segments = 2 ** (subnet_bits) subnets = case class_type when 'c' class_subnets(segments, hosts_per_subnet) when 'b' class_b_subnets(256 / segments, bit.decimal) when 'a' class_a_subnets(256 ** 2 / segments, bit.decimal) end default_inputs[:ip] ||= ['10.0.0.0', '172.16.0.0','192.166.0.0'][class_n-1] ip = (default_inputs[:ip] + ' ') .split('.')[0..class_n-1] first_ip = (ip + [subnets.first.first]).join('.') last_ip = (ip + [subnets.first.last]).join('.') result = { class_type: class_type.upcase, magic_number: magic_number, hosts: hosts_per_subnet - 2, subnet_mask: subnet_mask, subnet_bitmask: subnet_mask.split('.').map {|x| "%08d" % x.to_i.to_s(2)}, prefix: prefix, subnets: subnets, range: "%s-%s" % [first_ip, last_ip], subnet_bits: subnet_bits, max_subnets: 2 ** (subnet_bits) } @octet_n = octet_n @h = result end |
Instance Attribute Details
#octets ⇒ Object (readonly)
Returns the value of attribute octets.
25 26 27 |
# File 'lib/subnet_calc.rb', line 25 def octets @octets end |
#to_h ⇒ Object (readonly)
Returns the value of attribute to_h.
25 26 27 |
# File 'lib/subnet_calc.rb', line 25 def to_h @to_h end |
Instance Method Details
#to_s ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/subnet_calc.rb', line 185 def to_s() tfo = TableFormatter.new tfo.source = @h[:subnets].map.with_index do |x,i| ([i+1] + x.to_h.values).map(&:to_s) end tfo.labels = %w(index: Network: 1st: last: broadcast:) full_subnets_table = tfo.display(markdown: true).to_s subnets_table = if full_subnets_table.lines.length > 14 then (full_subnets_table.lines[0..13] + ["\n ... "]).join else full_subnets_table end # octet broken down tfo2 = TableFormatter.new prefixes = @prefixes[@octet_n].map {|x| '/' + x.to_s} tfo2.source = [@h[:subnet_bitmask][@octet_n].chars, prefixes] tfo2.labels = 8.times.map {|n| (2 ** n).to_s + ':' }.reverse octet_table = tfo2.display(markdown: true).to_s <<EOF Subnet calculator ================= Inputs: #{indent Kvx.new(@inputs).to_s} Summary ------- * Network class: #{@h[:class_type]} * magic number: #{@h[:magic_number]} * hosts per subnet: #{@h[:hosts]} * subnet mask: #{@h[:subnet_mask]} * subnet bitmask: #{@h[:subnet_bitmask].join('.')} * prefix bit-length: #{@h[:prefix]} * range: #{@h[:range]} * subnet_bits: #{@h[:subnet_bits]} * maximum subnets: #{@h[:max_subnets]} Breakdown --------- #{(@octet_n + 1).ordinal} octet: #{indent octet_table} ### Subnets #{indent subnets_table} ----------------------------------------------- EOF end |