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.