class Parse_Dhcp::DHCP

Attributes

datas[RW]
net[RW]

Public Class Methods

get_authoritative(subnet) click to toggle source
# File lib/parse_dhcp.rb, line 113
def self.get_authoritative(subnet)
  if subnet.nil?
    return false
  else
    authori = Parse_Dhcp::DHCP.get_list_option(subnet)
    if !authori["authoritative"].nil?
      return true
    else
      return false
    end
  end
end
get_list_option(subnet, condition = false) click to toggle source

Get all config option of subnet

# 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) click to toggle source
# 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) click to toggle source

Get host. Host is Hash

# 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) click to toggle source

Get subnet and netmask

# 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) click to toggle source
# 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
new(path) click to toggle source

Function constructor of DHCP

# File lib/parse_dhcp.rb, line 11
def initialize(path)
  @datas = Parse_Dhcp::DHCP.read_file(path)
  @array_net = []

end
read_file(path) click to toggle source

Read file config return Net. Net is hash

# 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

Public Instance Methods

allow() click to toggle source

Get allow

# 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() click to toggle source

Get value authoritative

# File lib/parse_dhcp.rb, line 283
def authoritative
  authori = []
  index = 0
  while index < @datas.count
    index += 1
    authori << Parse_Dhcp::DHCP.get_authoritative(@datas["net#{index}"])
  end
  return authori
end
data() click to toggle source

Return data in file

# File lib/parse_dhcp.rb, line 352
def data
  @datas
end
denny() click to toggle source

Get allow

# 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() click to toggle source

Get list netmask

# 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() click to toggle source

Get list option

# File lib/parse_dhcp.rb, line 272
def options
  option = []
  index = 0
  while index < @datas.count
    index += 1
    option << Parse_Dhcp::DHCP.get_list_option(@datas["net#{index}"])
  end
  return option
end
pools() click to toggle source

Get pool

# 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() click to toggle source

Get range

# 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() click to toggle source

Get list subnet

# 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) click to toggle source

Write file

# 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