Listing IPs from the dhcpd.conf


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Listing IPs from the dhcpd.conf
# 1  
Old 11-09-2017
RedHat 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:
Code:
#ddns-update-style interim;
ddns-update-style none;
ignore client-updates;
deny client-updates;
authoritative;

#### By red for PXE Booting
allow booting;
allow bootp;
### End by red

log-facility local6;

subnet 192.168.0.0 netmask 255.255.255.0 {
        # --- default gateway

        ##### By red for PXE booting
        class "pxeclients" {    match if substring(option vendor-class-identifier, 0, 9) = "PXEClient";
        next-server 192.168.0.1;
        filename "linux-install/pxelinux.0"; }
        #### End by red

        option routers 192.168.0.1;
        option subnet-mask 255.255.255.0;
        # option nis-domain             "domain.org";
        option domain-name "ensm.intranet";
        option domain-name-servers 192.168.0.1;
        option time-offset -18000;
        range dynamic-bootp 192.168.0.5 192.168.0.239;
        default-lease-time 3600;
        max-lease-time 7200;
        # we want the nameserver to appear at a fixed address

        group {
        use-host-decl-names true;

        host Naoui-EISN1A {
                hardware ethernet 00:71:CC:6E:A3:33;
                fixed-address 192.168.0.6;
                }

        host Labdazi-EISN1A {
                hardware ethernet C0:38:96:72:8B:5B;
                fixed-address 192.168.0.7;
                }

        host Zair-EISN1A {
                hardware ethernet 08:ED:B9:08:94:09;
                fixed-address 192.168.0.8;
                }

        host Laddi-EISN1A {
                hardware ethernet D0:53:49:CB:FE:0F;
                fixed-address 192.168.0.9;
                }

        host Zeghar-EISN1A {
                hardware ethernet C4:8E:8F:8F:45:A7;
                fixed-address 192.168.0.10;
    }
    

      }
}

We need a bash script which list all the IP's in one text file in a way that :

Those Ip's who are part of the dhcpd.conf will be listed one per line and will have a '#' symbol at the begining, such as :

Code:
#192.168.0.6
#192.168.0.7
#192.168.0.8
.......
.......

All the other IP's who are not part of the dhcpd.conf will be just listed one per line :

Code:
192.168.0.100
192.168.0.101
192.168.0.201
.........
.........

Thanks a lot for your help Smilie

red
# 2  
Old 11-09-2017
Where to start? What about the addresses given as

Code:
next-server 192.168.0.1;
option routers 192.168.0.1;
option domain-name-servers 192.168.0.1;
range dynamic-bootp 192.168.0.5 192.168.0.239;

And, should e.g. 192.168.1.xxx be excluded? Why?
# 3  
Old 11-09-2017
Hy Rudic, nice to hear you again.
Well, NO they are not included.

Thanks RudiC

---------- Post updated at 01:49 PM ---------- Previous update was at 01:48 PM ----------

Just those who are in blue color within the dhcpd.conf .Thank you

---------- Post updated at 01:59 PM ---------- Previous update was at 01:49 PM ----------

Well from 192.168.0.1 - 192.168.0.254.

The 192.168.1.xxx is excluded because my dhcpd.conf is giving fixed ip addresses from 192.168.0.1 until 192.168.0.254.
I mean the 192.168.0.0/24 subnet.
in other ways:
Code:
192.168.0.1
192.168.0.2
192.168.0.4
.......
192.168.0.254

that's it .

Thanks again RudiC

Last edited by rbatte1; 11-15-2017 at 01:40 PM.. Reason: Added CODE tags, corrected spelling
# 4  
Old 11-09-2017
Try
Code:
awk -F"[ ;]*" '
/^subnet/       {SUBNET = $2
                }
/^ *fixed/      {FXIP[$3]
                }
END             {sub (/[^.]*$/, "", SUBNET)
                 for (i=1; i<255; i++)   {TMP = sprintf ("%s%d", SUBNET, i) 
                                         printf "%c%s\n", (TMP in FXIP)?"#":"", TMP
                                        }
                }
' dhcpd.conf
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
#192.168.0.6
#192.168.0.7
#192.168.0.8
#192.168.0.9
#192.168.0.10
192.168.0.11
192.168.0.12
192.168.0.13
192.168.0.14
192.168.0.15
.
.
.

Next time please post your own efforts and evtl. errors / failures / shortcomings.
# 5  
Old 11-09-2017
Yes as usual you are the best. It works perfectly.
You 're right i should post my own script first and then .....

Well the result will be written in a file called "ipblocked"

and then i will run the following script against it like this:

#./ipblock.sh
Code:
#!/bin/bash
BLOCKDB=/etc/squid/ipblocked
IPS=$(grep -Ev "^#" $BLOCKDB)
for i in $IPS
do
iptables -I FORWARD -s $i -j DROP

The final result will be part of my firewall (iptables).
I mean all the IP's who got the # symbol will be allowed to pass the forward chain, and all the others (the rest) will be dropped.

Thanks again RudiC.

Last edited by rbatte1; 11-15-2017 at 01:42 PM.. Reason: Removed gratuitous COLOR formatting and added CODE tags to make it clear
# 6  
Old 11-09-2017
Well, remembering your other threads, I could imagine it possible to combine everything (ACCEPT, DROP) into one single script reading your dhcpd.conf once.
# 7  
Old 11-10-2017
RedHat

Quote:
Originally Posted by RudiC
Well, remembering your other threads, I could imagine it possible to combine everything (ACCEPT, DROP) into one single script reading your dhcpd.conf once.
Exactly, that's my GOAL RudiC Smilie

Thanks

---------- Post updated at 02:37 PM ---------- Previous update was at 02:35 PM ----------

I will try to put every thing in just one script and i will post it. It may help others which have the same situation.

---------- Post updated 11-10-17 at 07:01 AM ---------- Previous update was 11-09-17 at 02:37 PM ----------

Something is strange, i can't catch it !!!

If the output of the following code is on the screen, i got the right things i.e:

Code:
awk -F"[ ;]*" '
/^subnet/       {SUBNET = $2
                }
/^ *fixed/      {FXIP[$3]
                }
END             {sub (/[^.]*$/, "", SUBNET)
                 for (i=1; i<255; i++)   {TMP = sprintf ("%s%d", SUBNET, i)
                                         printf "%c%s\n", (TMP in FXIP)?"#":"", TMP
                                        }
                }
' /home/red/dhcpd.conf

The result:


Code:
192.168.0.235
192.168.0.236
192.168.0.237
192.168.0.238
#192.168.0.239
#192.168.0.240
#192.168.0.241
#192.168.0.242

Now if the output of the same code is sent to a file , i got other things î.e:

Code:
 awk -F"[ ;]*" '
/^subnet/       {SUBNET = $2
                }
/^ *fixed/      {FXIP[$3]
                }
END             {sub (/[^.]*$/, "", SUBNET)
                 for (i=1; i<255; i++)   {TMP = sprintf ("%s%d", SUBNET, i)
                                         printf "%c%s\n", (TMP in FXIP)?"#":"", TMP
                                        }
                }
' /home/red/dhcpd.conf  > /home/red/ipblocked.txt

Code:
^@192.168.0.235
^@192.168.0.236
^@192.168.0.237
^@192.168.0.238
#192.168.0.239
#192.168.0.240
#192.168.0.241
#192.168.0.242

I got this : ^@

Why and how to take off these ^@ from the output !

Thanks a lotSmilie


Moderator's Comments:
Mod Comment Please use CODE (not QUOTE) tags as required by forum rules!

Last edited by RudiC; 11-10-2017 at 10:28 AM.. Reason: Changed QUOTE to CODE tags.
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. Solaris

Configure resolv.conf and nsswitch.conf

Hi, I've installed Solaris 11.3(live media) and configured DNS. Everytime I reboot the server, resolv.conf got deleted and it created a new nsswitch.conf. I used below to configure both settings: # svccfg -s dns/client svc:/network/dns/client> setprop config/nameserver = (xx.xx.xx.aa... (1 Reply)
Discussion started by: flexihopper18
1 Replies

3. Shell Programming and Scripting

Script to update rsyslog.conf and auditd.conf

Hello all, Newbie here. I'm currently tasked with updating rsyslog.conf and auditd.conf on a large set of servers. I know the exact logging configurations that I want to enable. I have updated both files on on a server and hope to use the updated files as a template for the rest of the... (3 Replies)
Discussion started by: Mide
3 Replies

4. UNIX for Dummies Questions & Answers

[Solved] How to remove listing of current user cmd from ps -ef listing?

Hi All, Could you please help to resolve my following issues: Problem Description: Suppose my user name is "MI90". i.e. $USER = MI90 when i run below command, i get all the processes running on the system containing name MQ. ps -ef | grep MQ But sometimes it lists... (8 Replies)
Discussion started by: KDMishra
8 Replies

5. Shell Programming and Scripting

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 : host bertha-clp-0 { hardware ethernet AA:0A:A0:00:6c:40; fixed-address 10.10.10.72; option... (6 Replies)
Discussion started by: ekckabatop
6 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. UNIX for Advanced & Expert Users

Configuring snmpd.conf and snmptrapd.conf

HI, I want a help for Configuring snmpd.conf and snmptrapd.conf (i.e Configuring SNMP) for receiving TRAPS in my networks. I am using RHEL4.0 OS. Please tell me How I can configure above two files in a proper way and at an advanced level. Especially I am getting... (2 Replies)
Discussion started by: jagdish.machhi@
2 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