Skip to content

Instantly share code, notes, and snippets.

@ebisawa
Created December 20, 2012 03:46
Show Gist options
  • Save ebisawa/4342804 to your computer and use it in GitHub Desktop.
Save ebisawa/4342804 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'date'
require 'time'
LEASES = '/var/lib/dhcpd/dhcpd.leases'
def parse_lease_state(lease_state, lines)
state = {}
while (l = lines.shift) != nil
if l =~ /\s*\}\s*/
lease_state.merge!(state)
return
end
if l =~ /^\s*ends \d+ (.+);/
t_now = DateTime.now
t_end = DateTime.parse($1)
return if t_end < t_now
state[:lease_end] = t_end.new_offset(t_now.offset)
end
if l =~ /^\s*binding state ([a-z]+)/
state[:state] = $1
end
if l =~ /^\s*hardware ethernet (.+);/
state[:macaddr] = $1
end
if l =~ /^\s*client-hostname "(.+)";/
state[:hostname] = $1
end
end
end
lease_state = {}
open(LEASES) do |io|
lines = io.readlines
while (l = lines.shift) != nil
if l =~ /lease (\d+\.\d+\.\d+\.\d+) \{/
ipaddr = $1
lease_state[ipaddr] ||= {}
parse_lease_state(lease_state[ipaddr], lines)
end
end
end
puts "addr_________ expire_____________ leased to_________________________"
lease_state.keys.sort.each do |ipaddr|
s = lease_state[ipaddr]
next if s.keys.size == 0
t = s[:lease_end].strftime('%Y-%m-%d %H:%M:%S')
puts "#{ipaddr % "%-16s"} #{t} #{s[:macaddr]} #{s[:hostname]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment