class DHCPParser::Conf

Attributes

datas[RW]

Public Class Methods

get_authoritative(subnet) click to toggle source
# File lib/dhcp_parser.rb, line 117
def self.get_authoritative(subnet)
  if subnet.nil?
    return false
  else
    authori = DHCPParser::Conf.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/dhcp_parser.rb, line 131
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/dhcp_parser.rb, line 108
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/dhcp_parser.rb, line 184
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/dhcp_parser.rb, line 89
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/dhcp_parser.rb, line 99
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/dhcp_parser.rb, line 14
def initialize(path)
  @datas = DHCPParser::Conf.read_file(path)
  @array_net = []

end
read_file(path) click to toggle source

Read file config return Net. Net is hash

# File lib/dhcp_parser.rb, line 21
def self.read_file(path)
            
  str = ""
  count = 0
  counter = 0
  object = Hash.new

  begin
    if path.nil? || path.empty?
      path = "#{Gem.default_path[1]}/gems/dhcp_parser-#{DhcpParser::VERSION}/examples/default_dhcp.conf"
      # 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/dhcp_parser.rb, line 328
def allow
  allow = []
  index = 0
  while index < @datas.count
    index += 1
    data = DHCPParser::Conf.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/dhcp_parser.rb, line 287
def authoritative
  authori = []
  index = 0
  while index < @datas.count
    index += 1
    authori << DHCPParser::Conf.get_authoritative(@datas["net#{index}"])
  end
  return authori
end
data() click to toggle source

Return data in file

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

Get allow

# File lib/dhcp_parser.rb, line 342
def denny
  denny = []
  index = 0
  while index < @datas.count
    index += 1
    data = DHCPParser::Conf.get_pool(@datas["net#{index}"])
    if !data["denny"].nil?
      denny << data["denny"]
    end
  end
  return denny
end
net() click to toggle source

Set data in object

# File lib/dhcp_parser.rb, line 361
def net

  i = 0
  while i < @datas.count
    i += 1
    new_net = Net.new 
    new_net.subnet  = DHCPParser::Conf.get_subnet(@datas["net#{i}"])
    new_net.netmask = DHCPParser::Conf.get_netmask(@datas["net#{i}"])

    list_option = DHCPParser::Conf.get_list_option(@datas["net#{i}"], true)
    new_net.option  = list_option[0]
    new_net.differ  = list_option[1]

    pool        = DHCPParser::Conf.get_pool(@datas["net#{i}"])
    new_net.pool["range"] = pool["range"]
    new_net.pool["allow"] = pool["allow"]
    new_net.pool["denny"] = pool["denny"]
    # set host
    index = 0 
    while index < pool["hosts"].count
      index += 1
      host_name = pool["hosts"]["host#{index}"]["host"]
      ethernet  = pool["hosts"]["host#{index}"]["hardware_ethernet"]
      address   = pool["hosts"]["host#{index}"]["fixed-address"] 
      host      = Host.new(host_name, ethernet, address)
      new_net.pool["hosts"] << host
    end
    @array_net << new_net
  end
  return @array_net
end
netmasks() click to toggle source

Get list netmask

# File lib/dhcp_parser.rb, line 265
def netmasks
  netmask = []
  index = 0
  while index < @datas.count
    index += 1
    netmask << DHCPParser::Conf.get_netmask(@datas["net#{index}"])
  end
  return netmask
end
options() click to toggle source

Get list option

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

Get pool

# File lib/dhcp_parser.rb, line 298
def pools
  pool  = []
  index = 0
  while index < @datas.count
    index += 1
    data = DHCPParser::Conf.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/dhcp_parser.rb, line 316
def ranges
  range = []
  index = 0
  while index < @datas.count
    index += 1
    data = DHCPParser::Conf.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/dhcp_parser.rb, line 254
def subnets
  subnet = []
  index = 0
  while index < @datas.count
    index += 1
    subnet << DHCPParser::Conf.get_subnet(@datas["net#{index}"])
  end
  return subnet
end
to_xml(arr_net) click to toggle source

Convert xml

# File lib/dhcp_parser.rb, line 401
def to_xml(arr_net)
  xml = XMLConvert.to_xml(arr_net)
end
write_file_conf(file_name, arr_net, condition) click to toggle source

Write file

# File lib/dhcp_parser.rb, line 394
def write_file_conf(file_name, arr_net, condition)
  if !arr_net.empty?
    result = WriteConf.write_file_conf(file_name, arr_net, condition)
  end
end
write_file_xml(file_name, xml_string) click to toggle source

Write file xml

# File lib/dhcp_parser.rb, line 406
def write_file_xml(file_name, xml_string)
  result = XMLConvert.write_file_xml(file_name, xml_string)
end