Help with Perl to change dhcpd.conf file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Perl to change dhcpd.conf file
# 1  
Old 09-15-2011
Help with Perl to change dhcpd.conf file

Hi all,
I am too new for this stuff and i am lost in perl tutorials. I need help to change dhcp entries in .conf file with a perl script.

The file entries are like below :
Code:
host bertha-clp-0 {
    hardware ethernet       AA:0A:A0:00:6c:40;
    fixed-address           10.10.10.72;
    option subnet-mask      255.255.255.0;
    next-server             10.10.10.2;
    option routers          10.10.10.1;
    option extensions-path  "bertha.cfg";
    filename                "/agul.clp;
}
host mona-clp-1 {
    hardware ethernet       00:0A:A0:00:20:CC;
    fixed-address           10.10.10.73;
    option subnet-mask      255.255.255.0;
    next-server             10.10.10.2;
    option routers          10.10.10.1;
    option extensions-path  "bertha.cfg";
    filename                "/agul.clp";
}

I want to change bertha entry - filename portion to kucar.clp with command of " editdhcp.pl bertha kucar"

Hope you can help me
Thanks

Moderator's Comments:
Mod Comment Please use code tags!
# 2  
Old 09-15-2011
Hi,

Test next Perl script:
Code:
$ cat infile
host bertha-clp-0 {
    hardware ethernet       AA:0A:A0:00:6c:40;
    fixed-address           10.10.10.72;
    option subnet-mask      255.255.255.0;
    next-server             10.10.10.2;
    option routers          10.10.10.1;
    option extensions-path  "bertha.cfg";
    filename                "/agul.clp;
}
host mona-clp-1 {
    hardware ethernet       00:0A:A0:00:20:CC;
    fixed-address           10.10.10.73;
    option subnet-mask      255.255.255.0;
    next-server             10.10.10.2;
    option routers          10.10.10.1;
    option extensions-path  "bertha.cfg";
    filename                "/agul.clp";
}
$ cat script.pl
use warnings;
use strict;

@ARGV == 3 or die qq[Usage: perl $0 file entry name\n];

my ( $name, $entry ) = ( pop @ARGV, pop @ARGV );

{ 
        local $/ = "}";
        while ( <> ) {
                if ( /(?i:host)\s+${entry}-(?i:clp)-/ ) {
                        s|(filename\s+"/).+(\.clp)|$1${name}$2|is;
                }

                print;
        }
}
$ perl script.pl
Usage: perl script.pl file entry name
$ perl script.pl infile bertha kucar
host bertha-clp-0 {
    hardware ethernet       AA:0A:A0:00:6c:40;
    fixed-address           10.10.10.72;
    option subnet-mask      255.255.255.0;
    next-server             10.10.10.2;
    option routers          10.10.10.1;
    option extensions-path  "bertha.cfg";
    filename                "/kucar.clp;
}
host mona-clp-1 {
    hardware ethernet       00:0A:A0:00:20:CC;
    fixed-address           10.10.10.73;
    option subnet-mask      255.255.255.0;
    next-server             10.10.10.2;
    option routers          10.10.10.1;
    option extensions-path  "bertha.cfg";
    filename                "/agul.clp";
}

Regards,
Birei
# 3  
Old 09-15-2011
thanks Birei,
the thing is that i cropped other entries in the file to make it short here, so there are entries not only for clp in bertha class.

I tried to modify your script but it`s too complex to understand regexp in script. So for all bertha i want to change agul to kucar in filename portion.

thanks again
# 4  
Old 09-15-2011
Ah, ok. You mean that there are more entries like
Code:
host bertha-clp-0 {

but with different strings of 'clp'.

In that case, the script should change to:
Code:
$ cat script.pl
use warnings;
use strict;

@ARGV == 3 or die qq[Usage: perl $0 file entry name\n];

my ( $name, $entry ) = ( pop @ARGV, pop @ARGV );

{ 
        local $/ = "}";
        while ( <> ) {
                if ( /(?i:host)\s+${entry}/ ) {
                        s|(filename\s+"/).+(\.clp)|$1${name}$2|is;
                }

                print;
        }
}

Removing the part that tries to match '-clp-'. I hope it helps.

Regards,
Birei
# 5  
Old 09-15-2011
yes it worked , thanks a lot but it seems it does not write the changes to the file. how can i do that ? assigning the output to a variable and putting that var into a file ?
# 6  
Old 09-15-2011
The -i switch edits each file in-place. Use it with care. Next example saves the file appending .orig to it and later changes the original:
Code:
$ perl -i.orig script.pl infile bertha kucar

Next command changes original file without backup.
Code:
$ perl -i script.pl infile bertha kucar

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 7  
Old 09-15-2011
And yet another Perl program -

Code:
$
$
$ # Display the content of the file "dhcp.conf"
$
$ cat dhcp.conf
host bertha-clp-0 {
    hardware ethernet       AA:0A:A0:00:6c:40;
    fixed-address           10.10.10.72;
    option subnet-mask      255.255.255.0;
    next-server             10.10.10.2;
    option routers          10.10.10.1;
    option extensions-path  "bertha.cfg";
    filename                "/agul.clp"
}
host mona-clp-1 {
    hardware ethernet       00:0A:A0:00:20:CC;
    fixed-address           10.10.10.73;
    option subnet-mask      255.255.255.0;
    next-server             10.10.10.2;
    option routers          10.10.10.1;
    option extensions-path  "bertha.cfg";
    filename                "/agul.clp";
}
host bertha-clp-0 {
    hardware ethernet       AA:0A:A0:00:6c:40;
    fixed-address           10.10.10.72;
    option subnet-mask      255.255.255.0;
    next-server             10.10.10.2;
    option routers          10.10.10.1;
    option extensions-path  "bertha.cfg";
    filename                "/blah.txt"
}
$
$ # Display the content of the Perl program
$
$ cat -n editdhcp.pl
     1  #perl -w
     2  # Usage: perl editdhcp.pl <host> <file>
     3  use strict;
     4
     5  my $infile  = "dhcp.conf";
     6  my $outfile = "dhcp.conf.upd";
     7  my $start   = 0;
     8  my $host    = $ARGV[0];
     9  my $file    = $ARGV[1];
    10
    11  open (IN, "<", $infile) or die "Can't open $infile for reading: $!";
    12  open (OUT, ">", $outfile) or die "Can't open $outfile for writing: $!";
    13  while (<IN>) {
    14    if (/^host\s*$host-.*$/) {
    15      $start = 1;
    16    } elsif ($start and /^(\s+)filename(\s+)\S+$/) {
    17      $_ = "$1filename$2\"/$file.clp\"\n";
    18      $start = 0;
    19    }
    20    print OUT $_;
    21  }
    22  close (OUT) or die "Can't close $outfile: $!";
    23  close (IN) or die "Can't close $infile: $!";
    24  rename $outfile, $infile
    25
$
$
$ # Run the Perl program
$
$ perl editdhcp.pl bertha kucar
$
$ # Check the "dhcp.conf" file again
$
$ cat dhcp.conf
host bertha-clp-0 {
    hardware ethernet       AA:0A:A0:00:6c:40;
    fixed-address           10.10.10.72;
    option subnet-mask      255.255.255.0;
    next-server             10.10.10.2;
    option routers          10.10.10.1;
    option extensions-path  "bertha.cfg";
    filename                "/kucar.clp"
}
host mona-clp-1 {
    hardware ethernet       00:0A:A0:00:20:CC;
    fixed-address           10.10.10.73;
    option subnet-mask      255.255.255.0;
    next-server             10.10.10.2;
    option routers          10.10.10.1;
    option extensions-path  "bertha.cfg";
    filename                "/agul.clp";
}
host bertha-clp-0 {
    hardware ethernet       AA:0A:A0:00:6c:40;
    fixed-address           10.10.10.72;
    option subnet-mask      255.255.255.0;
    next-server             10.10.10.2;
    option routers          10.10.10.1;
    option extensions-path  "bertha.cfg";
    filename                "/kucar.clp"
}
$
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Changes in dhcpd.conf do not make a difference in DHCP service behaviour

Hi Experts, Our DHCP server currently answers the DHCP Discover requests from ServerX. In our dhcpd.conf file there are parameters defined for ServerX. Now we introduced some additional Servers into the network and want them to get service from the same DHCP server. Similar configuration... (13 Replies)
Discussion started by: ekorgur
13 Replies

2. Shell Programming and Scripting

Listing IPs from the dhcpd.conf

Hy everybody, Within a dhcpd.conf file, we got some fixed IP adresses from 192.168.0.1 - 192.168.0.254. Sample: #ddns-update-style interim; ddns-update-style none; ignore client-updates; deny client-updates; authoritative; #### By red for PXE Booting allow booting; allow bootp; ###... (17 Replies)
Discussion started by: hermouche
17 Replies

3. Shell Programming and Scripting

Change values in .conf file with a script

This is my first time posting here...so be gentle. Suppose I have a test.conf file that contains a value such as a IP. I would like to be able to use the Dialog Utility in Linux to allow me to enter the new IP in a Dialog form...and the results get saved into the test.conf file in the place... (4 Replies)
Discussion started by: calahanp
4 Replies

4. Shell Programming and Scripting

Perl to change a value in a file

Hi, I have a file with the following line open (unit=2, file='t0_p0.DAT')I want to change the value of the above zeros with variables $i and $j using a simple perl one linear. I've tried the following but it doesn't work perl -i -pe "/open/&&s/t\d+_p\d+/t$j_p$i/" fileThanks! (3 Replies)
Discussion started by: lost.identity
3 Replies

5. UNIX for Dummies Questions & Answers

Sendmail not working anymore after resolv.conf change

Hi there, I am having a small issue with the mail function on our controllers. Recently we set up all the boxes as NFS slave servers and mail sending was not affected. We then had to change the servers addresses in resolv.conf and now email is being queued and not being sent. I have restarted... (3 Replies)
Discussion started by: lodey
3 Replies

6. Solaris

basic question on sd.conf and lpc.conf file

Hello Guys, Do we need to configure this file only if we add SAN disk or even if we add local disk, do we need to modify? (4 Replies)
Discussion started by: mokkan
4 Replies

7. Shell Programming and Scripting

perl read and write to conf file

Hi Everyone, There is a perl file: a.pl ============ #!/usr/bin/perl my $config_file = $ARGV; open CONFIG, "$config_file" or die "Program stopping, couldn't open the configuration file '$config_file'.\n"; my $config = join "", <CONFIG>; close CONFIG; eval $config; die "Couldn't... (1 Reply)
Discussion started by: jimmy_y
1 Replies

8. Linux

dhcpd.conf - static route

Hi, I've setup DHCP Server on RH linux AS3 and everything works fine except static routes. They are not getting effected on client systems. My dhcpd.conf: +++++++++++ ddns-update-style interim; ddns-updates off; option domain-name-servers 192.168.116.122; option domain-name... (3 Replies)
Discussion started by: prvnrk
3 Replies

9. Shell Programming and Scripting

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:... (4 Replies)
Discussion started by: sahilb
4 Replies

10. Linux

dhcpd.conf

I have intall a REdhat 9.0 as a server and Ive configure to act as a DHCP however Im having technical problems b/c the file /etc/dhcpd.conf does not exists. I went to the text edit and I created : subnet 192.192.168.100.0 netmask 255.255.255.0 { range 192.168.100.10 192.168.100.150;... (1 Reply)
Discussion started by: keliy1
1 Replies
Login or Register to Ask a Question