Shell/Perl Script to edit dhcpd.conf

Hi,
I need to get a script together to edit the dhcp service configuration file dhcpd.conf.

Mac addresses are defined in classes ex.
class "HOST1" { match if substring (hardware, 1,18)=00:11:11:FF:FF:FF;}
class "HOST2" ...
class "HOST3" ...
...

followed by allow or deny statements:
deny members of "HOST1";
allow members of "HOST2";
...
...

The script should search for the MAC address entered by the user and if found should deny that class by changing allow to deny in the statement for that class and/or deleting the class.
If not found it should create a class with the given MAC address and add a deny statement for that class in the above given format.

I would appreciate any ideas or help...

Thanks

SB

one approach:

  1. read entire file dhcp.conf in an array
  2. then do the operations in that array (adding, editing, removing lines) and then
    3.write the contents of array back to the file

This, of course, is just the logic, the implementation could be "done in more than one way" :wink:

Any detailed ideas about the implementation?

  1. to read the file in an array:
my $dhcp_filename = "/path/to/dhcp.conf";
open (DHCP_FILE, "< $dhcp_filename")  or  die "Can not read file $dhcp_filename : $!";
my @dhcp_file = <DHCP_FILE>;
close (DHCP_FILE);
  1. use the functions pop, push, shift, splice, and unshift to do operations on the array

  2. to write contents of array back to the file:

open (DHCP_FILE, "> $dhcp_filename")  or  die "Can not write to file $dhcp_filename : $!";
print DHCP_FILE @dhcp_file;
close (DHCP_FILE);