Skip to content

Instantly share code, notes, and snippets.

@codec
Created August 24, 2011 11:42
Show Gist options
  • Save codec/1167893 to your computer and use it in GitHub Desktop.
Save codec/1167893 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'optparse'
require 'csv'
class StaticDhcpLease
attr_accessor :host, :mac, :ip
def initialize(hostname, mac, ip)
@host = hostname
@mac = mac
@ip = ip
end
def to_s
out = "host " + @host + " {\n"
out << " hardware ethernet " + @mac + ";\n"
out << " fixed-address " + @ip + ";\n"
out << "}\n"
out
end
def self.find_by_name(hostname)
r = nil
ObjectSpace.each_object(StaticDhcpLease) { |o|
r = o if o.host == hostname
}
r
end
def self.print_all
ObjectSpace.each_object(StaticDhcpLease) { |o|
puts o
puts "\n"
}
end
end
csvData = Array.new
leases = Array.new
opt = OptionParser.new
input = 'inventory-export-current.csv'
opt.on("--input [FILE]", "-f", String, "Puppet inventory export file (CSV)") do |f|
input = f
end
opt.parse!
CSV.open(input, 'r', ';') do |row|
hostname = row[0]
if StaticDhcpLease.find_by_name(hostname) == nil
StaticDhcpLease.new(hostname, '', '')
else
row[1] =~ /(.*)address/
match = $1
StaticDhcpLease.find_by_name(hostname).send("#{match}=".to_sym, row[2])
end
end
StaticDhcpLease.print_all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment