LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-29-2013, 08:24 PM   #1
Linux.tar.gz
Senior Member
 
Registered: Dec 2003
Location: Paris
Distribution: Slackware forever.
Posts: 2,227

Rep: Reputation: 86
Question Editing parts of dhcpd.conf automatically, how to do that ?


Hi !

I'd like to add or remove parts of dhcpd.conf automatically, i mean without using an editor.

I would like to add or remove that kind of sections:

Code:
host foobar {
hardware ethernet 00:00:00:00:00:00;
fixed-address 12.0.0.5;
option host-name "foobar";
filename "/pxelinux.0";
}
I have a very basic knowledge of sed or awk, and i don't figure out how to do that. Maybe another command would be better suited ?

I suppose the way to do that would be:
Remove all between (and including) parts "host foobar" and "}", with .

And for include:
include that block after "option routers 12.0.0.254;"
 
Old 03-30-2013, 03:45 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
I would do it with Perl, because it allows you to use something like the string 'host' as the record separator. If you can contrive the config file to have a predictable format, you can then just delete a matching record (say, by hostname), and write it back to the file (use -ip option for in-place editing). Most DHCP config files use a series of 'host' stanzas at the end, so including new ones can be a simple append.

--- rod.
 
Old 03-31-2013, 11:40 AM   #3
Linux.tar.gz
Senior Member
 
Registered: Dec 2003
Location: Paris
Distribution: Slackware forever.
Posts: 2,227

Original Poster
Rep: Reputation: 86
Interesting, thanks.

I'd like to try it with standard Linux tools too.
 
Old 03-31-2013, 12:11 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Perl is now included in virtually all Linux distros. This one-liner deletes a specified host stanza.
Code:
perl -e '$/="}"; $title="host\\s+$ARGV[0]\\s+{"; while(<>){ if( $_ !~ m/$title/ ){print $_;} }' specifiedHostName dhcpd.conf
Test first, and then add -ip switches to edit the file in-place.

--- rod.
 
Old 03-31-2013, 07:28 PM   #5
Linux.tar.gz
Senior Member
 
Registered: Dec 2003
Location: Paris
Distribution: Slackware forever.
Posts: 2,227

Original Poster
Rep: Reputation: 86
Ok, i'll try, thanks again !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to re-invoke dhcpd after changes were made to dhcpd.conf file. Azazwa Linux - Newbie 3 03-30-2009 04:44 AM
/etc/dhcpd.conf or /ltsp/dhcpd.conf ? maxsanders Ubuntu 1 07-07-2007 06:32 AM
editing dhcpd.conf renaissance Ubuntu 2 06-02-2006 01:58 AM
dhcpd subnet declaration problems in dhcpd.conf vcrispo Linux - Networking 6 07-15-2005 10:32 AM
dhcpd.master or dhcpd.conf rickg Linux - Networking 0 04-11-2002 03:34 PM


All times are GMT -5. The time now is 07:02 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration