subnet_calc 0.2.0 → 0.2.1
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 +3 -3
- data.tar.gz.sig +0 -0
- data/lib/subnet_calc.rb +34 -17
- 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: 7f08208310333a341c9f31757534e9e40986c827
|
4
|
+
data.tar.gz: c7a36481efddf08a866a1ed2697f49f0044dbfd4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71f6d30b105979461de671031e9ded95c93f98db2808571d3f63462908a3c7194502a75d73c5e1769b9ad20dfe08c667401665df464176158573991314194159
|
7
|
+
data.tar.gz: a0bce9a42f943f9e654a25abb4aaad74c1bafc5955dfed872218f477aeea9508eb71e6bd5100ce105376e317035efc2de149439626177212f5956a86ec95cbeb
|
checksums.yaml.gz.sig
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
c�l�6���/��ں�
|
2
|
+
��R?-�2R�AE�-/U�>��1�r�ܵ�%�S��ϵ�W�J�t�i�N������A�(��V�S��3tvg�ڕe���[3*$�c:�������W2�p��e?�ټ{|ha������w(��@���[�.�:'HV�
|
3
|
+
"�F<�m���k��pTK�e�'��G��2��֎
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/subnet_calc.rb
CHANGED
@@ -24,14 +24,14 @@ class SubnetCalc
|
|
24
24
|
|
25
25
|
attr_reader :to_h
|
26
26
|
|
27
|
-
def initialize(inputs={
|
27
|
+
def initialize(inputs={})
|
28
28
|
|
29
29
|
@inputs = inputs
|
30
|
-
default_inputs = {hosts:
|
30
|
+
default_inputs = {hosts: nil, ip: '192.168.0.0', prefix: nil}.merge(inputs)
|
31
31
|
|
32
32
|
# Using the input(s) supplied:
|
33
33
|
|
34
|
-
hosts = default_inputs[
|
34
|
+
hosts, prefix = %i(hosts prefix).map {|x| default_inputs[x]}
|
35
35
|
|
36
36
|
|
37
37
|
@prefixes = 1.upto(32).each_slice(8).to_a
|
@@ -64,14 +64,33 @@ class SubnetCalc
|
|
64
64
|
|
65
65
|
end
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
|
67
|
+
|
68
|
+
# ------------------------------------
|
69
|
+
|
70
|
+
|
71
|
+
octet_n, col = if hosts then
|
72
|
+
|
73
|
+
hosts_per_subnet = octets.flat_map.with_index do |octet,i|
|
74
|
+
octet.bits.map.with_index {|y,j| [i, j, y.hosts_per_subnet] }
|
75
|
+
end
|
76
|
+
|
77
|
+
# find the smallest decimal value to match the
|
78
|
+
# required number of hosts on a network
|
79
|
+
|
80
|
+
hosts_per_subnet.reverse.detect {|x| x.last > hosts + 1}
|
70
81
|
|
82
|
+
elsif prefix
|
71
83
|
|
72
|
-
|
84
|
+
prefixes = @prefixes.flat_map.with_index do |octet,i|
|
85
|
+
octet.map.with_index {|x,j| [i, j, x]}
|
86
|
+
end
|
87
|
+
|
88
|
+
prefixes.detect {|x| x.last == prefix}
|
89
|
+
|
90
|
+
end
|
73
91
|
|
74
|
-
|
92
|
+
# determine what class of network we are using
|
93
|
+
class_type = (hosts and hosts <= 254 or prefix >= 24) ? 'c' : 'b'
|
75
94
|
|
76
95
|
# Identify the network bits and the host bits
|
77
96
|
|
@@ -80,6 +99,7 @@ class SubnetCalc
|
|
80
99
|
# identify the initial network bits for a class *a,b or c*
|
81
100
|
|
82
101
|
class_n = (classes.to_a.index(class_type) + 1)
|
102
|
+
|
83
103
|
network_bits = class_n.times.map {|x| Array.new(8, 1)}
|
84
104
|
host_bits = (classes.to_a.length - class_n).times.map {|x| Array.new(8, 0)}
|
85
105
|
address_bits = network_bits + host_bits
|
@@ -90,14 +110,7 @@ class SubnetCalc
|
|
90
110
|
octet.mask = address_bits[i].join.to_i(2)
|
91
111
|
end
|
92
112
|
|
93
|
-
# ------------------------------------
|
94
|
-
|
95
|
-
|
96
113
|
|
97
|
-
# find the smallest decimal value to match the
|
98
|
-
# required number of hosts on a network
|
99
|
-
|
100
|
-
octet_n, col = hosts_per_subnet.reverse.detect {|x| x.last > hosts + 1}
|
101
114
|
|
102
115
|
bit = octets[octet_n].bits[col]
|
103
116
|
|
@@ -105,7 +118,8 @@ class SubnetCalc
|
|
105
118
|
|
106
119
|
no_of_subnets = (2 ** 8.0) / bit.hosts_per_subnet
|
107
120
|
|
108
|
-
segments = (no_of_subnets >= 1 ? no_of_subnets :
|
121
|
+
segments = (no_of_subnets >= 1 ? no_of_subnets :
|
122
|
+
256 / (256 * no_of_subnets)).to_i
|
109
123
|
n = col
|
110
124
|
|
111
125
|
# add the new mask to the octet
|
@@ -148,7 +162,10 @@ class SubnetCalc
|
|
148
162
|
|
149
163
|
tfo = TableFormatter.new
|
150
164
|
|
151
|
-
tfo.source = @h[:subnets].map.with_index
|
165
|
+
tfo.source = @h[:subnets].map.with_index do |x,i|
|
166
|
+
([i+1] + x.to_h.values).map(&:to_s)
|
167
|
+
end
|
168
|
+
|
152
169
|
tfo.labels = %w(index Network 1st last broadcast)
|
153
170
|
full_subnets_table = tfo.display(markdown: true).to_s
|
154
171
|
|
metadata
CHANGED
metadata.gz.sig
CHANGED
Binary file
|