subnet_calc 0.1.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 +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +1 -0
- data/lib/subnet_calc.rb +199 -0
- metadata +107 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a8e521e99a577d3c78b49a8400f3b38133150d4d
|
4
|
+
data.tar.gz: 0c006f2519c6aa9916020d326b481fa019031c30
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c355f531b300e7a96dfa0e1f21ef771de13a69714a2a81836766aa4ec4419a810eb02112134d9e897ed505a7ec5a3f88ac7dfa13c77d3713da25f42a54b3803c
|
7
|
+
data.tar.gz: 3f275b0feb9baaa4bc2548d19ae8e3fd101f801a75b58ef0798bfa220d3cf111e29b6c8515a9aeb61cc8837fdaa30b35d6bd54009659f56892f0670873d00fe1
|
checksums.yaml.gz.sig
ADDED
Binary file
|
data.tar.gz.sig
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1�Kz�ݧ��a�� o���3
|
data/lib/subnet_calc.rb
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# file: subnet_calc.rb
|
4
|
+
|
5
|
+
|
6
|
+
require 'kvx'
|
7
|
+
require 'ostruct'
|
8
|
+
require 'table-formatter'
|
9
|
+
|
10
|
+
|
11
|
+
module Ordinals
|
12
|
+
|
13
|
+
refine Fixnum do
|
14
|
+
def ordinal
|
15
|
+
self.to_s + ( (10...20).include?(self) ? 'th' :
|
16
|
+
%w{ th st nd rd th th th th th th }[self % 10] )
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class SubnetCalc
|
22
|
+
|
23
|
+
using Ordinals
|
24
|
+
|
25
|
+
attr_reader :to_h
|
26
|
+
|
27
|
+
def initialize(inputs={hosts: 254})
|
28
|
+
|
29
|
+
@inputs = inputs
|
30
|
+
default_inputs = {hosts: 254, ip: '192.168.0.0'}.merge(inputs)
|
31
|
+
|
32
|
+
@prefixes = 1.upto(32).each_slice(8).to_a
|
33
|
+
=begin
|
34
|
+
#=> [
|
35
|
+
[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16],
|
36
|
+
[17, 18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31, 32]
|
37
|
+
]
|
38
|
+
=end
|
39
|
+
|
40
|
+
octets = @prefixes.map do |octet|
|
41
|
+
|
42
|
+
a = octet.map.with_index do |prefix, i|
|
43
|
+
OpenStruct.new prefix: prefix, decimal: 2 ** (octet.length - 1 - i )
|
44
|
+
end
|
45
|
+
|
46
|
+
OpenStruct.new mask: nil, bits: a
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
# determine what class of network we are using
|
51
|
+
|
52
|
+
class_type = 'c'
|
53
|
+
|
54
|
+
# Identify the network bits and the host bits
|
55
|
+
|
56
|
+
classes = ('a'..'d')
|
57
|
+
|
58
|
+
# identify the initial network bits for a class *a,b or c*
|
59
|
+
|
60
|
+
class_n = (classes.to_a.index(class_type) + 1)
|
61
|
+
network_bits = class_n.times.map {|x| Array.new(8, 1)}
|
62
|
+
host_bits = (classes.to_a.length - class_n).times.map {|x| Array.new(8, 0)}
|
63
|
+
address_bits = network_bits + host_bits
|
64
|
+
|
65
|
+
# add the mask to each octet
|
66
|
+
|
67
|
+
octets.each.with_index do |octet, i|
|
68
|
+
octet.mask = address_bits[i].join.to_i(2)
|
69
|
+
end
|
70
|
+
|
71
|
+
# ------------------------------------
|
72
|
+
|
73
|
+
# Using the input(s) supplied:
|
74
|
+
|
75
|
+
hosts = default_inputs[:hosts]
|
76
|
+
|
77
|
+
# find the smallest decimal value to match the
|
78
|
+
# required number of hosts on a network
|
79
|
+
|
80
|
+
col = octets[class_n].bits.reverse.detect {|x| x.decimal > hosts }
|
81
|
+
|
82
|
+
magic_number, prefix = col.decimal, col.prefix
|
83
|
+
n = octets[class_n].bits.map(&:decimal).index(magic_number)
|
84
|
+
no_of_subnets = 2 ** (n+1)
|
85
|
+
|
86
|
+
|
87
|
+
# add the new mask to the octet
|
88
|
+
|
89
|
+
octets[class_n].mask = octets[class_n].bits.map(&:decimal)[0..n].inject(:+)
|
90
|
+
subnet_mask = octets.map(&:mask).join('.')
|
91
|
+
|
92
|
+
subnets = no_of_subnets.times.inject([]) do |r,n|
|
93
|
+
|
94
|
+
i = r.last ? r.last.network + magic_number : 0
|
95
|
+
|
96
|
+
broadcast = i + magic_number - 1
|
97
|
+
first = i + 1
|
98
|
+
last = broadcast - 1
|
99
|
+
r << OpenStruct.new({network: i, first: first, last: last,
|
100
|
+
broadcast: broadcast})
|
101
|
+
end
|
102
|
+
|
103
|
+
ip = (default_inputs[:ip] + ' ') .split('.')
|
104
|
+
ip[class_n] = subnets.first.first
|
105
|
+
first_ip = ip.join('.')
|
106
|
+
ip[class_n] = subnets.first.last
|
107
|
+
last_ip = ip.join('.')
|
108
|
+
|
109
|
+
result = {
|
110
|
+
class_type: class_type.upcase,
|
111
|
+
magic_number: magic_number,
|
112
|
+
hosts: magic_number - 2,
|
113
|
+
subnet_mask: subnet_mask,
|
114
|
+
subnet_bitmask: subnet_mask.split('.').map {|x| x.to_i.to_s(2)},
|
115
|
+
prefix: prefix,
|
116
|
+
subnets: subnets,
|
117
|
+
range: "%s-%s" % [first_ip, last_ip],
|
118
|
+
subnet_bits: n+1,
|
119
|
+
max_subnets: no_of_subnets
|
120
|
+
}
|
121
|
+
|
122
|
+
@class_n = class_n
|
123
|
+
@h = result
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
def to_s()
|
128
|
+
|
129
|
+
|
130
|
+
tfo = TableFormatter.new
|
131
|
+
|
132
|
+
tfo.source = @h[:subnets].map{|x| x.to_h.values.map(&:to_s) }
|
133
|
+
tfo.labels = %w(Network 1st last broadcast)
|
134
|
+
subnets_table = tfo.display(markdown: true).to_s
|
135
|
+
|
136
|
+
# octet broken down
|
137
|
+
|
138
|
+
tfo2 = TableFormatter.new
|
139
|
+
|
140
|
+
prefixes = @prefixes[@class_n].map {|x| '/' + x.to_s}
|
141
|
+
tfo2.source = [@h[:subnet_bitmask][@class_n].chars, prefixes]
|
142
|
+
tfo2.labels = 8.times.map {|n| (2 ** n).to_s }.reverse
|
143
|
+
octet_table = tfo2.display(markdown: true).to_s
|
144
|
+
|
145
|
+
|
146
|
+
<<EOF
|
147
|
+
|
148
|
+
|
149
|
+
Subnet calculator
|
150
|
+
=================
|
151
|
+
|
152
|
+
Inputs:
|
153
|
+
|
154
|
+
#{Kvx.new(@inputs).to_s.lines.map {|x| ' ' * 2 + x}.join}
|
155
|
+
|
156
|
+
Summary
|
157
|
+
-------
|
158
|
+
|
159
|
+
* Network class: #{@h[:class_type]}
|
160
|
+
* magic number: #{@h[:magic_number]}
|
161
|
+
* hosts per subnet (magic number - 2 ): #{@h[:hosts]}
|
162
|
+
* subnet mask: #{@h[:subnet_mask]}
|
163
|
+
* subnet bitmask: #{@h[:subnet_bitmask].join('.')}
|
164
|
+
* prefix bit-length: #{@h[:prefix]}
|
165
|
+
* range: #{@h[:range]}
|
166
|
+
|
167
|
+
* subnet_bits: #{@h[:subnet_bits]}
|
168
|
+
* maximum subnets: #{@h[:max_subnets]}
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
Breakdown
|
173
|
+
---------
|
174
|
+
|
175
|
+
#{(@class_n + 1).ordinal} octet:
|
176
|
+
|
177
|
+
#{indent octet_table}
|
178
|
+
|
179
|
+
### Subnets
|
180
|
+
|
181
|
+
|
182
|
+
#{indent subnets_table}
|
183
|
+
|
184
|
+
-----------------------------------------------
|
185
|
+
|
186
|
+
EOF
|
187
|
+
|
188
|
+
end
|
189
|
+
|
190
|
+
def to_h()
|
191
|
+
@h
|
192
|
+
end
|
193
|
+
|
194
|
+
private
|
195
|
+
|
196
|
+
def indent(s, i=2)
|
197
|
+
s.lines.map{|x| ' ' * i + x}.join
|
198
|
+
end
|
199
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: subnet_calc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Robertson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
- |
|
12
|
+
-----BEGIN CERTIFICATE-----
|
13
|
+
MIIDljCCAn6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBIMRIwEAYDVQQDDAlnZW1t
|
14
|
+
YXN0ZXIxHjAcBgoJkiaJk/IsZAEZFg5qYW1lc3JvYmVydHNvbjESMBAGCgmSJomT
|
15
|
+
8ixkARkWAmV1MB4XDTE2MTEwNjE1MjYxMFoXDTE3MTEwNjE1MjYxMFowSDESMBAG
|
16
|
+
A1UEAwwJZ2VtbWFzdGVyMR4wHAYKCZImiZPyLGQBGRYOamFtZXNyb2JlcnRzb24x
|
17
|
+
EjAQBgoJkiaJk/IsZAEZFgJldTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
|
18
|
+
ggEBAPVqVqTGhTW3Ph6mzF3tBResSXXhxAkyJbw4BcVRgk8dvqJwNpAgtlt/BB9B
|
19
|
+
KJmuAYo1cviZ8XIXCrbSEjXSLNF9acv2YSYZdmGdwNLtGQVKt+N9DLbFIGRMh2L2
|
20
|
+
4/he6hxypRKDS9gONQ4YXUVZdCawA3++wslin+BREK8Ujy6ULTkRvQJZF2J6J8ex
|
21
|
+
a5oGJsQuDyO52rz2m7nm+LPPGTuuv8wIjyuYL/fn/nw4FY0XfW9mOmLHFfEo/9HZ
|
22
|
+
7SEsrpA4u2Izxiz/ngnICUipJznXe2fF2KX9vYpnHpfV2jeeN6IoZX8Jg5pyoZBz
|
23
|
+
5bQISgWwJfpJjhsOCGy3ju631/UCAwEAAaOBijCBhzAJBgNVHRMEAjAAMAsGA1Ud
|
24
|
+
DwQEAwIEsDAdBgNVHQ4EFgQU1lNDXV4p/8OlTXMCiS+R28ekARgwJgYDVR0RBB8w
|
25
|
+
HYEbZ2VtbWFzdGVyQGphbWVzcm9iZXJ0c29uLmV1MCYGA1UdEgQfMB2BG2dlbW1h
|
26
|
+
c3RlckBqYW1lc3JvYmVydHNvbi5ldTANBgkqhkiG9w0BAQUFAAOCAQEAj62jBmu0
|
27
|
+
ttOHIlwiLlINXEEIfnLPAMX85eeepU+FFrC+LfAxRLZ/Z+jBMDiNaNC4696jdZnW
|
28
|
+
14Bc0EuI6ZSshPPQHlkZ9vCVvS5IbymMkhsGSdzBCn7U3RhbItc8eaZii6F0uuZX
|
29
|
+
ytJiqiVwujezDxT2QYyI94xbWfU6f9tBNX7blfXysexghcEDm0BxtrEOk4ssykt8
|
30
|
+
PyPsXxOL9+WA/tVhAFUQeAogZNF5ZFOO1t0EWxTthM5H79lzF7qtthPO2nsRGeNn
|
31
|
+
oAH9dWawpFIItSVexeVF4ycrINDboVQH/q93Udr9ZLbwux6Dp2bpsAxyXAdIw+Um
|
32
|
+
ZWJvjR10ewlMyQ==
|
33
|
+
-----END CERTIFICATE-----
|
34
|
+
date: 2016-11-06 00:00:00.000000000 Z
|
35
|
+
dependencies:
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: kvx
|
38
|
+
requirement: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0.6'
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.6.1
|
46
|
+
type: :runtime
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - "~>"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0.6'
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 0.6.1
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: table-formatter
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0.3'
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 0.3.1
|
66
|
+
type: :runtime
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - "~>"
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0.3'
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.3.1
|
76
|
+
description:
|
77
|
+
email: james@r0bertson.co.uk
|
78
|
+
executables: []
|
79
|
+
extensions: []
|
80
|
+
extra_rdoc_files: []
|
81
|
+
files:
|
82
|
+
- lib/subnet_calc.rb
|
83
|
+
homepage: https://github.com/jrobertson/subnet_calc
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.5.1
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: A subnet calculator (only tested for a class C network)
|
107
|
+
test_files: []
|
metadata.gz.sig
ADDED
Binary file
|