3

I am looking for a command that allows me to insert an host inside the dhcpd.conf file without manualy touching that file.

I am using: CentOS release 5.3.

The file looks like this:

subnet 97.129.0.0 netmask 255.255.240.0 {
    deny unknown-clients;
    range 97.129.2.2 97.129.2.254;
    group {
            filename "3M-1M-OKS2016NOV.cm";
            host client1 {
                    hardware ethernet 00:04:0d:0c:0f:0a;
                    }
            host client2 {
                    hardware ethernet a0:be:cd:ea:1d:14;
                    }
            ###Block I wanto to insert
            host client_i_want_to_insert {
                    hardware ethernet e3:ee:ed:ea:1d:e4;
                    }
            ###########
    }}
Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227

2 Answers2

4
  • For answering to your specific string-handling question:

    You can use sed to insert lines before a string in the middle of file, for your use case.

    As such, supposing your file is:

    subnet 97.129.0.0 netmask 255.255.240.0 { deny unknown-clients; range 97.129.2.2 97.129.2.254; group { filename "3M-1M-OKS2016NOV.cm"; host client1 { hardware ethernet 00:04:0d:0c:0f:0a; } host client2 { hardware ethernet a0:be:cd:ea:1d:14; } #### }}

    You can do:

    sed -i '/####/i\ \thost client_i_want_to_insert {\n\thardware ethernet e3:ee:ed:ea:1d:e4;\n\t}\n' /etc/dhcp/dhcpd.conf


However, in the past I also managed a couple of ISP cable modems (I clearly recognise that line filename "3M-1M-OKS2016NOV.cm"; as provisioning a cable modem configuration file).

I have several recommendations to add:

- Having a host definition spanning several lines is not practical. Over time the DHCP file will become unwieldy. For scripting it is

more difficult, both for inserting or removing lines. I recommend doing only it over one line as:

       host client2 { hardware ethernet a0:be:cd:ea:1d:14; }

- The names of hostnames also have to be unique. Either you increment them, or use your customer code *if* a numeric code.
- an alternative, when incrementing the host part, is putting a comment with the customer code *after* the host definition. As an

added bonus, it is more easier to deal with the file, if you need to do it manually to fix some provisioning mistake, or do some rapid intervention.

       host client2 { hardware ethernet a0:be:cd:ea:1d:14; } #_cus234XP_

As such, when deleting a customer, as you are dealing with one-liners, you just need a single grep -v or sed.

Further, ISC DHCP also lets you include files. For not having to insert lines into the middle of a configuration file, you can do:

   subnet 97.129.0.0 netmask 255.255.240.0 {
       deny unknown-clients;
       range 97.129.2.2 97.129.2.254;
       group {
               filename "3M-1M-OKS2016NOV.cm";
               include "customers";
       }}

And then the customers file should be something like:

   host client1 { hardware ethernet 00:04:0d:0c:0f:0a; } #_cus234XP_
   host client2 { hardware ethernet a0:be:cd:ea:1d:14; } #_cus235XZ_

So, then, you just need to append new CM/customers to the end of the file, and not need to deal with sed/awk, at least for adding new customers.


Furthermore, I would advise eyeing other possible solutions for implementing DHCP provisioning for CM modems/customers.

In the past, I wrote provisioning software for dealing with ISC DHCP text files, for a couple of years. There are some limitations to the process:

- Each time a new item is removed/inserted, the service has to be restarted;  
- If by chance you duplicate an host, the service won't restart;
- Any kind of parsing has to be done in text mode, or done in ancillary/duplicate methods;
- If some auxiliary housekeeping is done by hand, it is prone to errors. 

I then discovered docsis_server which is an "hacked" ISC DHCP on top of MySQL for Linux, specifically developed as a provisioning open source middle ware for the cable industry. I ended up writing my provisioning software/web front end on top of it. It was a boon dealing directly with MySQL queries, instead of text files, for interacting with the DHCP service.

Sadly, I think the project is no longer maintained. https://github.com/bschirrmeister/docsis_server

Nowadays, you also have the Kea project from ISC for DHCP, which is worth a look. It seems very interesting to develop on top of it for provisioning schemes. https://kea.isc.org

Kea is an open source software system including DHCPv4, DHCPv6 servers, Dynamic DNS daemon, REST API interface, MySQL, PostgreSQL and Cassandra databases, RADIUS and NETCONF interfaces and related utilities.

Lastly, normally the cable modem control network is a private IP address space in the 10.x.x.x range ; there is no business here with giving public IP addresses to cable modems such as 97.129.2.x as you are using.

P.S. AFAIK, the provisioning solution I wrote on top of docsis_server has been in production for 10 years now.

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
  • 1
    @GAD3R I was searching to that netmask tidbit to delete it, you beat me to it...thanks. – Rui F Ribeiro Jan 05 '19 at 17:56
  • dhcpd file has multiple subnets with multiple filenames, so I have to look for the exact subnet with the group having the exact filename and insert the new client. – Yerry García Jan 06 '19 at 19:08
  • but what I am looking for is a native ISC DHCP SERVER linux command like: dhcp add host "host client_i_want_to_insert {hardware ethernet e3:ee:ed:ea:1d:e4;}" inside "subnet ip_address" with filename "3M-1M-OKS2016NOV.cm" This is just an example code. – Yerry García Jan 06 '19 at 19:18
  • @YerryGarcía I recommend investing more time in questions, that is not obvious from the question itself. Those kind of management "problems" is why I wrote management software, and later on went with `docsis_server` and why it might be worthy investigating kea, that supports other backends besides text. However, some backends /ISP extensions in kea are paid. Supposedly such backend was meant to exist in ISC DHCP, but I suppose was not never extended and fixed(?). Even it worked well (it does not), it would meant keeping the text files on parallel, as it would be run-time modifications only. – Rui F Ribeiro Jan 06 '19 at 20:45
  • @YerryGarcía Truth to be told, there are LDAP extensions to ISC DHCP, but AFAIK they were not never officially fully supported by ISC. These kind of automation stuff involves some planning and programming, and even the question and my answer enters sound in off-topic territory here. Contact me into my profile personal contacts, I can give some very general pointers. – Rui F Ribeiro Jan 06 '19 at 20:54
  • (short answer.......there is not such extension in regular ISCP DHCP. I used SQL INSERT directives with `docsis_server` last time I did such things for the cable industry. I also looked later on briefly at LDAP+ISC DHCP when doing an ISP DHCP cluster setup and found it confusing to handle, and I am no stranger to LDAP ) – Rui F Ribeiro Jan 06 '19 at 20:57
  • I was not remembering the name in previous comments: OMAPI would be an interesting idea 1) if it worked 2) Besides working, it it were not limited to only manipulate objects in run time. Note: the OMAPI notes hint about adding objects in runtime, but it is not able to do it. I think it is capable of deleting (some) objects though. In the meanwhile, ISC is investing in Kea and ISC DHCP will just be maintained...so the possibility of OMAPI being fixed are nil. Nevertheless, keeping only the usefulness of only manipulating runtime data was always an afterthought. – Rui F Ribeiro Jan 06 '19 at 23:16
  • I trying to contact you but i don't see any contact info on your profile. Any ways I am using sed and I have this: sed -n -e :a -e '/subnet 97\.129\.0\.0 netmask 255\.255\.240\.0/,/}/{/filename "3M-1M-OKS2016NOV\.cm";/!{$!{N;ba};};p;}' dhcpd.conf To insert the host wright after that result but I don't know how to do it. – Yerry García Jan 07 '19 at 16:04
2

awk solution

awk '/}}/{$0="\thost client3 {\n\t\thardware ethernet a:b:c;\n\t}\n"$0}1' file
steve
  • 21,582
  • 5
  • 48
  • 75