subnet_calc 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +4 -2
- data/lib/subnet_calc.rb +2 -317
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f58b0336e3cc9963db8a899c1374becd98687b53
|
4
|
+
data.tar.gz: 84c13e31ef5091c48b3301693814390a5996f4f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94e0125b67f02fff4ca4024ea61a10443456d164ae0cb01c166844a081f1b13e9d4fd933b08c99e6e3e08876c58d31902a36669c51e035952b67cb641519b8ee
|
7
|
+
data.tar.gz: 62e9cf6fa08ef0fb457cf2c8bac143a5d0245302f56f941c2c274421e29aa78be2316780d12faa33f35a4332772c12bd8f1a5163e51e9db14a5a1b924702027e
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
@@ -1,2 +1,4 @@
|
|
1
|
-
��
|
2
|
-
|
1
|
+
Ľ%�XD��0�?�=lj�jΖ�4��ܹ��S-�m����G�6=�5�k��`
|
2
|
+
�{�
|
3
|
+
;���eb�U
|
4
|
+
2n妺k]$��P���V@�(r>�*{�iٜQ-��s��+q.C��?1<��ܦ�W��K�hL2���/����Y������jUײVtcںy�U�� 3j�B�A��� }���-Q
|
data/lib/subnet_calc.rb
CHANGED
@@ -137,331 +137,16 @@ class SubnetCalc
|
|
137
137
|
magic_number, hosts_per_subnet, prefix = bit.decimal,
|
138
138
|
bit.hosts_per_subnet, bit.prefix
|
139
139
|
|
140
|
-
no_of_subnets = (2 ** 8.0) / bit.hosts_per_subnet
|
141
|
-
|
142
|
-
segments = (no_of_subnets >= 1 ? no_of_subnets : (1 / no_of_subnets)).to_i
|
143
140
|
n = col
|
144
141
|
|
145
142
|
# add the new mask to the octet
|
146
143
|
|
147
144
|
octets[octet_n].mask = octets[octet_n].bits.map(&:decimal)[0..n].inject(:+)
|
148
145
|
subnet_mask = octets.map(&:mask).join('.')
|
149
|
-
|
150
|
-
subnets = case class_type
|
151
|
-
when 'c'
|
152
|
-
class_subnets(segments, hosts_per_subnet)
|
153
|
-
when 'b'
|
154
|
-
class_b_subnets(256 / segments, bit.decimal)
|
155
|
-
when 'a'
|
156
|
-
class_a_subnets(256 ** 2 / segments, bit.decimal)
|
157
|
-
end
|
158
|
-
|
159
|
-
ip = (default_inputs[:ip] + ' ') .split('.')[0..octet_n-1]
|
160
146
|
|
161
|
-
first_ip = (ip + [subnets.first.first]).join('.')
|
162
|
-
last_ip = (ip + [subnets.first.last]).join('.')
|
163
|
-
|
164
147
|
subnet_bits = (subnet_mask.split('.')[class_n]).to_i.to_s(2).count("1")
|
165
148
|
|
166
|
-
|
167
|
-
class_type: class_type.upcase,
|
168
|
-
magic_number: magic_number,
|
169
|
-
hosts: hosts_per_subnet - 2,
|
170
|
-
subnet_mask: subnet_mask,
|
171
|
-
subnet_bitmask: subnet_mask.split('.').map \
|
172
|
-
{|x| ('0' * 7 + x.to_i.to_s(2))[-8..-1]},
|
173
|
-
prefix: prefix,
|
174
|
-
subnets: subnets,
|
175
|
-
range: "%s-%s" % [first_ip, last_ip],
|
176
|
-
subnet_bits: subnet_bits,
|
177
|
-
max_subnets: 2 ** (subnet_bits)
|
178
|
-
}
|
179
|
-
|
180
|
-
@octet_n = octet_n
|
181
|
-
@h = result
|
182
|
-
end
|
183
|
-
|
184
|
-
|
185
|
-
def to_s()
|
186
|
-
|
187
|
-
tfo = TableFormatter.new
|
188
|
-
|
189
|
-
tfo.source = @h[:subnets].map.with_index do |x,i|
|
190
|
-
([i+1] + x.to_h.values).map(&:to_s)
|
191
|
-
end
|
192
|
-
|
193
|
-
tfo.labels = %w(index Network 1st last broadcast)
|
194
|
-
full_subnets_table = tfo.display(markdown: true).to_s
|
195
|
-
|
196
|
-
subnets_table = if full_subnets_table.lines.length > 14 then
|
197
|
-
(full_subnets_table.lines[0..13] + ["\n ... "]).join
|
198
|
-
else
|
199
|
-
full_subnets_table
|
200
|
-
end
|
201
|
-
|
202
|
-
# octet broken down
|
203
|
-
|
204
|
-
tfo2 = TableFormatter.new
|
205
|
-
|
206
|
-
prefixes = @prefixes[@octet_n].map {|x| '/' + x.to_s}
|
207
|
-
tfo2.source = [@h[:subnet_bitmask][@octet_n].chars, prefixes]
|
208
|
-
tfo2.labels = 8.times.map {|n| (2 ** n).to_s }.reverse
|
209
|
-
octet_table = tfo2.display(markdown: true).to_s
|
210
|
-
|
211
|
-
<<EOF
|
212
|
-
|
213
|
-
|
214
|
-
Subnet calculator
|
215
|
-
=================
|
216
|
-
|
217
|
-
Inputs:
|
218
|
-
|
219
|
-
#{Kvx.new(@inputs).to_s.lines.map {|x| ' ' * 2 + x}.join}
|
220
|
-
|
221
|
-
Summary
|
222
|
-
-------
|
223
|
-
|
224
|
-
* Network class: #{@h[:class_type]}
|
225
|
-
* magic number: #{@h[:magic_number]}
|
226
|
-
* hosts per subnet: #{@h[:hosts]}
|
227
|
-
* subnet mask: #{@h[:subnet_mask]}
|
228
|
-
* subnet bitmask: #{@h[:subnet_bitmask].join('.')}
|
229
|
-
* prefix bit-length: #{@h[:prefix]}
|
230
|
-
* range: #{@h[:range]}
|
231
|
-
|
232
|
-
* subnet_bits: #{@h[:subnet_bits]}
|
233
|
-
* maximum subnets: #{@h[:max_subnets]}
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
Breakdown
|
238
|
-
---------
|
239
|
-
|
240
|
-
#{(@octet_n + 1).ordinal} octet:
|
241
|
-
|
242
|
-
#{indent octet_table}
|
243
|
-
|
244
|
-
### Subnets
|
245
|
-
|
246
|
-
|
247
|
-
#{indent subnets_table}
|
248
|
-
|
249
|
-
-----------------------------------------------
|
250
|
-
|
251
|
-
EOF
|
252
|
-
|
253
|
-
end
|
254
|
-
|
255
|
-
def to_h()
|
256
|
-
@h
|
257
|
-
end
|
258
|
-
|
259
|
-
private
|
260
|
-
|
261
|
-
def class_subnets(n, block_size)
|
262
|
-
|
263
|
-
i = 0
|
264
|
-
|
265
|
-
subnets = n.times.inject([]) do |r,n|
|
266
|
-
|
267
|
-
broadcast = i + block_size - 1
|
268
|
-
first = i + 1
|
269
|
-
last = broadcast - 1
|
270
|
-
|
271
|
-
h = if block_given? then
|
272
|
-
yield(i,first,last,broadcast)
|
273
|
-
else
|
274
|
-
{ network: i, first: first, last: last, broadcast: broadcast }
|
275
|
-
end
|
276
|
-
i += block_size
|
277
|
-
|
278
|
-
r << OpenStruct.new(h)
|
279
|
-
|
280
|
-
end
|
281
|
-
|
282
|
-
end
|
283
|
-
|
284
|
-
def class_b_subnets(n, block_size)
|
285
|
-
|
286
|
-
class_subnets(n, block_size) do |network, first, last, broadcast|
|
287
|
-
|
288
|
-
{
|
289
|
-
network: [network, 0].join('.'),
|
290
|
-
first: [network, 1].join('.'),
|
291
|
-
last: [broadcast, 254].join('.'),
|
292
|
-
broadcast: [broadcast, 255].join('.')
|
293
|
-
}
|
294
|
-
|
295
|
-
end
|
296
|
-
|
297
|
-
end
|
298
|
-
|
299
|
-
def class_a_subnets(n, block_size)
|
300
|
-
|
301
|
-
class_subnets(n, block_size) do |network, first, last, broadcast|
|
302
|
-
|
303
|
-
{
|
304
|
-
network: [network, 0, 0].join('.'),
|
305
|
-
first: [network, 1, 1].join('.'),
|
306
|
-
last: [broadcast, 255, 254].join('.'),
|
307
|
-
broadcast: [broadcast, 255, 255].join('.')
|
308
|
-
}
|
309
|
-
|
310
|
-
end
|
311
|
-
|
312
|
-
end
|
313
|
-
|
314
|
-
def indent(s, i=2)
|
315
|
-
s.lines.map{|x| ' ' * i + x}.join
|
316
|
-
end
|
317
|
-
end#!/usr/bin/env ruby
|
318
|
-
|
319
|
-
# file: subnet_calc.rb
|
320
|
-
|
321
|
-
|
322
|
-
require 'kvx'
|
323
|
-
require 'ostruct'
|
324
|
-
require 'table-formatter'
|
325
|
-
|
326
|
-
|
327
|
-
module Ordinals
|
328
|
-
|
329
|
-
refine Fixnum do
|
330
|
-
def ordinal
|
331
|
-
self.to_s + ( (10...20).include?(self) ? 'th' :
|
332
|
-
%w{ th st nd rd th th th th th th }[self % 10] )
|
333
|
-
end
|
334
|
-
end
|
335
|
-
end
|
336
|
-
|
337
|
-
class SubnetCalc
|
338
|
-
|
339
|
-
using Ordinals
|
340
|
-
|
341
|
-
attr_reader :to_h, :octets
|
342
|
-
|
343
|
-
def initialize(inputs={})
|
344
|
-
|
345
|
-
@inputs = inputs
|
346
|
-
default_inputs = {hosts: nil, ip: '192.168.0.0', prefix: nil}.merge(inputs)
|
347
|
-
|
348
|
-
# Using the input(s) supplied:
|
349
|
-
|
350
|
-
hosts, prefix = %i(hosts prefix).map {|x| default_inputs[x]}
|
351
|
-
|
352
|
-
|
353
|
-
@prefixes = 1.upto(32).each_slice(8).to_a
|
354
|
-
|
355
|
-
=begin
|
356
|
-
#=> [
|
357
|
-
[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16],
|
358
|
-
[17, 18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31, 32]
|
359
|
-
]
|
360
|
-
=end
|
361
|
-
|
362
|
-
|
363
|
-
octets = @prefixes.map.with_index do |octet, j|
|
364
|
-
|
365
|
-
a = octet.map.with_index do |prefix, i|
|
366
|
-
|
367
|
-
decimal = 2 ** (octet.length - 1 - i )
|
368
|
-
|
369
|
-
h = {
|
370
|
-
prefix: prefix,
|
371
|
-
decimal: decimal,
|
372
|
-
hosts_per_subnet: (2 ** 8) ** (@prefixes.length - 1 - j) * decimal
|
373
|
-
}
|
374
|
-
|
375
|
-
OpenStruct.new h
|
376
|
-
|
377
|
-
end
|
378
|
-
|
379
|
-
OpenStruct.new mask: nil, bits: a
|
380
|
-
|
381
|
-
end
|
382
|
-
|
383
|
-
@octets = octets
|
384
|
-
|
385
|
-
|
386
|
-
# ------------------------------------
|
387
|
-
|
388
|
-
|
389
|
-
octet_n, col, class_type = if hosts then
|
390
|
-
|
391
|
-
hosts_per_subnet = octets.flat_map.with_index do |octet,i|
|
392
|
-
octet.bits.map.with_index {|y,j| [i, j, y.hosts_per_subnet] }
|
393
|
-
end
|
394
|
-
|
395
|
-
# find the smallest decimal value to match the
|
396
|
-
# required number of hosts on a network
|
397
|
-
|
398
|
-
*r, _ = hosts_per_subnet.reverse.detect {|x| x.last > hosts + 1}
|
399
|
-
|
400
|
-
network = case hosts
|
401
|
-
when 1..254
|
402
|
-
'c'
|
403
|
-
when 255..65534
|
404
|
-
'b'
|
405
|
-
else
|
406
|
-
'a'
|
407
|
-
end
|
408
|
-
|
409
|
-
(r + [network])
|
410
|
-
|
411
|
-
elsif prefix
|
412
|
-
|
413
|
-
prefixes = @prefixes.flat_map.with_index do |octet,i|
|
414
|
-
octet.map.with_index {|x,j| [i, j, x]}
|
415
|
-
end
|
416
|
-
|
417
|
-
*r, _ = prefixes.detect {|x| x.last == prefix}
|
418
|
-
|
419
|
-
network = case prefix
|
420
|
-
when 24..32
|
421
|
-
'c'
|
422
|
-
when 16..23
|
423
|
-
'b'
|
424
|
-
else
|
425
|
-
'a'
|
426
|
-
end
|
427
|
-
|
428
|
-
(r + [network])
|
429
|
-
end
|
430
|
-
|
431
|
-
|
432
|
-
# Identify the network bits and the host bits
|
433
|
-
|
434
|
-
classes = ('a'..'d')
|
435
|
-
|
436
|
-
# identify the initial network bits for a class *a,b or c*
|
437
|
-
|
438
|
-
class_n = (classes.to_a.index(class_type) + 1)
|
439
|
-
|
440
|
-
network_bits = class_n.times.map {|x| Array.new(8, 1)}
|
441
|
-
host_bits = (classes.to_a.length - class_n).times.map {|x| Array.new(8, 0)}
|
442
|
-
address_bits = network_bits + host_bits
|
443
|
-
|
444
|
-
# add the mask to each octet
|
445
|
-
|
446
|
-
octets.each.with_index do |octet, i|
|
447
|
-
octet.mask = address_bits[i].join.to_i(2)
|
448
|
-
end
|
449
|
-
|
450
|
-
|
451
|
-
bit = octets[octet_n].bits[col]
|
452
|
-
|
453
|
-
magic_number, hosts_per_subnet, prefix = bit.decimal,
|
454
|
-
bit.hosts_per_subnet, bit.prefix
|
455
|
-
|
456
|
-
no_of_subnets = (2 ** 8.0) / bit.hosts_per_subnet
|
457
|
-
|
458
|
-
segments = (no_of_subnets >= 1 ? no_of_subnets : (1 / no_of_subnets)).to_i
|
459
|
-
n = col
|
460
|
-
|
461
|
-
# add the new mask to the octet
|
462
|
-
|
463
|
-
octets[octet_n].mask = octets[octet_n].bits.map(&:decimal)[0..n].inject(:+)
|
464
|
-
subnet_mask = octets.map(&:mask).join('.')
|
149
|
+
no_of_subnets = segments = 2 ** (subnet_bits)
|
465
150
|
|
466
151
|
subnets = case class_type
|
467
152
|
when 'c'
|
@@ -477,7 +162,7 @@ class SubnetCalc
|
|
477
162
|
first_ip = (ip + [subnets.first.first]).join('.')
|
478
163
|
last_ip = (ip + [subnets.first.last]).join('.')
|
479
164
|
|
480
|
-
|
165
|
+
|
481
166
|
|
482
167
|
result = {
|
483
168
|
class_type: class_type.upcase,
|
metadata
CHANGED
metadata.gz.sig
CHANGED
Binary file
|