subnet_calc 0.1.2 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3541fb5686bd771f912d578a7d12fda7a53cbeaa
4
- data.tar.gz: 0a08287455c96a33016ac1e3be4c54c59e80786f
3
+ metadata.gz: 4bb254774e26a03f73b61199cb800765362710d7
4
+ data.tar.gz: b534f281a42f5d2119145881ebe9852fe5be0eb6
5
5
  SHA512:
6
- metadata.gz: 4fc514377befd4ff8fa3c6d8fa97c0ba9b3aab5e5dafae7807b2d8d483e0603ae8d813128ae5011949a54cdac09f1b2844f42e4c87721c371d727238a7079567
7
- data.tar.gz: 9ca4eb4802ad73c97fae25fef5d0f0f2637cbc666c5df33e70e1fc15c90eda941dcb43cc0f0e864efc9f575f3128cf2b62baa79347a37e630aa06331232cea5b
6
+ metadata.gz: 435ad51490ff9c2b322e609917333c2450b2ac24621c5643ad8b453513192e4f279961b23f15d1b9e6fa2c4ca406e20614cd191ea872b27be076af1d53b9117c
7
+ data.tar.gz: 71c37c544ed68cf59919a8e41a9fc934d1c5be678f77cf8866e635520d9133575075c95023f72d396f1e34b1b1be8c7521c602257f3ef4ea748278e93a77b476
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
@@ -1 +1,2 @@
1
- (`8XP]?ز��n�%��F{�*]e�.!}X�@�f�>3 ��~�YG�������D+$����4%�v4f[��̸O��V����cĝ�fx�Ҵ]+�R��7���*�`C\��I��XK�������o:���O��dR<�TT&���U_����虒U�}�`����A����=2
1
+ riVkINI��ˏIt�ύU9
2
+ ȱOm��N�� �b�]Wz�{��ݩ�W�b~�HȰ���R��I鄰���*YE,�� �Gi]�DS,
data/lib/subnet_calc.rb CHANGED
@@ -28,6 +28,11 @@ class SubnetCalc
28
28
 
29
29
  @inputs = inputs
30
30
  default_inputs = {hosts: 254, ip: '192.168.0.0'}.merge(inputs)
31
+
32
+ # Using the input(s) supplied:
33
+
34
+ hosts = default_inputs[:hosts]
35
+
31
36
 
32
37
  @prefixes = 1.upto(32).each_slice(8).to_a
33
38
 
@@ -66,7 +71,7 @@ class SubnetCalc
66
71
 
67
72
  # determine what class of network we are using
68
73
 
69
- class_type = 'c'
74
+ class_type = hosts <= 254 ? 'c' : 'b'
70
75
 
71
76
  # Identify the network bits and the host bits
72
77
 
@@ -87,23 +92,20 @@ class SubnetCalc
87
92
 
88
93
  # ------------------------------------
89
94
 
90
- # Using the input(s) supplied:
91
-
92
- hosts = default_inputs[:hosts]
93
-
94
95
 
95
96
 
96
97
  # find the smallest decimal value to match the
97
98
  # required number of hosts on a network
98
99
 
99
- octet_n, col = hosts_per_subnet.reverse.detect {|x| x.last > hosts}
100
+ octet_n, col = hosts_per_subnet.reverse.detect {|x| x.last > hosts + 1}
100
101
 
101
102
  bit = octets[octet_n].bits[col]
102
103
 
103
- magic_number, prefix = bit.hosts_per_subnet, bit.prefix
104
-
105
- no_of_subnets = (2 ** 8) / bit.hosts_per_subnet
104
+ magic_number, prefix = bit.hosts_per_subnet, bit.prefix
106
105
 
106
+ no_of_subnets = (2 ** 8.0) / bit.hosts_per_subnet
107
+
108
+ segments = (no_of_subnets >= 1 ? no_of_subnets : 256 / (256 * no_of_subnets)).to_i
107
109
  n = col
108
110
 
109
111
  # add the new mask to the octet
@@ -111,22 +113,17 @@ class SubnetCalc
111
113
  octets[octet_n].mask = octets[octet_n].bits.map(&:decimal)[0..n].inject(:+)
112
114
  subnet_mask = octets.map(&:mask).join('.')
113
115
 
114
- subnets = no_of_subnets.times.inject([]) do |r,n|
115
-
116
- i = r.last ? r.last.network + magic_number : 0
117
-
118
- broadcast = i + magic_number - 1
119
- first = i + 1
120
- last = broadcast - 1
121
- r << OpenStruct.new({network: i, first: first, last: last,
122
- broadcast: broadcast})
116
+ subnets = case class_type
117
+ when 'c'
118
+ class_subnets(segments, magic_number)
119
+ when 'b'
120
+ class_b_subnets(256 / segments, bit.decimal)
123
121
  end
122
+
123
+ ip = (default_inputs[:ip] + ' ') .split('.')[0..octet_n-1]
124
124
 
125
- ip = (default_inputs[:ip] + ' ') .split('.')
126
- ip[class_n] = subnets.first.first
127
- first_ip = ip.join('.')
128
- ip[class_n] = subnets.first.last
129
- last_ip = ip.join('.')
125
+ first_ip = (ip + [subnets.first.first]).join('.')
126
+ last_ip = (ip + [subnets.first.last]).join('.')
130
127
 
131
128
  result = {
132
129
  class_type: class_type.upcase,
@@ -138,7 +135,7 @@ class SubnetCalc
138
135
  subnets: subnets,
139
136
  range: "%s-%s" % [first_ip, last_ip],
140
137
  subnet_bits: n+1,
141
- max_subnets: no_of_subnets
138
+ max_subnets: segments
142
139
  }
143
140
 
144
141
  @octet_n = octet_n
@@ -151,9 +148,15 @@ class SubnetCalc
151
148
 
152
149
  tfo = TableFormatter.new
153
150
 
154
- tfo.source = @h[:subnets].map{|x| x.to_h.values.map(&:to_s) }
155
- tfo.labels = %w(Network 1st last broadcast)
156
- subnets_table = tfo.display(markdown: true).to_s
151
+ tfo.source = @h[:subnets].map.with_index{|x,i| ([i] + x.to_h.values).map(&:to_s) }
152
+ tfo.labels = %w(index Network 1st last broadcast)
153
+ full_subnets_table = tfo.display(markdown: true).to_s
154
+
155
+ subnets_table = if full_subnets_table.lines.length > 14 then
156
+ (full_subnets_table.lines[0..13] + ["\n ... "]).join
157
+ else
158
+ full_subnets_table
159
+ end
157
160
 
158
161
  # octet broken down
159
162
 
@@ -214,6 +217,47 @@ EOF
214
217
 
215
218
  private
216
219
 
220
+ def class_subnets(n, block_size)
221
+
222
+ i = 0
223
+
224
+ subnets = n.times.inject([]) do |r,n|
225
+
226
+ broadcast = i + block_size - 1
227
+ first = i + 1
228
+ last = broadcast - 1
229
+
230
+
231
+
232
+ h = if block_given? then
233
+ yield(i,first,last,broadcast)
234
+ else
235
+ { network: i, first: first, last: last, broadcast: broadcast }
236
+ end
237
+ i += block_size
238
+
239
+ r << OpenStruct.new(h)
240
+
241
+ end
242
+
243
+ end
244
+
245
+ def class_b_subnets(n, block_size)
246
+
247
+ class_subnets(n, block_size) do |network, first, last, broadcast|
248
+
249
+ {
250
+ network: [network, 0].join('.'),
251
+ first: [network, 1].join('.'),
252
+ last: [broadcast, 254].join('.'),
253
+ broadcast: [broadcast, 255].join('.')
254
+ }
255
+
256
+ end
257
+
258
+ end
259
+
260
+
217
261
  def indent(s, i=2)
218
262
  s.lines.map{|x| ' ' * i + x}.join
219
263
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subnet_calc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -103,5 +103,5 @@ rubyforge_project:
103
103
  rubygems_version: 2.5.1
104
104
  signing_key:
105
105
  specification_version: 4
106
- summary: A subnet calculator (only tested for a class C network)
106
+ summary: A subnet calculator (only tested for class C and class B networks)
107
107
  test_files: []
metadata.gz.sig CHANGED
Binary file