Class: Parse_Dhcp::DHCP
- Inherits:
-
Object
- Object
- Parse_Dhcp::DHCP
- Defined in:
- lib/parse_dhcp.rb
Instance Attribute Summary collapse
-
#datas ⇒ Object
Returns the value of attribute datas.
-
#net ⇒ Object
Set data in object.
Class Method Summary collapse
- .get_authoritative(subnet) ⇒ Object
-
.get_list_option(subnet, condition = false) ⇒ Object
Get all config option of subnet.
- .get_netmask(subnet) ⇒ Object
-
.get_pool(subnet) ⇒ Object
Get host.
-
.get_sub_mask(subnet) ⇒ Object
Get subnet and netmask.
- .get_subnet(subnet) ⇒ Object
-
.read_file(path) ⇒ Object
Read file config return Net.
Instance Method Summary collapse
-
#allow ⇒ Object
Get allow.
-
#authoritative ⇒ Object
Get value authoritative.
-
#data ⇒ Object
Return data in file.
-
#denny ⇒ Object
Get allow.
-
#initialize(path) ⇒ DHCP
constructor
Function constructor of DHCP.
-
#netmasks ⇒ Object
Get list netmask.
-
#options ⇒ Object
Get list option.
-
#pools ⇒ Object
Get pool.
-
#ranges ⇒ Object
Get range.
-
#subnets ⇒ Object
Get list subnet.
-
#write(file_name, arr_net) ⇒ Object
Write file.
Constructor Details
#initialize(path) ⇒ DHCP
Function constructor of DHCP
11 12 13 14 15 |
# File 'lib/parse_dhcp.rb', line 11 def initialize(path) @datas = Parse_Dhcp::DHCP.read_file(path) @array_net = [] end |
Instance Attribute Details
#datas ⇒ Object
Returns the value of attribute datas.
8 9 10 |
# File 'lib/parse_dhcp.rb', line 8 def datas @datas end |
#net ⇒ Object
Set data in object
357 358 359 |
# File 'lib/parse_dhcp.rb', line 357 def net @net end |
Class Method Details
.get_authoritative(subnet) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/parse_dhcp.rb', line 113 def self.(subnet) if subnet.nil? return false else = Parse_Dhcp::DHCP.get_list_option(subnet) if !["authoritative"].nil? return true else return false end end end |
.get_list_option(subnet, condition = false) ⇒ Object
Get all config option of subnet
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/parse_dhcp.rb', line 127 def self.get_list_option(subnet, condition = false) if subnet.nil? return false else option = {} differ = {} i = 0 line_number = subnet["option"].lines.count if !condition while i < line_number do if !subnet["option"].lines[i].strip.eql?("") substring = subnet["option"].lines[i].gsub("\;","") array = substring.split if array.include?("option") option["#{array[1]}"] = "#{array[2]}" elsif array.include?("authoritative") option["#{array[0]}"] = true else option["#{array[0]}"] = "#{array[1]}" end end i += 1 end # Delete trash element option.delete("}") return option else while i < line_number do if !subnet["option"].lines[i].strip.eql?("") substring = subnet["option"].lines[i].gsub("\;","") array = substring.split if array.include?("option") option["#{array[1]}"] = "#{array[2]}" elsif array.include?("authoritative") differ["#{array[0]}"] = true else differ["#{array[0]}"] = "#{array[1]}" end end i += 1 end # Delete trash element differ.delete("}") return [option, differ] end end end |
.get_netmask(subnet) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/parse_dhcp.rb', line 104 def self.get_netmask(subnet) if subnet.nil? return false else array = subnet["subnet"].split address = array[3] end end |
.get_pool(subnet) ⇒ Object
Get host. Host is Hash
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/parse_dhcp.rb', line 180 def self.get_pool(subnet) if subnet.nil? return false else pool = { "hosts" => {} } count = 0 counter = 0 check_first = true checkhost = true i = 0 line_number = subnet["pool"].lines.count lines = subnet["pool"].lines while i < line_number do if !lines[i].eql?("\n") line = lines[i].gsub("\n","") # valid block last = line.strip.slice(-1,1) if last.eql?("{") check_first = false count += 1 counter -= 1 pool["hosts"]["host#{count}"] = {} if counter == -1 item = line.split pool["hosts"]["host#{count}"]["#{item[0]}"] = item [1] checkhost = false end elsif last.eql?("}") counter += 1 end # Create new host if counter == 0 && !line.eql?("}") if check_first substring = line.gsub("\;","") item = substring.split if item.include?("range") pool["#{item[0]}"] = { "min" => item[1], "max" => item[2] } else pool["#{item[0]}"] = item[1] end end end # Get data if !checkhost substring = line.gsub("\;","") item = substring.split if item.include?("hardware") pool["hosts"]["host#{count}"]["#{item[0]}_#{item[1]}"] = item[2] else pool["hosts"]["host#{count}"]["#{item[0]}"] = item[1] end end end i += 1 end # Delete trash element [*1..count].each do |i| pool["hosts"]["host#{i}"].tap {|key| key.delete("}") } end return pool end end |
.get_sub_mask(subnet) ⇒ Object
Get subnet and netmask
85 86 87 88 89 90 91 92 93 |
# File 'lib/parse_dhcp.rb', line 85 def self.get_sub_mask(subnet) if subnet.nil? return false else array = subnet["subnet"].split address = { "#{array[0]}" => array[1], "#{array[2]}" => array[3] } end end |
.get_subnet(subnet) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/parse_dhcp.rb', line 95 def self.get_subnet(subnet) if subnet.nil? return false else array = subnet["subnet"].split address = array[1] end end |
.read_file(path) ⇒ Object
Read file config return Net. Net is hash
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/parse_dhcp.rb', line 18 def self.read_file(path) str = "" count = 0 counter = 0 object = Hash.new begin if path.nil? || path.empty? path = "../examples/default_dhcp.conf" end file = File.new("#{path}", "r") while (line = file.gets) if !line.eql?("\n") && !line.eql?("") element = line.strip.split if !element.include?("#") # Set new net if counter == 0 count += 1 checkoption = false checkhost = false checkpool = true checksub = true object["net#{count}"] = { "subnet" => "", "option" => "", "pool" => "" } end # Filter subnet last = line.strip.slice(-1,1) checkoption = true if !checksub checkhost = true if !checkpool checkpool = true if if last.eql?("{") counter -= 1 if counter == -1 object["net#{count}"]["subnet"] = line.gsub("\{\n","") checksub = false end if counter == -2 checkpool = false end elsif last.eql?("}") counter += 1 end # Get data if counter == -1 && checkoption object["net#{count}"]["option"] = object["net#{count}"]["option"] + "#{line}" elsif checkhost object["net#{count}"]["pool"] = object["net#{count}"]["pool"] + "#{line}" end end end end file.close rescue => err puts "Exception: #{err}" err end return object end |
Instance Method Details
#allow ⇒ Object
Get allow
324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/parse_dhcp.rb', line 324 def allow allow = [] index = 0 while index < @datas.count index += 1 data = Parse_Dhcp::DHCP.get_pool(@datas["net#{index}"]) if !data["allow"].nil? allow << data["allow"] end end return allow end |
#authoritative ⇒ Object
Get value authoritative
283 284 285 286 287 288 289 290 291 |
# File 'lib/parse_dhcp.rb', line 283 def = [] index = 0 while index < @datas.count index += 1 << Parse_Dhcp::DHCP.(@datas["net#{index}"]) end return end |
#data ⇒ Object
Return data in file
352 353 354 |
# File 'lib/parse_dhcp.rb', line 352 def data @datas end |
#denny ⇒ Object
Get allow
338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/parse_dhcp.rb', line 338 def denny denny = [] index = 0 while index < @datas.count index += 1 data = Parse_Dhcp::DHCP.get_pool(@datas["net#{index}"]) if !data["denny"].nil? denny << data["denny"] end end return denny end |
#netmasks ⇒ Object
Get list netmask
261 262 263 264 265 266 267 268 269 |
# File 'lib/parse_dhcp.rb', line 261 def netmasks netmask = [] index = 0 while index < @datas.count index += 1 netmask << Parse_Dhcp::DHCP.get_netmask(@datas["net#{index}"]) end return netmask end |
#options ⇒ Object
Get list option
272 273 274 275 276 277 278 279 280 |
# File 'lib/parse_dhcp.rb', line 272 def option = [] index = 0 while index < @datas.count index += 1 option << Parse_Dhcp::DHCP.get_list_option(@datas["net#{index}"]) end return option end |
#pools ⇒ Object
Get pool
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/parse_dhcp.rb', line 294 def pools pool = [] index = 0 while index < @datas.count index += 1 data = Parse_Dhcp::DHCP.get_pool(@datas["net#{index}"]) i = 0 tmp_hash = {} while i < data["hosts"].count i += 1 tmp_hash["#{i}"] = data["hosts"]["host#{i}"] end pool << tmp_hash end return pool end |
#ranges ⇒ Object
Get range
312 313 314 315 316 317 318 319 320 321 |
# File 'lib/parse_dhcp.rb', line 312 def ranges range = [] index = 0 while index < @datas.count index += 1 data = Parse_Dhcp::DHCP.get_pool(@datas["net#{index}"]) range << "#{data["range"]["min"]} #{data["range"]["max"]}" end return range end |
#subnets ⇒ Object
Get list subnet
250 251 252 253 254 255 256 257 258 |
# File 'lib/parse_dhcp.rb', line 250 def subnets subnet = [] index = 0 while index < @datas.count index += 1 subnet << Parse_Dhcp::DHCP.get_subnet(@datas["net#{index}"]) end return subnet end |
#write(file_name, arr_net) ⇒ Object
Write file
390 391 392 393 394 |
# File 'lib/parse_dhcp.rb', line 390 def write(file_name, arr_net) if !arr_net.empty? result = WriteConf.write_file(file_name, arr_net) end end |