dhcp_parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aad0212f7b2285b14665b4c788dd764e5201f727
4
+ data.tar.gz: 701d1214c32571608a81dcb6f7f5b308b7c93524
5
+ SHA512:
6
+ metadata.gz: 015398db30b0a09e2b8a857447767784485aafbd8c400c7931c7662103906f80144e2dc6092347f3f6c7a384608ca25bbc1c53bb51318596351226d75ac8e443
7
+ data.tar.gz: bb566b6a6e52a89b35af98f761436b45abdb1368519361ce94b96d86b5f6a111439cd26de77963d920debab492390f84192569c80c5df84d2364d737d1c288d0
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 dhcp_parser.gemspec
4
+ gem 'dhcp_parser', :git => 'https://github.com/hoanganhhanoi/dhcp_parser.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,232 @@
1
+ # DhcpParser
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/dhcp_parser`. 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 'dhcp_parser'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install dhcp_parser
22
+
23
+ ## Usage
24
+
25
+ _1. Read file__
26
+
27
+ __With file dhcp.conf__
28
+
29
+ ```ruby
30
+ subnet 192.168.1.0 netmask 255.255.255.0 {
31
+
32
+ # config file
33
+ # config file
34
+ option routers 192.168.1.1;
35
+ option subnet-mask 255.255.255.0;
36
+ option broadcast-address 192.168.1.255;
37
+ option domain-name-servers 194.168.4.100;
38
+ option ntp-servers 192.168.1.1;
39
+ option netbios-name-servers 192.168.1.1;
40
+ option netbios-node-type 2;
41
+ default-lease-time 86400;
42
+ max-lease-time 86400;
43
+ authoritative;
44
+
45
+ # config file
46
+ # config file
47
+ pool {
48
+ # config file
49
+ range 192.168.25.20 192.168.25.200;
50
+ allow unknown-clients;
51
+ host bla1 {
52
+ # comment .......
53
+ hardware ethernet DD:GH:DF:E5:F7:D7;
54
+ fixed-address 192.168.1.2;
55
+ }
56
+ host bla2 {
57
+ # comment .......
58
+ hardware ethernet 00:JJ:YU:38:AC:45;
59
+ fixed-address 192.168.1.20;
60
+ }
61
+ }
62
+ }
63
+
64
+ subnet 10.152.187.0 netmask 255.255.255.0 {
65
+
66
+ option routers 10.152.187.1;
67
+ option subnet-mask 255.255.255.0;
68
+ option broadcast-address 10.150.168.255;
69
+ option domain-name-servers 192.168.4.101;
70
+ option ntp-servers 10.152.187.10;
71
+ option netbios-name-servers 10.152.187.12;
72
+ option netbios-node-type 2;
73
+
74
+ default-lease-time 86400;
75
+ max-lease-time 86400;
76
+ pool {
77
+ range 192.168.25.20 192.168.25.30;
78
+ denny unknown-clients;
79
+ host bla3 {
80
+ hardware ethernet 00:KK:HD:66:55:9B;
81
+ fixed-address 10.152.187.2;
82
+ }
83
+ }
84
+ }
85
+ ```
86
+
87
+ __1. Read file__
88
+
89
+ * The first, create object dhcp with param: the path to file config
90
+
91
+ ```ruby
92
+ dhcp = DHCPParser::Conf.new(path)
93
+ ```
94
+
95
+ * Get subnet
96
+
97
+ ```ruby
98
+ dhcp.subnets
99
+ #Result
100
+ => ["192.168.1.0", "10.152.187.0"]
101
+ ```
102
+
103
+ * Get netmask
104
+
105
+ ```ruby
106
+ dhcp.netmasks
107
+ #Result
108
+ => ["255.255.255.0"]
109
+ ```
110
+
111
+
112
+ * Get list pool
113
+
114
+ ```ruby
115
+ dhcp.pools
116
+ #Result
117
+ => [{"1"=>
118
+ {"host"=>"bla1",
119
+ "hardware_ethernet"=>"DD:GH:DF:E5:F7:D7",
120
+ "fixed-address"=>"192.168.1.2"},
121
+ "2"=>
122
+ {"host"=>"bla2",
123
+ "hardware_ethernet"=>"00:JJ:YU:38:AC:45",
124
+ "fixed-address"=>"192.168.1.20"}},
125
+ {"1"=>
126
+ {"host"=>"bla3",
127
+ "hardware_ethernet"=>"00:KK:HD:66:55:9B",
128
+ "fixed-address"=>"10.152.187.2"}}]
129
+ ```
130
+
131
+ * Get list option
132
+
133
+ ```ruby
134
+ dhcp.options
135
+ => [{"routers"=>"192.168.1.1",
136
+ "subnet-mask"=>"255.255.255.0",
137
+ "broadcast-address"=>"192.168.1.255",
138
+ "domain-name-servers"=>"194.168.4.100",
139
+ "ntp-servers"=>"192.168.1.1",
140
+ "netbios-name-servers"=>"192.168.1.1",
141
+ "netbios-node-type"=>"2",
142
+ "default-lease-time"=>"86400",
143
+ "max-lease-time"=>"86400",
144
+ "authoritative"=>true},
145
+ {"routers"=>"10.152.187.1",
146
+ "subnet-mask"=>"255.255.255.0",
147
+ "broadcast-address"=>"10.150.168.255",
148
+ "domain-name-servers"=>"192.168.4.101",
149
+ "ntp-servers"=>"10.152.187.10",
150
+ "netbios-name-servers"=>"10.152.187.12",
151
+ "netbios-node-type"=>"2",
152
+ "default-lease-time"=>"86400",
153
+ "max-lease-time"=>"86400"}]
154
+ ```
155
+
156
+ * Get range, allow, denny in pool
157
+
158
+ ```ruby
159
+ dhcp.ranges
160
+ #Result
161
+ => ["192.168.25.20 192.168.25.200", "192.168.25.20 192.168.25.30"]
162
+
163
+ dhcp.allow
164
+ #Result
165
+ => ["unknown-clients"]
166
+
167
+ dhcp.denny
168
+ #Result
169
+ => ["unknown-clients"]
170
+ ```
171
+
172
+ * Get data to object array
173
+
174
+ ```ruby
175
+ array_net = dhcp.net
176
+ # Result
177
+ array_net[0]
178
+ => [#<Net:0xb98d1594
179
+ @differ=
180
+ {"default-lease-time"=>"60400",
181
+ "max-lease-time"=>"60400",
182
+ "authoritative"=>true},
183
+ @netmask="255.255.255.0",
184
+ @option=
185
+ {"routers"=>"192.168.1.1",
186
+ "subnet-mask"=>"255.255.255.0",
187
+ "broadcast-address"=>"192.168.1.255",
188
+ "domain-name-servers"=>"194.168.4.100",
189
+ "ntp-servers"=>"192.168.1.1",
190
+ "netbios-name-servers"=>"192.168.1.1"},
191
+ @pool=
192
+ {"range"=>{"min"=>"192.168.25.20", "max"=>"192.168.25.200"},
193
+ "allow"=>"unknown-clients",
194
+ "denny"=>nil,
195
+ "hosts"=>
196
+ [#<Host:0xb98d8a24
197
+ @fix_address="192.168.1.2",
198
+ @hardware_ethernet="DD:GH:DF:E5:F7:D7",
199
+ @host="bla1">,
200
+ #<Host:0xb98d88a8
201
+
202
+ ```
203
+
204
+ __2. Write file__
205
+
206
+ Create object net, then set attribute for object. Then call method write_file in module WriteConf with param: "path/file_name", "array_net"
207
+
208
+ ```ruby
209
+ result = dhcp.write_file_conf(path/file_name, array_net)
210
+ # or
211
+ result = WriteConf.write_file_conf(path/file_name, array_net)
212
+ # Success
213
+ => true
214
+ # Fail
215
+ => false
216
+ ```
217
+
218
+ ## Development
219
+
220
+ 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.
221
+
222
+ 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).
223
+
224
+ ## Contributing
225
+
226
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dhcp_parser.
227
+
228
+
229
+ ## License
230
+
231
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
232
+
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 "dhcp_parser"
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,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dhcp_parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dhcp_parser"
8
+ spec.version = DhcpParser::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 = "https://github.com/hoanganhhanoi/dhcp_parser"
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
@@ -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,44 @@
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
+ host bla1 {
15
+ hardware ethernet DD:GH:DF:E5:F7:D7;
16
+ fixed-address 192.168.1.2
17
+ }
18
+ host bla2 {
19
+ hardware ethernet 00:JJ:YU:38:AC:45;
20
+ fixed-address 192.168.1.20
21
+ }
22
+ }
23
+ }
24
+
25
+ subnet 10.152.187.0 netmask 255.255.255.255 {
26
+ option routers 10.152.187.1;
27
+ option subnet-mask 255.255.255.255;
28
+ option broadcast-address 10.150.168.255;
29
+ option domain-name-servers 192.168.4.101;
30
+ option ntp-servers 10.152.187.10;
31
+ option netbios-name-servers 10.152.187.12;
32
+ option netbios-node-type 2;
33
+ default-lease-time 86400;
34
+ max-lease-time 86400;
35
+ pool {
36
+ range 192.168.25.20 192.168.25.30;
37
+ denny unknown-clients;
38
+ host bla3 {
39
+ hardware ethernet 00:KK:HD:66:55:9B;
40
+ fixed-address 10.152.187.2
41
+ }
42
+ }
43
+ }
44
+
@@ -0,0 +1,43 @@
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
+ 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
+ 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
+ option routers 192.168.1.1;
25
+ option subnet-mask 255.255.255.0;
26
+ option broadcast-address 192.168.1.255;
27
+ option domain-name-servers 194.168.4.100;
28
+ default-lease-time 2;
29
+ max-lease-time 86400;
30
+ pool {
31
+ range 192.168.25.20 192.168.25.200;
32
+ denny 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,88 @@
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
+ host bla1 {
15
+ hardware ethernet DD:GH:DF:E5:F7:D7;
16
+ fixed-address 192.168.1.2
17
+ }
18
+ host bla2 {
19
+ hardware ethernet 00:JJ:YU:38:AC:45;
20
+ fixed-address 192.168.1.20
21
+ }
22
+ }
23
+ }
24
+
25
+ subnet 10.152.187.0 netmask 255.255.255.255 {
26
+ option routers 10.152.187.1;
27
+ option subnet-mask 255.255.255.255;
28
+ option broadcast-address 10.150.168.255;
29
+ option domain-name-servers 192.168.4.101;
30
+ option ntp-servers 10.152.187.10;
31
+ option netbios-name-servers 10.152.187.12;
32
+ option netbios-node-type 2;
33
+ default-lease-time 86400;
34
+ max-lease-time 86400;
35
+ pool {
36
+ range 192.168.25.20 192.168.25.30;
37
+ denny unknown-clients;
38
+ host bla3 {
39
+ hardware ethernet 00:KK:HD:66:55:9B;
40
+ fixed-address 10.152.187.2
41
+ }
42
+ }
43
+ }
44
+
45
+ subnet 192.168.1.0 netmask 255.255.255.0 {
46
+ option routers 192.168.1.1;
47
+ option subnet-mask 255.255.255.0;
48
+ option broadcast-address 192.168.1.255;
49
+ option domain-name-servers 194.168.4.100;
50
+ option ntp-servers 192.168.1.1;
51
+ option netbios-name-servers 192.168.1.1;
52
+ default-lease-time 60400;
53
+ max-lease-time 60400;
54
+ authoritative;
55
+ pool {
56
+ range 192.168.25.20 192.168.25.200;
57
+ allow unknown-clients;
58
+ host bla1 {
59
+ hardware ethernet DD:GH:DF:E5:F7:D7;
60
+ fixed-address 192.168.1.2
61
+ }
62
+ host bla2 {
63
+ hardware ethernet 00:JJ:YU:38:AC:45;
64
+ fixed-address 192.168.1.20
65
+ }
66
+ }
67
+ }
68
+
69
+ subnet 10.152.187.0 netmask 255.255.255.255 {
70
+ option routers 10.152.187.1;
71
+ option subnet-mask 255.255.255.255;
72
+ option broadcast-address 10.150.168.255;
73
+ option domain-name-servers 192.168.4.101;
74
+ option ntp-servers 10.152.187.10;
75
+ option netbios-name-servers 10.152.187.12;
76
+ option netbios-node-type 2;
77
+ default-lease-time 86400;
78
+ max-lease-time 86400;
79
+ pool {
80
+ range 192.168.25.20 192.168.25.30;
81
+ denny unknown-clients;
82
+ host bla3 {
83
+ hardware ethernet 00:KK:HD:66:55:9B;
84
+ fixed-address 10.152.187.2
85
+ }
86
+ }
87
+ }
88
+
@@ -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 set
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 DhcpParser
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,63 @@
1
+ module WriteConf
2
+
3
+ def self.write_file_conf(file_name,nets)
4
+ if nets.nil?
5
+ return false
6
+ else
7
+ data = ""
8
+ nets.each do |net|
9
+ data += WriteConf.set_data(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_data(net)
25
+
26
+ data = "subnet #{net.subnet} netmask #{net.netmask} {\n"
27
+
28
+ # set option
29
+ net.option.each do |key, value|
30
+ data += "\s\soption #{key}\s\s\s\s\s\s\s\s\s #{value};\n"
31
+ end
32
+
33
+ # set differ
34
+ net.differ.each do |key, value|
35
+ if key == "authoritative"
36
+ data += "\s\s#{key};\n"
37
+ else
38
+ data += "\s\s#{key}\s\s\s\s\s\s\s\s\s#{value};\n"
39
+ end
40
+ end
41
+
42
+ # set pool
43
+ data += "\s\spool {\n"
44
+ net.pool.each do |key, value|
45
+ if key.eql?("range")
46
+ data += "\s\s\s\srange #{value["min"]} #{value["max"]};\n"
47
+ elsif key.eql?("hosts")
48
+ value.each do |h|
49
+ data += "\s\s\s\shost #{h.host} {\n"
50
+ data += "\s\s\s\s\s\shardware ethernet #{h.hardware_ethernet};\n"
51
+ data += "\s\s\s\s\s\sfixed-address #{h.fixed_address}\n"
52
+ data += "\s\s\s\s}\n"
53
+ end
54
+ elsif !value.nil? && !value.empty?
55
+ data += "\s\s\s\s#{key} #{value};\n"
56
+ end
57
+ end
58
+ data += "\s\s}\n"
59
+ data += "}\n\n"
60
+
61
+ return data
62
+ end
63
+ end
@@ -0,0 +1,396 @@
1
+ require_relative "dhcp_parser/writeconf.rb"
2
+ require_relative "dhcp_parser/net.rb"
3
+ require_relative "dhcp_parser/host.rb"
4
+
5
+ module DHCPParser
6
+ class Conf
7
+
8
+ attr_accessor :datas
9
+
10
+ # Function constructor of DHCP
11
+ def initialize(path)
12
+ @datas = DHCPParser::Conf.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 = DHCPParser::Conf.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 << DHCPParser::Conf.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 << DHCPParser::Conf.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 << DHCPParser::Conf.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 << DHCPParser::Conf.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 = DHCPParser::Conf.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 = DHCPParser::Conf.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 = DHCPParser::Conf.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 = DHCPParser::Conf.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 = DHCPParser::Conf.get_subnet(@datas["net#{i}"])
364
+ new_net.netmask = DHCPParser::Conf.get_netmask(@datas["net#{i}"])
365
+
366
+ list_option = DHCPParser::Conf.get_list_option(@datas["net#{i}"], true)
367
+ new_net.option = list_option[0]
368
+ new_net.differ = list_option[1]
369
+
370
+ pool = DHCPParser::Conf.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_conf(file_name, arr_net)
391
+ if !arr_net.empty?
392
+ result = WriteConf.write_file_conf(file_name, arr_net)
393
+ end
394
+ end
395
+ end
396
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dhcp_parser
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
+ - dhcp_parser.gemspec
57
+ - examples/default_dhcp.conf
58
+ - examples/test1_dhcp.conf
59
+ - examples/test3_dhcp.conf
60
+ - examples/test5_dhcp.conf
61
+ - examples/test6_dhcp.conf
62
+ - examples/test_dhcp.conf
63
+ - lib/dhcp_parser.rb
64
+ - lib/dhcp_parser/host.rb
65
+ - lib/dhcp_parser/net.rb
66
+ - lib/dhcp_parser/version.rb
67
+ - lib/dhcp_parser/writeconf.rb
68
+ homepage: https://github.com/hoanganhhanoi/dhcp_parser
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.4.8
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Parser file dhcp.conf.
92
+ test_files: []
93
+ has_rdoc: