subnet_calc 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/subnet_calc.rb +36 -15
- data.tar.gz.sig +1 -1
- 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: 6765bee68b5f48ecbac6fdbb859652cd6146b9d7
|
4
|
+
data.tar.gz: '049853d600678fc06a67c0035f98ff8883d80891'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9ee059a44c6de04445fcb49e64568649995b3d4d4dde08e004269423d69acda1539bf7d7085868bc75969e73f9b4bba86f9f6dda0f17d82ba6bcd84125dde41
|
7
|
+
data.tar.gz: f50ca74be7b2637c3244e264e45b0067f575748b57961388ddd79fea802c88d026838d742629366511fff444d5389506227de9bf9619911421725661041ca222
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/lib/subnet_calc.rb
CHANGED
@@ -30,22 +30,39 @@ class SubnetCalc
|
|
30
30
|
default_inputs = {hosts: 254, ip: '192.168.0.0'}.merge(inputs)
|
31
31
|
|
32
32
|
@prefixes = 1.upto(32).each_slice(8).to_a
|
33
|
+
|
33
34
|
=begin
|
34
35
|
#=> [
|
35
36
|
[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16],
|
36
37
|
[17, 18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31, 32]
|
37
38
|
]
|
38
39
|
=end
|
40
|
+
|
39
41
|
|
40
|
-
octets = @prefixes.map do |octet|
|
42
|
+
octets = @prefixes.map.with_index do |octet, j|
|
41
43
|
|
42
44
|
a = octet.map.with_index do |prefix, i|
|
43
|
-
|
45
|
+
|
46
|
+
decimal = 2 ** (octet.length - 1 - i )
|
47
|
+
|
48
|
+
h = {
|
49
|
+
prefix: prefix,
|
50
|
+
decimal: decimal,
|
51
|
+
hosts_per_subnet: (2 ** 8) ** (@prefixes.length - 1 - j) * decimal
|
52
|
+
}
|
53
|
+
|
54
|
+
OpenStruct.new h
|
55
|
+
|
44
56
|
end
|
45
57
|
|
46
58
|
OpenStruct.new mask: nil, bits: a
|
47
59
|
|
48
60
|
end
|
61
|
+
|
62
|
+
hosts_per_subnet = octets.map.with_index do |x,i|
|
63
|
+
x.bits.map.with_index {|y,j| [i, j, y.hosts_per_subnet] }
|
64
|
+
end.flatten(1)
|
65
|
+
|
49
66
|
|
50
67
|
# determine what class of network we are using
|
51
68
|
|
@@ -74,19 +91,24 @@ class SubnetCalc
|
|
74
91
|
|
75
92
|
hosts = default_inputs[:hosts]
|
76
93
|
|
94
|
+
|
95
|
+
|
77
96
|
# find the smallest decimal value to match the
|
78
|
-
# required number of hosts on a network
|
97
|
+
# required number of hosts on a network
|
98
|
+
|
99
|
+
octet_n, col = hosts_per_subnet.reverse.detect {|x| x.last > hosts}
|
79
100
|
|
80
|
-
|
101
|
+
bit = octets[octet_n].bits[col]
|
81
102
|
|
82
|
-
magic_number, prefix =
|
83
|
-
|
84
|
-
no_of_subnets = 2 ** (n+1)
|
103
|
+
magic_number, prefix = bit.hosts_per_subnet, bit.prefix
|
104
|
+
no_of_subnets = (2 ** 8) / bit.decimal
|
85
105
|
|
106
|
+
|
107
|
+
n = col
|
86
108
|
|
87
109
|
# add the new mask to the octet
|
88
110
|
|
89
|
-
octets[
|
111
|
+
octets[octet_n].mask = octets[octet_n].bits.map(&:decimal)[0..n].inject(:+)
|
90
112
|
subnet_mask = octets.map(&:mask).join('.')
|
91
113
|
|
92
114
|
subnets = no_of_subnets.times.inject([]) do |r,n|
|
@@ -119,7 +141,7 @@ class SubnetCalc
|
|
119
141
|
max_subnets: no_of_subnets
|
120
142
|
}
|
121
143
|
|
122
|
-
@
|
144
|
+
@octet_n = octet_n
|
123
145
|
@h = result
|
124
146
|
end
|
125
147
|
|
@@ -137,12 +159,11 @@ class SubnetCalc
|
|
137
159
|
|
138
160
|
tfo2 = TableFormatter.new
|
139
161
|
|
140
|
-
prefixes = @prefixes[@
|
141
|
-
tfo2.source = [@h[:subnet_bitmask][@
|
162
|
+
prefixes = @prefixes[@octet_n].map {|x| '/' + x.to_s}
|
163
|
+
tfo2.source = [@h[:subnet_bitmask][@octet_n].chars, prefixes]
|
142
164
|
tfo2.labels = 8.times.map {|n| (2 ** n).to_s }.reverse
|
143
165
|
octet_table = tfo2.display(markdown: true).to_s
|
144
166
|
|
145
|
-
|
146
167
|
<<EOF
|
147
168
|
|
148
169
|
|
@@ -172,7 +193,7 @@ Summary
|
|
172
193
|
Breakdown
|
173
194
|
---------
|
174
195
|
|
175
|
-
#{(@
|
196
|
+
#{(@octet_n + 1).ordinal} octet:
|
176
197
|
|
177
198
|
#{indent octet_table}
|
178
199
|
|
@@ -191,9 +212,9 @@ EOF
|
|
191
212
|
@h
|
192
213
|
end
|
193
214
|
|
194
|
-
private
|
215
|
+
private
|
195
216
|
|
196
217
|
def indent(s, i=2)
|
197
218
|
s.lines.map{|x| ' ' * i + x}.join
|
198
219
|
end
|
199
|
-
end
|
220
|
+
end
|
data.tar.gz.sig
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
��Jў`��;V"Fz��=T����?ʖu���d����{_��'��f�Ӽ���խ`^�w�� V�[��Ra4n����A��g���É�]�ϋ��i�<H��d�߹"�&Zb�Pą���^d�A{���J��fV�f���|^�|���]6�ob����ݕ���-̊���tg�`��$��0���b��o��w���`�xϮ�A���m`&A�EIO��K������!Q�$d_�W���4!�`jT���G��q�91Я�
|
metadata
CHANGED
metadata.gz.sig
CHANGED
Binary file
|