Created
July 16, 2012 07:26
-
-
Save sgirones/3121350 to your computer and use it in GitHub Desktop.
ABIQUO - Recreate a DHCP lease based on the MAC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
#Check if the commandline param is a MAC address | |
if not /^(\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2})?$/.match("#{ARGV[0]}") | |
puts "#{ARGV[0]} is not a valid mac" | |
exit | |
end | |
def self.ip_to_hex ip | |
return if not ip | |
ip.split(".").map{|s| "%.2x" % s.to_i}.join(":") | |
end | |
#Get lease name, ip, mac and netmask | |
res = `mysql kinton -e "select ipm.name, ipm.ip, ipm.mac, nc.netmask from ip_pool_management ipm, rasd_management rm, virtualmachine vm, vlan_network vn, network_configuration nc WHERE rm.idManagement = ipm.idManagement AND rm.idVM = vm.idVM AND ipm.vlan_network_id = vn.vlan_network_id AND vn.network_configuration_id = nc.network_configuration_id AND ipm.mac = \\"#{ARGV[0]}\\";"` | |
exit if $? != 0 or res.size == 0 | |
lease_name, ip, mac, netmask = res.split("\n")[1].split | |
#Get gateway and DNS info only if this lease represents the principal NIC | |
res = `mysql kinton -e "select nc.gateway, nc.primary_dns, nc.secondary_dns from ip_pool_management ipm, rasd_management rm, virtualmachine vm, network_configuration nc, vlan_network vn WHERE rm.idManagement = ipm.idManagement AND rm.idVM = vm.idVM AND vm.network_configuration_id = nc.network_configuration_id AND vn.network_configuration_id = nc.network_configuration_id AND vn.vlan_network_id = ipm.vlan_network_id AND ipm.mac = \\"#{ARGV[0]}\\";"` | |
exit if $? != 0 | |
gateway, dns1, dns2 = res.split("\n")[1].split if res.size > 0 | |
#Generate the lease content | |
lease = """host #{lease_name} { | |
dynamic; | |
hardware ethernet #{mac}; | |
fixed-address #{ip}; | |
supersede subnet-mask = #{ip_to_hex(netmask)}; | |
""" | |
lease += " supersede routers = #{ip_to_hex(gateway)};\n" if gateway | |
if dns1 | |
if dns2 | |
lease += " supersede domain-name-servers = concat (#{[ip_to_hex(dns1), ip_to_hex(dns2)].join(',')});\n" | |
else | |
lease += " supersede domain-name-servers = concat (#{ip_to_hex(dns1)});\n" | |
end | |
elsif dns2 | |
lease += " supersede domain-name-servers = concat (#{ip_to_hex(dns2)});\n" | |
end | |
lease += "}" | |
puts lease |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment