LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 05-23-2017, 08:45 AM   #1
cdhjrt
Member
 
Registered: Mar 2002
Location: Marysville, WA
Distribution: Redhat, Win2K, Solaris
Posts: 45

Rep: Reputation: 15
pulling unused scopes from dhcpd.conf


I am having a scripting issue. I took over managing DHCP recently and have been tasked with moving to a new system. There are over 10,000 scopes defined on these servers and ~1000 are currently active.
I have identified active scopes from the leases file, I would like to pull them from the DHCPD.conf file. I'm having issues selecting the entire scope and exporting it.

EG: I need to pull scopes VLAN b and VLAN c. I can locate the vlan's based on IP address but I can't quite get the scopes fully selected. I need to select everything between the { and } including any static hosts which also have opening and closing {}.

Any help is appreciated.

# VLAN a
subnet 10.244.89.128 netmask 255.255.255.128 {
range 10.244.89.129 10.244.89.192;
option routers 10.244.89.254;
}
# VLAN b
subnet 10.235.4.0 netmask 255.255.254.0 {
range 10.235.4.6 10.235.4.255;
option routers 10.235.5.254;
host 7test1 {
hardware ethernet 00:00:cc:d8:1a:aa;
fixed-address 10.235.4.121;
}
host test2 {
hardware ethernet 00:00:2b:4c:ef:ab;
fixed-address 10.235.4.170;
}
}

# VLAN c
subnet 10.235.8.0 netmask 255.255.254.0 {
range 10.235.8.6 10.235.8.255;
option routers 10.235.9.254;
}
 
Old 05-24-2017, 06:38 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
I put "grep multiple lines" into my favorite search engine and perused about 5 links to find
Code:
grep -oP '(?s)abc.*?def' filename
where abc is the first string to match, def is the second string to match and (?s) is the modifier that causes the dot to match eol/newline character.
so...
Code:
scasey $ cat lqtext.txt
# VLAN a
subnet 10.244.89.128 netmask 255.255.255.128 {
range 10.244.89.129 10.244.89.192;
option routers 10.244.89.254;
}
# VLAN b
subnet 10.235.4.0 netmask 255.255.254.0 {
range 10.235.4.6 10.235.4.255;
option routers 10.235.5.254;
host 7test1 {
hardware ethernet 00:00:cc:d8:1a:aa;
fixed-address 10.235.4.121;
}
host test2 {
hardware ethernet 00:00:2b:4c:ef:ab;
fixed-address 10.235.4.170;
}
}

# VLAN c
subnet 10.235.8.0 netmask 255.255.254.0 {
range 10.235.8.6 10.235.8.255;
option routers 10.235.9.254;
}
and
Code:
scasey $ grep -oP  '(?s)\{.*?\}' lqtext.txt
{
range 10.244.89.129 10.244.89.192;
option routers 10.244.89.254;
}
{
range 10.235.4.6 10.235.4.255;
option routers 10.235.5.254;
host 7test1 {
hardware ethernet 00:00:cc:d8:1a:aa;
fixed-address 10.235.4.121;
}
{
hardware ethernet 00:00:2b:4c:ef:ab;
fixed-address 10.235.4.170;
}
{
range 10.235.8.6 10.235.8.255;
option routers 10.235.9.254;
}
according to man grep, the -o means Show only the part of a matching line that matches PATTERN.
without it we get:
Code:
scasey $ grep -P  '(?s)\{.*?\}' lqtext.txt
subnet 10.244.89.128 netmask 255.255.255.128 {
range 10.244.89.129 10.244.89.192;
option routers 10.244.89.254;
}
subnet 10.235.4.0 netmask 255.255.254.0 {
range 10.235.4.6 10.235.4.255;
option routers 10.235.5.254;
host 7test1 {
hardware ethernet 00:00:cc:d8:1a:aa;
fixed-address 10.235.4.121;
}
host test2 {
hardware ethernet 00:00:2b:4c:ef:ab;
fixed-address 10.235.4.170;
}
subnet 10.235.8.0 netmask 255.255.254.0 {
range 10.235.8.6 10.235.8.255;
option routers 10.235.9.254;
}
The -P operator makes grep accept perl regexps

I'll leave it to you to work out how to select what you want to keep from your list of ~1000 IP addresses.

HTH
 
Old 05-30-2017, 02:41 PM   #3
cdhjrt
Member
 
Registered: Mar 2002
Location: Marysville, WA
Distribution: Redhat, Win2K, Solaris
Posts: 45

Original Poster
Rep: Reputation: 15
Thank you for the reply. The issue with grep is identifying how many lines need to be cut.

The example here I need to cut this into 3 sections. Some of the scopes have 30 static clients so the number of lines would be over 100.

Here is one of the sections I need to cut out.

If I can find a way to count the number of lines between the opening { and closing } then I could easily use grep or sed. I can't quite find a way to count the lines or grab all these lines.

Code:
# VLAN b
subnet 10.235.4.0 netmask 255.255.254.0 { 
range 10.235.4.6 10.235.4.255;
option routers 10.235.5.254;
host 7test1 {
hardware ethernet 00:00:cc:d8:1a:aa;
fixed-address 10.235.4.121;
}
host test2 {
hardware ethernet 00:00:2b:4c:ef:ab;
fixed-address 10.235.4.170;
}
}
 
Old 05-30-2017, 03:51 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Oh! I though you were going the other way...parse the DHCPD.conf file and pull out the scopes you wanted to keep...(isn't that a much shorter list?)
So, if you wanted to keep only VLAN a in your example:
Code:
$ grep -P  '(?s)subnet 10.244.89.128.*\{.*?\}\n#' lqtext.txt | grep -v \#
subnet 10.244.89.128 netmask 255.255.255.128 {
range 10.244.89.129 10.244.89.192;
option routers 10.244.89.254;
}
...replace the hard-coded IP in my example with a variable populated from a list of scope IP's you want to keep. Loop the list in your script and redirect the output to what will be your new DHCPD.conf

But to answer your other question, pipe the grep to wc -l to get a line count.
 
Old 05-31-2017, 10:18 AM   #5
cdhjrt
Member
 
Registered: Mar 2002
Location: Marysville, WA
Distribution: Redhat, Win2K, Solaris
Posts: 45

Original Poster
Rep: Reputation: 15
thanks again, I am getting closer. That grabs everything from the IP listed to the end of the file.

I want to locate and KEEP all of this information.

# VLAN b
subnet 10.235.4.0 netmask 255.255.254.0 {
range 10.235.4.6 10.235.4.255;
option routers 10.235.5.254;
host 7test1 {
hardware ethernet 00:00:cc:d8:1a:aa;
fixed-address 10.235.4.121;
}
host test2 {
hardware ethernet 00:00:2b:4c:ef:ab;
fixed-address 10.235.4.170;
}
}

Keep in mind, some of these scopes are /16 and have hundreds of reservations which is why I want to select the line from the opening { to the closing } I colored the opening and closing lines in red.
 
Old 07-17-2017, 04:47 PM   #6
cdhjrt
Member
 
Registered: Mar 2002
Location: Marysville, WA
Distribution: Redhat, Win2K, Solaris
Posts: 45

Original Poster
Rep: Reputation: 15
Solved

I finally got it and I am posting it in case anyone else ever runs into this issue.

Feel free to let me know how how I can make this better, I love to learn

Code:
!/bin/bash
# Script to pull active scopes from dhcpd.conf. This only works for /24 networksat this time.
#remove files used by the script.
rm ip.out
rm ip1.out
#Get active leases and place them into a file.
grep lease /var/lib/dhcp/db/dhcpd.leases|cut -d " " -f2|uniq| while read line
do
IFS=. read ip1 ip2 ip3 ip4
vip=$ip1.$ip2.$ip3.0
echo $vip >> ip.out
done
#Make sure all the leases are uniq and put them into a new file
cat ip.out|sort|uniq >> ip1.out
#Read the uniq IP's and grab them from the dhcpd.conf file.
cat ip1.out | while read line
do
#Get the starting and ending line numbers and assign them l1 and l2
read vip
if [[ $(/home/chammontree/awkline.awk -vip="$vip netmask" dhcpd.conf) ]];then
line=`/home/chammontree/awkline.awk -vip="$vip netmask" dhcpd.conf`
#echo $line "this is line"
l1=$(echo $line | cut -d " " -f1)
l2=$(echo $line | cut -d " " -f2)
# This makes sure we have 1 line above copied from our dhcpd.conf
l1=$((l1-1))
#echo $l1
#echo $l2
# Get the scopes and copy to new file
awk -vl1=$l1 -vl2=$l2 'NR >= l1 && NR <= l2' dhcpd.conf >>new.dhcpd.conf
else
#echo $vip >>vip.out
continue
fi
done
Here is the awkline script that is called.

Quote:
#!/usr/bin/awk -f
#$ip=ip
$0 ~ ip {subnet++};

/{/ && subnet {
b++;
if (!first++) {
print NR
}
} ;

/}/ && subnet {b--} ;

subnet && b == 0 && NR > 1 {
print NR;
exit
}
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"one-lease-per-client" option in ISC dhcpd dhcpd.conf file m4rtin Linux - Networking 3 09-27-2012 04:44 AM
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
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

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:06 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration