parse_dhcp 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00e5bd2deb3909bdd339fe260544c8ac1baf3477
4
+ data.tar.gz: 859765b22ffa43ec3fb7b29a1ba1e96f106ec796
5
+ SHA512:
6
+ metadata.gz: ea1e9ad0138b8d5c37534d836c9641c3c3236c8b0839357e257ecadf9bdbe058f11d92be354aa7bcc8fa468ee00076d909f4ecec78c2eb5569203a0842d2b053
7
+ data.tar.gz: b93c2c71b78ba8ec56b88fd5f73183a1ebcf6122ac4ff77b958c06b80d617c0f6670ff70d15216d69d40d09d710e347355c308a0bc4ed89156d7441b3a6a63e2
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in parse_dhcp.gemspec
4
+ gem 'parse_dhcp', :git => 'git@github.com:hoanganhhanoi/parse_dhcp.git'
5
+ gem 'pry'
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nguyen hoang anh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,229 @@
1
+ # ParseDhcp
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/parse_dhcp`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'parse_dhcp'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install parse_dhcp
22
+
23
+ ## Usage
24
+ __1. Read file__
25
+
26
+ __With file dhcp.conf__
27
+
28
+ ```ruby
29
+ subnet 192.168.1.0 netmask 255.255.255.0 {
30
+
31
+ # config file
32
+ # config file
33
+ option routers 192.168.1.1;
34
+ option subnet-mask 255.255.255.0;
35
+ option broadcast-address 192.168.1.255;
36
+ option domain-name-servers 194.168.4.100;
37
+ option ntp-servers 192.168.1.1;
38
+ option netbios-name-servers 192.168.1.1;
39
+ option netbios-node-type 2;
40
+ default-lease-time 86400;
41
+ max-lease-time 86400;
42
+ authoritative;
43
+
44
+ # config file
45
+ # config file
46
+ pool {
47
+ # config file
48
+ range 192.168.25.20 192.168.25.200;
49
+ allow unknown-clients;
50
+ host bla1 {
51
+ # comment .......
52
+ hardware ethernet DD:GH:DF:E5:F7:D7;
53
+ fixed-address 192.168.1.2;
54
+ }
55
+ host bla2 {
56
+ # comment .......
57
+ hardware ethernet 00:JJ:YU:38:AC:45;
58
+ fixed-address 192.168.1.20;
59
+ }
60
+ }
61
+ }
62
+
63
+ subnet 10.152.187.0 netmask 255.255.255.0 {
64
+
65
+ option routers 10.152.187.1;
66
+ option subnet-mask 255.255.255.0;
67
+ option broadcast-address 10.150.168.255;
68
+ option domain-name-servers 192.168.4.101;
69
+ option ntp-servers 10.152.187.10;
70
+ option netbios-name-servers 10.152.187.12;
71
+ option netbios-node-type 2;
72
+
73
+ default-lease-time 86400;
74
+ max-lease-time 86400;
75
+ pool {
76
+ range 192.168.25.20 192.168.25.30;
77
+ denny unknown-clients;
78
+ host bla3 {
79
+ hardware ethernet 00:KK:HD:66:55:9B;
80
+ fixed-address 10.152.187.2;
81
+ }
82
+ }
83
+ }
84
+ ```
85
+
86
+ __1. Read file__
87
+
88
+ * The first, create object dhcp with param: the path to file config
89
+
90
+ ```ruby
91
+ dhcp = Parse_Dhcp::DHCP.new(path)
92
+ ```
93
+
94
+ * Get subnet
95
+
96
+ ```ruby
97
+ dhcp.subnets
98
+ #Result
99
+ => ["192.168.1.0", "10.152.187.0"]
100
+ ```
101
+
102
+ * Get netmask
103
+
104
+ ```ruby
105
+ dhcp.netmasks
106
+ #Result
107
+ => ["255.255.255.0"]
108
+ ```
109
+
110
+
111
+ * Get list pool
112
+
113
+ ```ruby
114
+ dhcp.pools
115
+ #Result
116
+ => [{"1"=>
117
+ {"host"=>"bla1",
118
+ "hardware_ethernet"=>"DD:GH:DF:E5:F7:D7",
119
+ "fixed-address"=>"192.168.1.2"},
120
+ "2"=>
121
+ {"host"=>"bla2",
122
+ "hardware_ethernet"=>"00:JJ:YU:38:AC:45",
123
+ "fixed-address"=>"192.168.1.20"}},
124
+ {"1"=>
125
+ {"host"=>"bla3",
126
+ "hardware_ethernet"=>"00:KK:HD:66:55:9B",
127
+ "fixed-address"=>"10.152.187.2"}}]
128
+ ```
129
+
130
+ * Get list option
131
+
132
+ ```ruby
133
+ dhcp.options
134
+ => [{"routers"=>"192.168.1.1",
135
+ "subnet-mask"=>"255.255.255.0",
136
+ "broadcast-address"=>"192.168.1.255",
137
+ "domain-name-servers"=>"194.168.4.100",
138
+ "ntp-servers"=>"192.168.1.1",
139
+ "netbios-name-servers"=>"192.168.1.1",
140
+ "netbios-node-type"=>"2",
141
+ "default-lease-time"=>"86400",
142
+ "max-lease-time"=>"86400",
143
+ "authoritative"=>true},
144
+ {"routers"=>"10.152.187.1",
145
+ "subnet-mask"=>"255.255.255.0",
146
+ "broadcast-address"=>"10.150.168.255",
147
+ "domain-name-servers"=>"192.168.4.101",
148
+ "ntp-servers"=>"10.152.187.10",
149
+ "netbios-name-servers"=>"10.152.187.12",
150
+ "netbios-node-type"=>"2",
151
+ "default-lease-time"=>"86400",
152
+ "max-lease-time"=>"86400"}]
153
+ ```
154
+
155
+ * Get range, allow, denny in pool
156
+
157
+ ```ruby
158
+ dhcp.ranges
159
+ #Result
160
+ => ["192.168.25.20 192.168.25.200", "192.168.25.20 192.168.25.30"]
161
+
162
+ dhcp.allow
163
+ #Result
164
+ => ["unknown-clients"]
165
+
166
+ dhcp.denny
167
+ #Result
168
+ => ["unknown-clients"]
169
+ ```
170
+
171
+ * Get data to object array
172
+
173
+ ```ruby
174
+ array_net = dhcp.net
175
+ # Result
176
+ array_net[0]
177
+ => [#<Net:0xb98d1594
178
+ @differ=
179
+ {"default-lease-time"=>"60400",
180
+ "max-lease-time"=>"60400",
181
+ "authoritative"=>true},
182
+ @netmask="255.255.255.0",
183
+ @option=
184
+ {"routers"=>"192.168.1.1",
185
+ "subnet-mask"=>"255.255.255.0",
186
+ "broadcast-address"=>"192.168.1.255",
187
+ "domain-name-servers"=>"194.168.4.100",
188
+ "ntp-servers"=>"192.168.1.1",
189
+ "netbios-name-servers"=>"192.168.1.1"},
190
+ @pool=
191
+ {"range"=>{"min"=>"192.168.25.20", "max"=>"192.168.25.200"},
192
+ "allow"=>"unknown-clients",
193
+ "denny"=>nil,
194
+ "hosts"=>
195
+ [#<Host:0xb98d8a24
196
+ @fix_address="192.168.1.2",
197
+ @hardware_ethernet="DD:GH:DF:E5:F7:D7",
198
+ @host="bla1">,
199
+ #<Host:0xb98d88a8
200
+
201
+ ```
202
+
203
+ __2. Write file__
204
+
205
+ Create object net, then set attribute for object. Then call method write_file in module WriteConf with param: "path/file_name", "array_net"
206
+
207
+ ```ruby
208
+ result = dhcp.write(path/file_name, array_net)
209
+ # or
210
+ result = WriteConf.write_file(path/file_name, array_net)
211
+ # Success
212
+ => true
213
+ # Fail
214
+ => false
215
+ ```
216
+ ## Development
217
+
218
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
219
+
220
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
221
+
222
+ ## Contributing
223
+
224
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/parse_dhcp.
225
+
226
+
227
+ ## License
228
+
229
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "parse_dhcp"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,55 @@
1
+ subnet 192.168.1.0 netmask 255.255.255.0 {
2
+
3
+ # config file
4
+ # config file
5
+
6
+ option routers 192.168.1.1;
7
+ option subnet-mask 255.255.255.0;
8
+ option broadcast-address 192.168.1.255;
9
+ option domain-name-servers 194.168.4.100;
10
+ option ntp-servers 192.168.1.1;
11
+ option netbios-name-servers 192.168.1.1;
12
+ default-lease-time 60400;
13
+ max-lease-time 60400;
14
+ authoritative;
15
+
16
+ # config file
17
+ # config file
18
+ pool {
19
+ # config file
20
+ range 192.168.25.20 192.168.25.200;
21
+ allow unknown-clients;
22
+ host bla1 {
23
+ # comment .......
24
+ hardware ethernet DD:GH:DF:E5:F7:D7;
25
+ fixed-address 192.168.1.2;
26
+ }
27
+ host bla2 {
28
+ # comment .......
29
+ hardware ethernet 00:JJ:YU:38:AC:45;
30
+ fixed-address 192.168.1.20;
31
+ }
32
+ }
33
+ }
34
+
35
+ subnet 10.152.187.0 netmask 255.255.255.255 {
36
+
37
+ option routers 10.152.187.1;
38
+ option subnet-mask 255.255.255.255;
39
+ option broadcast-address 10.150.168.255;
40
+ option domain-name-servers 192.168.4.101;
41
+ option ntp-servers 10.152.187.10;
42
+ option netbios-name-servers 10.152.187.12;
43
+ option netbios-node-type 2;
44
+
45
+ default-lease-time 86400;
46
+ max-lease-time 86400;
47
+ pool {
48
+ range 192.168.25.20 192.168.25.30;
49
+ denny unknown-clients;
50
+ host bla3 {
51
+ hardware ethernet 00:KK:HD:66:55:9B;
52
+ fixed-address 10.152.187.2;
53
+ }
54
+ }
55
+ }
@@ -0,0 +1,45 @@
1
+ subnet 192.168.1.0 netmask 255.255.255.0 {
2
+ routers 192.168.1.1;
3
+ subnet-mask 255.255.255.0;
4
+ broadcast-address 192.168.1.255;
5
+ domain-name-servers 194.168.4.100;
6
+ authoritative;
7
+ default-lease-time 2;
8
+ max-lease-time 86400;
9
+ pool {
10
+ range 192.168.25.20 192.168.25.200;
11
+ allow unknown-clients;
12
+ denny ;
13
+ host bla1 {
14
+ hardware ethernet DD:GH:DF:E5:F7:D7;
15
+ fixed-address 192.168.1.2
16
+ }
17
+ host bla2 {
18
+ hardware ethernet 00:JJ:YU:38:AC:45;
19
+ fixed-address 192.168.1.20
20
+ }
21
+ }
22
+ }
23
+
24
+ subnet 192.168.1.0 netmask 255.255.255.0 {
25
+ routers 192.168.1.1;
26
+ subnet-mask 255.255.255.0;
27
+ broadcast-address 192.168.1.255;
28
+ domain-name-servers 194.168.4.100;
29
+ default-lease-time 2;
30
+ max-lease-time 86400;
31
+ pool {
32
+ range 192.168.25.20 192.168.25.200;
33
+ allow ;
34
+ denny unknown-clients;
35
+ host bla1 {
36
+ hardware ethernet DD:GH:DF:E5:F7:D7;
37
+ fixed-address 192.168.1.2
38
+ }
39
+ host bla2 {
40
+ hardware ethernet 00:JJ:YU:38:AC:45;
41
+ fixed-address 192.168.1.20
42
+ }
43
+ }
44
+ }
45
+
@@ -0,0 +1,46 @@
1
+ subnet 192.168.1.0 netmask 255.255.255.0 {
2
+ option routers 192.168.1.1;
3
+ option subnet-mask 255.255.255.0;
4
+ option broadcast-address 192.168.1.255;
5
+ option domain-name-servers 194.168.4.100;
6
+ option ntp-servers 192.168.1.1;
7
+ option netbios-name-servers 192.168.1.1;
8
+ default-lease-time 60400;
9
+ max-lease-time 60400;
10
+ authoritative;
11
+ pool {
12
+ range 192.168.25.20 192.168.25.200;
13
+ allow unknown-clients;
14
+ denny ;
15
+ host bla1 {
16
+ hardware ethernet DD:GH:DF:E5:F7:D7;
17
+ fixed-address 192.168.1.2
18
+ }
19
+ host bla2 {
20
+ hardware ethernet 00:JJ:YU:38:AC:45;
21
+ fixed-address 192.168.1.20
22
+ }
23
+ }
24
+ }
25
+
26
+ subnet 10.152.187.0 netmask 255.255.255.255 {
27
+ option routers 10.152.187.1;
28
+ option subnet-mask 255.255.255.255;
29
+ option broadcast-address 10.150.168.255;
30
+ option domain-name-servers 192.168.4.101;
31
+ option ntp-servers 10.152.187.10;
32
+ option netbios-name-servers 10.152.187.12;
33
+ option netbios-node-type 2;
34
+ default-lease-time 86400;
35
+ max-lease-time 86400;
36
+ pool {
37
+ range 192.168.25.20 192.168.25.30;
38
+ allow ;
39
+ denny unknown-clients;
40
+ host bla3 {
41
+ hardware ethernet 00:KK:HD:66:55:9B;
42
+ fixed-address 10.152.187.2
43
+ }
44
+ }
45
+ }
46
+
@@ -0,0 +1,43 @@
1
+ subnet 192.168.1.0 netmask 255.255.255.0 {
2
+ routers 192.168.1.1;
3
+ subnet-mask 255.255.255.0;
4
+ broadcast-address 192.168.1.255;
5
+ domain-name-servers 194.168.4.100;
6
+ authoritative;
7
+ default-lease-time 2;
8
+ max-lease-tim 86400;
9
+ pool {
10
+ range 192.168.25.20 192.168.25.200;
11
+ allow unknown-clients;
12
+ host bla1 {
13
+ hardware ethernet DD:GH:DF:E5:F7:D7;
14
+ fixed-address 192.168.1.2
15
+ }
16
+ host bla2 {
17
+ hardware ethernet 00:JJ:YU:38:AC:45;
18
+ fixed-address 192.168.1.20
19
+ }
20
+ }
21
+ }
22
+
23
+ subnet 192.168.1.0 netmask 255.255.255.0 {
24
+ routers 192.168.1.1;
25
+ subnet-mask 255.255.255.0;
26
+ broadcast-address 192.168.1.255;
27
+ domain-name-servers 194.168.4.100;
28
+ default-lease-time 2;
29
+ max-lease-tim 86400;
30
+ pool {
31
+ range 192.168.25.20 192.168.25.200;
32
+ allow unknown-clients;
33
+ host bla1 {
34
+ hardware ethernet DD:GH:DF:E5:F7:D7;
35
+ fixed-address 192.168.1.2
36
+ }
37
+ host bla2 {
38
+ hardware ethernet 00:JJ:YU:38:AC:45;
39
+ fixed-address 192.168.1.20
40
+ }
41
+ }
42
+ }
43
+
@@ -0,0 +1,11 @@
1
+
2
+ class Host
3
+
4
+ attr_reader :host, :hardware_ethernet, :fixed_address
5
+
6
+ def initialize(host, hardware_ethernet, fixed_address)
7
+ @host = host
8
+ @hardware_ethernet = hardware_ethernet
9
+ @fixed_address = fixed_address
10
+ end
11
+ end
@@ -0,0 +1,58 @@
1
+ require_relative "writeconf.rb"
2
+ require_relative "host.rb"
3
+
4
+ class Net
5
+
6
+ attr_accessor :subnet, :netmask, :option, :differ, :pool
7
+
8
+ def initialize
9
+ @subnet = ""
10
+ @netmask = ""
11
+ @option = {}
12
+ @differ = {}
13
+ @pool = { "range" => "",
14
+ "allow" => "",
15
+ "denny" => "",
16
+ "hosts" => []
17
+ }
18
+ end
19
+
20
+ def test
21
+ net1 = Net.new
22
+ net2 = Net.new
23
+ # Set subnet
24
+ net1.subnet = "192.168.1.0"
25
+ net1.netmask = "255.255.255.0"
26
+ # Set option
27
+ net1.option["routers"] = "192.168.1.1"
28
+ net1.option["subnet-mask"] = "255.255.255.0"
29
+ net1.option["broadcast-address"] = "192.168.1.255"
30
+ net1.option["domain-name-servers"] = "194.168.4.100"
31
+ net1.differ["authoritative"] = "true"
32
+ net1.differ["default-lease-time"] = "2"
33
+ net1.differ['max-lease-time'] = "86400"
34
+ # Set pool
35
+ net1.pool["range"] = { "min" => "192.168.25.20", "max" => "192.168.25.200" }
36
+ net1.pool["allow"] = "unknown-clients"
37
+ net1.pool["hosts"] << Host.new("bla1", "DD:GH:DF:E5:F7:D7", "192.168.1.2")
38
+ net1.pool["hosts"] << Host.new("bla2", "00:JJ:YU:38:AC:45", "192.168.1.20")
39
+
40
+ # Set subnet
41
+ net2.subnet = "192.168.1.0"
42
+ net2.netmask = "255.255.255.0"
43
+ # Set option
44
+ net2.option["routers"] = "192.168.1.1"
45
+ net2.option["subnet-mask"] = "255.255.255.0"
46
+ net2.option["broadcast-address"] = "192.168.1.255"
47
+ net2.option["domain-name-servers"] = "194.168.4.100"
48
+ net2.differ["default-lease-time"] = "2"
49
+ net2.differ['max-lease-time'] = "86400"
50
+ # Set pool
51
+ net2.pool["range"] = { "min" => "192.168.25.20", "max" => "192.168.25.200" }
52
+ net2.pool["denny"] = "unknown-clients"
53
+ net2.pool["hosts"] << Host.new("bla1", "DD:GH:DF:E5:F7:D7", "192.168.1.2")
54
+ net2.pool["hosts"] << Host.new("bla2", "00:JJ:YU:38:AC:45", "192.168.1.20")
55
+ return [net1,net2]
56
+ end
57
+
58
+ end
@@ -0,0 +1,3 @@
1
+ module ParseDhcp
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,62 @@
1
+ module WriteConf
2
+
3
+ def self.write_file(file_name,nets)
4
+ if nets.nil?
5
+ return false
6
+ else
7
+ data = ""
8
+ nets.each do |net|
9
+ data += WriteConf.set_file(net)
10
+ end
11
+ file = File.open("#{file_name}","w+") do |file|
12
+ file.write(data)
13
+ end
14
+ return true
15
+ end
16
+ end
17
+
18
+ def self.test(file_name)
19
+ file = File.open("#{file_name}","w+") do |file|
20
+ file.write("test")
21
+ end
22
+ end
23
+
24
+ def self.set_file(net)
25
+ data = "subnet #{net.subnet} netmask #{net.netmask} {\n"
26
+
27
+ # set option
28
+ net.option.each do |key, value|
29
+ data += "\s\soption #{key}\s\s\s\s\s\s\s\s\s #{value};\n"
30
+ end
31
+
32
+ # set differ
33
+ net.differ.each do |key, value|
34
+ if key == "authoritative"
35
+ data += "\s\s#{key};\n"
36
+ else
37
+ data += "\s\s#{key}\s\s\s\s\s\s\s\s\s#{value};\n"
38
+ end
39
+ end
40
+
41
+ # set pool
42
+ data += "\s\spool {\n"
43
+ net.pool.each do |key, value|
44
+ if key.eql?("range")
45
+ data += "\s\s\s\srange #{value["min"]} #{value["max"]};\n"
46
+ elsif key.eql?("hosts")
47
+ value.each do |h|
48
+ data += "\s\s\s\shost #{h.host} {\n"
49
+ data += "\s\s\s\s\s\shardware ethernet #{h.hardware_ethernet};\n"
50
+ data += "\s\s\s\s\s\sfixed-address #{h.fixed_address}\n"
51
+ data += "\s\s\s\s}\n"
52
+ end
53
+ else
54
+ data += "\s\s\s\s#{key} #{value};\n"
55
+ end
56
+ end
57
+ data += "\s\s}\n"
58
+ data += "}\n\n"
59
+
60
+ return data
61
+ end
62
+ end
data/lib/parse_dhcp.rb ADDED
@@ -0,0 +1,396 @@
1
+ require_relative "parse_dhcp/writeconf.rb"
2
+ require_relative "parse_dhcp/net.rb"
3
+ require_relative "parse_dhcp/host.rb"
4
+
5
+ module Parse_Dhcp
6
+ class DHCP
7
+
8
+ attr_accessor :datas, :net
9
+
10
+ # Function constructor of DHCP
11
+ def initialize(path)
12
+ @datas = Parse_Dhcp::DHCP.read_file(path)
13
+ @array_net = []
14
+
15
+ end
16
+
17
+ # Read file config return Net. Net is hash
18
+ def self.read_file(path)
19
+
20
+ str = ""
21
+ count = 0
22
+ counter = 0
23
+ object = Hash.new
24
+
25
+ begin
26
+ if path.nil? || path.empty?
27
+ path = "../examples/default_dhcp.conf"
28
+ end
29
+ file = File.new("#{path}", "r")
30
+ while (line = file.gets)
31
+ if !line.eql?("\n") && !line.eql?("")
32
+ element = line.strip.split
33
+ if !element.include?("#")
34
+ # Set new net
35
+ if counter == 0
36
+ count += 1
37
+ checkoption = false
38
+ checkhost = false
39
+ checkpool = true
40
+ checksub = true
41
+ object["net#{count}"] = { "subnet" => "",
42
+ "option" => "",
43
+ "pool" => ""
44
+ }
45
+
46
+ end
47
+
48
+ # Filter subnet
49
+ last = line.strip.slice(-1,1)
50
+ checkoption = true if !checksub
51
+ checkhost = true if !checkpool
52
+ checkpool = true if
53
+ if last.eql?("{")
54
+ counter -= 1
55
+ if counter == -1
56
+ object["net#{count}"]["subnet"] = line.gsub("\{\n","")
57
+ checksub = false
58
+ end
59
+ if counter == -2
60
+ checkpool = false
61
+ end
62
+ elsif last.eql?("}")
63
+ counter += 1
64
+ end
65
+
66
+ # Get data
67
+ if counter == -1 && checkoption
68
+ object["net#{count}"]["option"] = object["net#{count}"]["option"] + "#{line}"
69
+ elsif checkhost
70
+ object["net#{count}"]["pool"] = object["net#{count}"]["pool"] + "#{line}"
71
+ end
72
+ end
73
+ end
74
+ end
75
+ file.close
76
+ rescue => err
77
+ puts "Exception: #{err}"
78
+ err
79
+ end
80
+
81
+ return object
82
+ end
83
+
84
+ # Get subnet and netmask
85
+ def self.get_sub_mask(subnet)
86
+ if subnet.nil?
87
+ return false
88
+ else
89
+ array = subnet["subnet"].split
90
+ address = { "#{array[0]}" => array[1],
91
+ "#{array[2]}" => array[3] }
92
+ end
93
+ end
94
+
95
+ def self.get_subnet(subnet)
96
+ if subnet.nil?
97
+ return false
98
+ else
99
+ array = subnet["subnet"].split
100
+ address = array[1]
101
+ end
102
+ end
103
+
104
+ def self.get_netmask(subnet)
105
+ if subnet.nil?
106
+ return false
107
+ else
108
+ array = subnet["subnet"].split
109
+ address = array[3]
110
+ end
111
+ end
112
+
113
+ def self.get_authoritative(subnet)
114
+ if subnet.nil?
115
+ return false
116
+ else
117
+ authori = Parse_Dhcp::DHCP.get_list_option(subnet)
118
+ if !authori["authoritative"].nil?
119
+ return true
120
+ else
121
+ return false
122
+ end
123
+ end
124
+ end
125
+
126
+ # Get all config option of subnet
127
+ def self.get_list_option(subnet, condition = false)
128
+ if subnet.nil?
129
+ return false
130
+ else
131
+ option = {}
132
+ differ = {}
133
+ i = 0
134
+ line_number = subnet["option"].lines.count
135
+ if !condition
136
+ while i < line_number do
137
+ if !subnet["option"].lines[i].strip.eql?("")
138
+ substring = subnet["option"].lines[i].gsub("\;","")
139
+ array = substring.split
140
+ if array.include?("option")
141
+ option["#{array[1]}"] = "#{array[2]}"
142
+ elsif array.include?("authoritative")
143
+ option["#{array[0]}"] = true
144
+ else
145
+ option["#{array[0]}"] = "#{array[1]}"
146
+ end
147
+ end
148
+ i += 1
149
+ end
150
+
151
+ # Delete trash element
152
+ option.delete("}")
153
+
154
+ return option
155
+ else
156
+ while i < line_number do
157
+ if !subnet["option"].lines[i].strip.eql?("")
158
+ substring = subnet["option"].lines[i].gsub("\;","")
159
+ array = substring.split
160
+ if array.include?("option")
161
+ option["#{array[1]}"] = "#{array[2]}"
162
+ elsif array.include?("authoritative")
163
+ differ["#{array[0]}"] = true
164
+ else
165
+ differ["#{array[0]}"] = "#{array[1]}"
166
+ end
167
+ end
168
+ i += 1
169
+ end
170
+
171
+ # Delete trash element
172
+ differ.delete("}")
173
+
174
+ return [option, differ]
175
+ end
176
+ end
177
+ end
178
+
179
+ # Get host. Host is Hash
180
+ def self.get_pool(subnet)
181
+ if subnet.nil?
182
+ return false
183
+ else
184
+ pool = { "hosts" => {} }
185
+ count = 0
186
+ counter = 0
187
+ check_first = true
188
+ checkhost = true
189
+ i = 0
190
+ line_number = subnet["pool"].lines.count
191
+ lines = subnet["pool"].lines
192
+
193
+ while i < line_number do
194
+ if !lines[i].eql?("\n")
195
+ line = lines[i].gsub("\n","")
196
+ # valid block
197
+ last = line.strip.slice(-1,1)
198
+ if last.eql?("{")
199
+ check_first = false
200
+ count += 1
201
+ counter -= 1
202
+ pool["hosts"]["host#{count}"] = {}
203
+ if counter == -1
204
+ item = line.split
205
+ pool["hosts"]["host#{count}"]["#{item[0]}"] = item [1]
206
+ checkhost = false
207
+ end
208
+ elsif last.eql?("}")
209
+ counter += 1
210
+ end
211
+
212
+ # Create new host
213
+ if counter == 0 && !line.eql?("}")
214
+ if check_first
215
+ substring = line.gsub("\;","")
216
+ item = substring.split
217
+ if item.include?("range")
218
+ pool["#{item[0]}"] = { "min" => item[1], "max" => item[2] }
219
+ else
220
+ pool["#{item[0]}"] = item[1]
221
+ end
222
+ end
223
+ end
224
+ # Get data
225
+ if !checkhost
226
+ substring = line.gsub("\;","")
227
+ item = substring.split
228
+ if item.include?("hardware")
229
+ pool["hosts"]["host#{count}"]["#{item[0]}_#{item[1]}"] = item[2]
230
+ else
231
+ pool["hosts"]["host#{count}"]["#{item[0]}"] = item[1]
232
+ end
233
+ end
234
+ end
235
+ i += 1
236
+ end
237
+
238
+ # Delete trash element
239
+ [*1..count].each do |i|
240
+ pool["hosts"]["host#{i}"].tap {|key|
241
+ key.delete("}")
242
+ }
243
+ end
244
+
245
+ return pool
246
+ end
247
+ end
248
+
249
+ # Get list subnet
250
+ def subnets
251
+ subnet = []
252
+ index = 0
253
+ while index < @datas.count
254
+ index += 1
255
+ subnet << Parse_Dhcp::DHCP.get_subnet(@datas["net#{index}"])
256
+ end
257
+ return subnet
258
+ end
259
+
260
+ # Get list netmask
261
+ def netmasks
262
+ netmask = []
263
+ index = 0
264
+ while index < @datas.count
265
+ index += 1
266
+ netmask << Parse_Dhcp::DHCP.get_netmask(@datas["net#{index}"])
267
+ end
268
+ return netmask
269
+ end
270
+
271
+ # Get list option
272
+ def options
273
+ option = []
274
+ index = 0
275
+ while index < @datas.count
276
+ index += 1
277
+ option << Parse_Dhcp::DHCP.get_list_option(@datas["net#{index}"])
278
+ end
279
+ return option
280
+ end
281
+
282
+ # Get value authoritative
283
+ def authoritative
284
+ authori = []
285
+ index = 0
286
+ while index < @datas.count
287
+ index += 1
288
+ authori << Parse_Dhcp::DHCP.get_authoritative(@datas["net#{index}"])
289
+ end
290
+ return authori
291
+ end
292
+
293
+ # Get pool
294
+ def pools
295
+ pool = []
296
+ index = 0
297
+ while index < @datas.count
298
+ index += 1
299
+ data = Parse_Dhcp::DHCP.get_pool(@datas["net#{index}"])
300
+ i = 0
301
+ tmp_hash = {}
302
+ while i < data["hosts"].count
303
+ i += 1
304
+ tmp_hash["#{i}"] = data["hosts"]["host#{i}"]
305
+ end
306
+ pool << tmp_hash
307
+ end
308
+ return pool
309
+ end
310
+
311
+ # Get range
312
+ def ranges
313
+ range = []
314
+ index = 0
315
+ while index < @datas.count
316
+ index += 1
317
+ data = Parse_Dhcp::DHCP.get_pool(@datas["net#{index}"])
318
+ range << "#{data["range"]["min"]} #{data["range"]["max"]}"
319
+ end
320
+ return range
321
+ end
322
+
323
+ # Get allow
324
+ def allow
325
+ allow = []
326
+ index = 0
327
+ while index < @datas.count
328
+ index += 1
329
+ data = Parse_Dhcp::DHCP.get_pool(@datas["net#{index}"])
330
+ if !data["allow"].nil?
331
+ allow << data["allow"]
332
+ end
333
+ end
334
+ return allow
335
+ end
336
+
337
+ # Get allow
338
+ def denny
339
+ denny = []
340
+ index = 0
341
+ while index < @datas.count
342
+ index += 1
343
+ data = Parse_Dhcp::DHCP.get_pool(@datas["net#{index}"])
344
+ if !data["denny"].nil?
345
+ denny << data["denny"]
346
+ end
347
+ end
348
+ return denny
349
+ end
350
+
351
+ # Return data in file
352
+ def data
353
+ @datas
354
+ end
355
+
356
+ # Set data in object
357
+ def net
358
+
359
+ i = 0
360
+ while i < @datas.count
361
+ i += 1
362
+ new_net = Net.new
363
+ new_net.subnet = Parse_Dhcp::DHCP.get_subnet(@datas["net#{i}"])
364
+ new_net.netmask = Parse_Dhcp::DHCP.get_netmask(@datas["net#{i}"])
365
+
366
+ list_option = Parse_Dhcp::DHCP.get_list_option(@datas["net#{i}"], true)
367
+ new_net.option = list_option[0]
368
+ new_net.differ = list_option[1]
369
+
370
+ pool = Parse_Dhcp::DHCP.get_pool(@datas["net#{i}"])
371
+ new_net.pool["range"] = pool["range"]
372
+ new_net.pool["allow"] = pool["allow"]
373
+ new_net.pool["denny"] = pool["denny"]
374
+ # set host
375
+ index = 0
376
+ while index < pool["hosts"].count
377
+ index += 1
378
+ host_name = pool["hosts"]["host#{index}"]["host"]
379
+ ethernet = pool["hosts"]["host#{index}"]["hardware_ethernet"]
380
+ address = pool["hosts"]["host#{index}"]["fixed-address"]
381
+ host = Host.new(host_name, ethernet, address)
382
+ new_net.pool["hosts"] << host
383
+ end
384
+ @array_net << new_net
385
+ end
386
+ return @array_net
387
+ end
388
+
389
+ # Write file
390
+ def write(file_name, arr_net)
391
+ if !arr_net.empty?
392
+ result = WriteConf.write_file(file_name, arr_net)
393
+ end
394
+ end
395
+ end
396
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'parse_dhcp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "parse_dhcp"
8
+ spec.version = ParseDhcp::VERSION
9
+ spec.authors = ["Nguyen hoang anh"]
10
+ spec.email = ["anhnh@vccloud.vn"]
11
+
12
+ spec.summary = %q{Parser file dhcp.conf}
13
+ spec.description = %q{Parser file dhcp.conf}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parse_dhcp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nguyen hoang anh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Parser file dhcp.conf
42
+ email:
43
+ - anhnh@vccloud.vn
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - examples/default_dhcp.conf
57
+ - examples/test1_dhcp.conf
58
+ - examples/test2_dhcp.conf
59
+ - examples/test_dhcp.conf
60
+ - lib/parse_dhcp.rb
61
+ - lib/parse_dhcp/host.rb
62
+ - lib/parse_dhcp/net.rb
63
+ - lib/parse_dhcp/version.rb
64
+ - lib/parse_dhcp/writeconf.rb
65
+ - parse_dhcp.gemspec
66
+ homepage: ''
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.4.8
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Parser file dhcp.conf
90
+ test_files: []
91
+ has_rdoc: