Centos – modify dhcpd.conf via script to convert whole subnet to DHCP reservations

centosdhcpisc-dhcp

We have a hodgepodge of systems that are either static IP addresses, or DHCP reservations. (We don't hand out any addresses except reserved ones). Some servers have multiple Nics, and ILO cards. Its getting annoying to have to scan the subnet to find free IP's to be able to use, especially because a server might be offline for a few mintues when I perform the scan. Any listing seems to be forgotten about quickly..

I would like to create a reservation for every single IP address in my subnets. That would make things quite a bit easier to manage/view. (and afterwards, look at setting up DHCP to do DNS updates too)

I have run the following command:

sudo arp-scan -I eth0 10.10.10.0/24

And have a list of all IP's and Mac Addresses. I am about to get a list of all host/dns names as well. (and if they don't have a hostname, I will call it ip-last octet, so for 10.10.10.100, "ip-100".). That is easy to do with some work in Open Office Calcl

Is there a way to automagically add reservations to the dhcpd.conf file?

I really don't want to hand edit this, as there are hundreds (multiple subnets).

is there an easy way I can call a command to create a reservation, or import a list from CSV? I could find many ways to do this with Windows DHCP, using Net Sh, but not with my CentOS based DHCP server.

I would rather use an existing way, then have to write my own tool.

TL:DR – I need a way to modify Cento's DHCPd from the command line, like you can in Windows with Netsh

Best Answer

I suppose you are using ISC DHCPd. In this case just add

host client_name {
    hardware ethernet 00:00:de:ad:be:ef;
    fixed-address 10.10.10.100;
}

to your scope in /etc/dhcpd.conf.

Of course, you would not "hand-edit" this but rather create a short script that would spit out the necessary host definitions per scope. Something like that would do:

#!/bin/bash
function hostdef {
        echo host $1 {
        echo -e \\thardware ethernet $2\;
        echo -e \\tfixed-address $3\;
        echo }
        echo
}

cat $1 | while read name mac ip; do hostdef $name $mac $ip; done

calling it with a flat file with contents like this:

ip-100 00:00:de:ad:be:ef 10.10.10.100
ip-101 00:00:0b:ad:be:ef 10.10.10.101
ip-102 00:00:0b:ad:de:ed 10.10.10.102
ip-103 00:00:de:af:be:ef 10.10.10.103

would generate the necessary lines to paste into your config file or write the output to a separate file which you would include in your dhcpd.conf configuration like this:

include "/etc/dhcpd-reservations-10-10-10.conf";