dhcp_parser 0.2.0 → 0.3.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: 5134862f8b0303ff92a48a95c63d377841e062f8
4
- data.tar.gz: 10263ffd6dc2ee13fb981c3d4c46b9c772eecb64
3
+ metadata.gz: 45c02bbf595dabb546c4d7f52dea4651b75a360b
4
+ data.tar.gz: 4119da3631add28e7ad0f138da16d6993941859e
5
5
  SHA512:
6
- metadata.gz: 04c93b5f0b9bf0eaf5ae20d74a5c34487e2bc82a14465114b72958e259db8c77742d1b2e75c8b0d04fdea8f0274a980666427f06dbe88a30e536b119e6682e9e
7
- data.tar.gz: 6df6a49a70713e42225d5eaf30ee2b8046cc008eed7d628d63d067b3843538e0a530f30f232b86c944df6eaff766a5d42433644941de9f683c792975b4061d71
6
+ metadata.gz: 7351f33960e5b9c5babb1ab2230995291561e661f69bcfb846f0a881c61765e4e0eab09463a79528701ddcfa241f06203d5a6fca3c06705d5d4e12d10186f499
7
+ data.tar.gz: c90f6db16449c4003107b5ecebdba776acbcaff01778993630ba14ba54aa342857a5539d81a4699cb4e8dc4699cea5230d87f3a043dc4cd8c3b7ea19b0db5241
data/README.md CHANGED
@@ -22,9 +22,7 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- _1. Read file__
26
-
27
- __With file dhcp.conf__
25
+ #### With file dhcp.conf
28
26
 
29
27
  ```ruby
30
28
  subnet 192.168.1.0 netmask 255.255.255.0 {
@@ -84,7 +82,7 @@ subnet 10.152.187.0 netmask 255.255.255.0 {
84
82
  }
85
83
  ```
86
84
 
87
- __1. Read file__
85
+ #### 1. Read file
88
86
 
89
87
  * The first, create object dhcp with param: the path to file config
90
88
 
@@ -200,14 +198,53 @@ array_net[0]
200
198
 
201
199
  ```
202
200
 
203
- __2. Write file__
201
+ * Set array net
202
+
203
+ ```ruby
204
+ #
205
+ # Create object
206
+ net1 = Net.new
207
+ net2 = Net.new
208
+
209
+ # the object's attribute list
210
+ subnet = ""
211
+ netmask = ""
212
+ option = {}
213
+ differ = {}
214
+ pool = { "range" => "",
215
+ "allow" => "",
216
+ "denny" => "",
217
+ "hosts" => []
218
+ }
219
+
220
+ # Set attributes:
221
+ net1.attribute
222
+ net2.attribute
223
+ ```
224
+
225
+ #### 2. Write file
204
226
 
205
227
  Create object net, then set attribute for object. Then call method write_file in module WriteConf with param: "path/file_name", "array_net"
206
228
 
207
229
  ```ruby
208
- result = dhcp.write_file_conf(path/file_name, array_net)
209
- # or
210
- result = WriteConf.write_file_conf(path/file_name, array_net)
230
+ array_net = dhcp.net
231
+ result = dhcp.write_file_conf(path/file_name.type, array_net)
232
+ # Success
233
+ => true
234
+ # Fail
235
+ => false
236
+ ```
237
+
238
+ #### 3. Convert to XML and write file xml
239
+
240
+ ```ruby
241
+ # Convert to XML
242
+ array_net = dhcp.net
243
+ xml = dhcp.to_xml(array_net)
244
+
245
+ # Write file
246
+ result = dhcp.write_file_xml(path/filename.type, xml)
247
+
211
248
  # Success
212
249
  => true
213
250
  # Fail
Binary file
data/dhcp_parser.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Nguyen hoang anh"]
10
10
  spec.email = ["anhnh@vccloud.vn"]
11
11
 
12
- spec.summary = %q{Parser file dhcp.conf.}
13
- spec.description = %q{Parser file dhcp.conf.}
12
+ spec.summary = %q{Parser file dhcp.conf}
13
+ spec.description = %q{Update convert file config to xml and write file xml.}
14
14
  spec.homepage = "https://github.com/hoanganhhanoi/dhcp_parser"
15
15
  spec.license = "MIT"
16
16
 
@@ -0,0 +1,68 @@
1
+ <subnets>
2
+ <subnet>
3
+ <net>192.168.1.0</net>
4
+ <mask>255.255.255.0</mask>
5
+ <option>
6
+ <routers>192.168.1.1</routers>
7
+ <subnet-mask>255.255.255.0</subnet-mask>
8
+ <broadcast-address>192.168.1.255</broadcast-address>
9
+ <domain-name-servers>194.168.4.100</domain-name-servers>
10
+ <ntp-servers>192.168.1.1</ntp-servers>
11
+ <netbios-name-servers>192.168.1.1</netbios-name-servers>
12
+ </option>
13
+ <default-lease-time>60400</default-lease-time>
14
+ <max-lease-time>60400</max-lease-time>
15
+ <authoritative>true</authoritative>
16
+ <pool>
17
+ <range>
18
+ <min>192.168.25.20</min>
19
+ <max>192.168.25.200</max>
20
+ </range>
21
+ <allow>unknown-clients</allow>
22
+ <denny></denny>
23
+ <hosts>
24
+ <host>
25
+ <name>bla1</name>
26
+ <ethernet>DD:GH:DF:E5:F7:D7</ethernet>
27
+ <address>192.168.1.2</address>
28
+ </host>
29
+ <host>
30
+ <name>bla2</name>
31
+ <ethernet>00:JJ:YU:38:AC:45</ethernet>
32
+ <address>192.168.1.20</address>
33
+ </host>
34
+ </hosts>
35
+ </pool>
36
+ </subnet>
37
+ <subnet>
38
+ <net>10.152.187.0</net>
39
+ <mask>255.255.255.255</mask>
40
+ <option>
41
+ <routers>10.152.187.1</routers>
42
+ <subnet-mask>255.255.255.255</subnet-mask>
43
+ <broadcast-address>10.150.168.255</broadcast-address>
44
+ <domain-name-servers>192.168.4.101</domain-name-servers>
45
+ <ntp-servers>10.152.187.10</ntp-servers>
46
+ <netbios-name-servers>10.152.187.12</netbios-name-servers>
47
+ <netbios-node-type>2</netbios-node-type>
48
+ </option>
49
+ <authoritative>false<authoritative>
50
+ <default-lease-time>86400</default-lease-time>
51
+ <max-lease-time>86400</max-lease-time>
52
+ <pool>
53
+ <range>
54
+ <min>192.168.25.20</min>
55
+ <max>192.168.25.30</max>
56
+ </range>
57
+ <allow></allow>
58
+ <denny>unknown-clients</denny>
59
+ <hosts>
60
+ <host>
61
+ <name>bla3</name>
62
+ <ethernet>00:KK:HD:66:55:9B</ethernet>
63
+ <address>10.152.187.2</address>
64
+ </host>
65
+ </hosts>
66
+ </pool>
67
+ </subnet>
68
+ </subnets>
@@ -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,67 @@
1
+ <subnets>
2
+ <subnet>
3
+ <net>192.168.1.0</net>
4
+ <mask>255.255.255.0</mask>
5
+ <option>
6
+ <routers>192.168.1.1</routers>
7
+ <subnet-mask>255.255.255.0</subnet-mask>
8
+ <broadcast-address>192.168.1.255</broadcast-address>
9
+ <domain-name-servers>194.168.4.100</domain-name-servers>
10
+ </option>
11
+ <authoritative>true</authoritative>
12
+ <default-lease-time>2</default-lease-time>
13
+ <max-lease-time>86400</max-lease-time>
14
+ <pool>
15
+ <range>
16
+ <min>192.168.25.20</min>
17
+ <max>192.168.25.200</max>
18
+ </range>
19
+ <allow>unknown-clients</allow>
20
+ <denny></denny>
21
+ <hosts>
22
+ <host>
23
+ <name>bla1</name>
24
+ <ethernet>DD:GH:DF:E5:F7:D7</ethernet>
25
+ <address>192.168.1.2</address>
26
+ </host>
27
+ <host>
28
+ <name>bla2</name>
29
+ <ethernet>00:JJ:YU:38:AC:45</ethernet>
30
+ <address>192.168.1.20</address>
31
+ </host>
32
+ </hosts>
33
+ </pool>
34
+ </subnet>
35
+ <subnet>
36
+ <net>192.168.1.0</net>
37
+ <mask>255.255.255.0</mask>
38
+ <option>
39
+ <routers>192.168.1.1</routers>
40
+ <subnet-mask>255.255.255.0</subnet-mask>
41
+ <broadcast-address>192.168.1.255</broadcast-address>
42
+ <domain-name-servers>194.168.4.100</domain-name-servers>
43
+ </option>
44
+ <default-lease-time>2</default-lease-time>
45
+ <max-lease-time>86400</max-lease-time>
46
+ <pool>
47
+ <range>
48
+ <min>192.168.25.20</min>
49
+ <max>192.168.25.200</max>
50
+ </range>
51
+ <allow></allow>
52
+ <denny>unknown-clients</denny>
53
+ <hosts>
54
+ <host>
55
+ <name>bla1</name>
56
+ <ethernet>DD:GH:DF:E5:F7:D7</ethernet>
57
+ <address>192.168.1.2</address>
58
+ </host>
59
+ <host>
60
+ <name>bla2</name>
61
+ <ethernet>00:JJ:YU:38:AC:45</ethernet>
62
+ <address>192.168.1.20</address>
63
+ </host>
64
+ </hosts>
65
+ </pool>
66
+ </subnet>
67
+ </subnets>
@@ -0,0 +1,67 @@
1
+ <subnets>
2
+ <subnet>
3
+ <net>192.168.1.0</net>
4
+ <mask>255.255.255.0</mask>
5
+ <option>
6
+ <routers>192.168.1.1</routers>
7
+ <subnet-mask>255.255.255.0</subnet-mask>
8
+ <broadcast-address>192.168.1.255</broadcast-address>
9
+ <domain-name-servers>194.168.4.100</domain-name-servers>
10
+ </option>
11
+ <authoritative>true</authoritative>
12
+ <default-lease-time>2</default-lease-time>
13
+ <max-lease-time>86400</max-lease-time>
14
+ <pool>
15
+ <range>
16
+ <min>192.168.25.20</min>
17
+ <max>192.168.25.200</max>
18
+ </range>
19
+ <allow>unknown-clients</allow>
20
+ <denny></denny>
21
+ <hosts>
22
+ <host>
23
+ <name>bla1</name>
24
+ <ethernet>DD:GH:DF:E5:F7:D7</ethernet>
25
+ <address>192.168.1.2</address>
26
+ </host>
27
+ <host>
28
+ <name>bla2</name>
29
+ <ethernet>00:JJ:YU:38:AC:45</ethernet>
30
+ <address>192.168.1.20</address>
31
+ </host>
32
+ </hosts>
33
+ </pool>
34
+ </subnet>
35
+ <subnet>
36
+ <net>192.168.1.0</net>
37
+ <mask>255.255.255.0</mask>
38
+ <option>
39
+ <routers>192.168.1.1</routers>
40
+ <subnet-mask>255.255.255.0</subnet-mask>
41
+ <broadcast-address>192.168.1.255</broadcast-address>
42
+ <domain-name-servers>194.168.4.100</domain-name-servers>
43
+ </option>
44
+ <default-lease-time>2</default-lease-time>
45
+ <max-lease-time>86400</max-lease-time>
46
+ <pool>
47
+ <range>
48
+ <min>192.168.25.20</min>
49
+ <max>192.168.25.200</max>
50
+ </range>
51
+ <allow></allow>
52
+ <denny>unknown-clients</denny>
53
+ <hosts>
54
+ <host>
55
+ <name>bla1</name>
56
+ <ethernet>DD:GH:DF:E5:F7:D7</ethernet>
57
+ <address>192.168.1.2</address>
58
+ </host>
59
+ <host>
60
+ <name>bla2</name>
61
+ <ethernet>00:JJ:YU:38:AC:45</ethernet>
62
+ <address>192.168.1.20</address>
63
+ </host>
64
+ </hosts>
65
+ </pool>
66
+ </subnet>
67
+ </subnets>
data/lib/dhcp_parser.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require_relative "dhcp_parser/writeconf.rb"
2
+ require_relative "dhcp_parser/xml.rb"
2
3
  require_relative "dhcp_parser/net.rb"
3
4
  require_relative "dhcp_parser/host.rb"
4
5
  require_relative "dhcp_parser/version.rb"
@@ -394,5 +395,15 @@ module DHCPParser
394
395
  result = WriteConf.write_file_conf(file_name, arr_net)
395
396
  end
396
397
  end
398
+
399
+ # Convert xml
400
+ def to_xml(arr_net)
401
+ xml = XMLConvert.to_xml(arr_net)
402
+ end
403
+
404
+ # Write file xml
405
+ def write_file_xml(file_name, xml_string)
406
+ result = XMLConvert.write_file_xml(file_name, xml_string)
407
+ end
397
408
  end
398
409
  end
@@ -1,4 +1,5 @@
1
1
  require_relative "writeconf.rb"
2
+ require_relative "xml.rb"
2
3
  require_relative "host.rb"
3
4
 
4
5
  class Net
@@ -45,6 +46,7 @@ class Net
45
46
  net2.option["subnet-mask"] = "255.255.255.0"
46
47
  net2.option["broadcast-address"] = "192.168.1.255"
47
48
  net2.option["domain-name-servers"] = "194.168.4.100"
49
+ net1.differ["authoritative"] = "true"
48
50
  net2.differ["default-lease-time"] = "2"
49
51
  net2.differ['max-lease-time'] = "86400"
50
52
  # Set pool
@@ -1,3 +1,3 @@
1
1
  module DhcpParser
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -33,7 +33,9 @@ module WriteConf
33
33
  # set differ
34
34
  net.differ.each do |key, value|
35
35
  if key == "authoritative"
36
- data += "\s\s#{key};\n"
36
+ if value
37
+ data += "\s\s#{key};\n"
38
+ end
37
39
  else
38
40
  data += "\s\s#{key}\s\s\s\s\s\s\s\s\s#{value};\n"
39
41
  end
@@ -0,0 +1,75 @@
1
+
2
+ module XMLConvert
3
+
4
+ def self.to_xml(arr_net)
5
+ if arr_net.empty?
6
+ return false
7
+ else
8
+ xml = "<subnets>\n"
9
+
10
+ arr_net.each do |net|
11
+ xml += "\s\s<subnet>\n"
12
+ # Set subnet
13
+ xml += "\s\s\s\s<net>#{net.subnet}</net>\n"
14
+ # Set netmask
15
+ xml += "\s\s\s\s<mask>#{net.netmask}</mask>\n"
16
+
17
+ # Set option
18
+ xml += "\s\s\s\s<option>\n"
19
+ net.option.each do |key, value|
20
+ xml += "\s\s\s\s\s\s<#{key}>#{value}</#{key}>\n"
21
+ end
22
+ xml += "\s\s\s\s</option>\n"
23
+
24
+ # Set differ
25
+ if net.differ["authoritative"].nil?
26
+ xml += "\s\s\s\s<authoritative>false<authoritative>\n"
27
+ end
28
+ net.differ.each do |key, value|
29
+ xml += "\s\s\s\s<#{key}>#{value}</#{key}>\n"
30
+ end
31
+
32
+ # Set pool
33
+ xml += "\s\s\s\s<pool>\n"
34
+ net.pool.each do |key, value|
35
+ if key.eql?("range")
36
+ xml += "\s\s\s\s\s\s<#{key}>\n"
37
+ xml += "\s\s\s\s\s\s\s\s<min>#{value["min"]}</min>\n"
38
+ xml += "\s\s\s\s\s\s\s\s<max>#{value["max"]}</max>\n"
39
+ xml += "\s\s\s\s\s\s</#{key}>\n"
40
+ elsif key.eql?("hosts")
41
+ xml += "\s\s\s\s\s\s<#{key}>\n"
42
+ value.each do |h|
43
+ xml += "\s\s\s\s\s\s\s\s<host>\n"
44
+ xml += "\s\s\s\s\s\s\s\s\s\s<name>#{h.host}</name>\n"
45
+ xml += "\s\s\s\s\s\s\s\s\s\s<ethernet>#{h.hardware_ethernet}</ethernet>\n"
46
+ xml += "\s\s\s\s\s\s\s\s\s\s<address>#{h.fixed_address}</address>\n"
47
+ xml += "\s\s\s\s\s\s\s\s</host>\n"
48
+ end
49
+ xml += "\s\s\s\s\s\s</#{key}>\n"
50
+ else
51
+ xml += "\s\s\s\s\s\s<#{key}>#{value}</#{key}>\n"
52
+ end
53
+ end
54
+ xml += "\s\s\s\s</pool>\n"
55
+
56
+ # end subnet
57
+ xml += "\s\s</subnet>\n"
58
+ end
59
+
60
+ # end subnets
61
+ xml += "</subnets>"
62
+ end
63
+ end
64
+
65
+ def self.write_file_xml(file_name, xml_string)
66
+ if xml_string.empty? && file_name.empty?
67
+ return false
68
+ else
69
+ File.open("#{file_name}", "w+") do |file|
70
+ file.write(xml_string)
71
+ end
72
+ return true
73
+ end
74
+ end
75
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dhcp_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nguyen hoang anh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-25 00:00:00.000000000 Z
11
+ date: 2015-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
- description: Parser file dhcp.conf.
41
+ description: Update convert file config to xml and write file xml.
42
42
  email:
43
43
  - anhnh@vccloud.vn
44
44
  executables: []
@@ -54,18 +54,24 @@ files:
54
54
  - bin/console
55
55
  - bin/setup
56
56
  - dhcp_parser-0.1.0.gem
57
+ - dhcp_parser-0.2.0.gem
57
58
  - dhcp_parser.gemspec
58
59
  - examples/default_dhcp.conf
59
60
  - examples/test1_dhcp.conf
61
+ - examples/test2_xml.xml
60
62
  - examples/test3_dhcp.conf
61
63
  - examples/test5_dhcp.conf
62
64
  - examples/test6_dhcp.conf
65
+ - examples/test7_dhcp.conf
66
+ - examples/test_data.xml
63
67
  - examples/test_dhcp.conf
68
+ - examples/xmlconvert.xml
64
69
  - lib/dhcp_parser.rb
65
70
  - lib/dhcp_parser/host.rb
66
71
  - lib/dhcp_parser/net.rb
67
72
  - lib/dhcp_parser/version.rb
68
73
  - lib/dhcp_parser/writeconf.rb
74
+ - lib/dhcp_parser/xml.rb
69
75
  homepage: https://github.com/hoanganhhanoi/dhcp_parser
70
76
  licenses:
71
77
  - MIT
@@ -86,9 +92,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
92
  version: '0'
87
93
  requirements: []
88
94
  rubyforge_project:
89
- rubygems_version: 2.4.8
95
+ rubygems_version: 2.5.0
90
96
  signing_key:
91
97
  specification_version: 4
92
- summary: Parser file dhcp.conf.
98
+ summary: Parser file dhcp.conf
93
99
  test_files: []
94
100
  has_rdoc: