Register
It is currently Sun Apr 22, 2018 9:56 pm

get the IP vs MAC address from dhcpd


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Sat Nov 04, 2017 3:59 am   

Joined: Thu Nov 02, 2017 7:43 am
Posts: 3
Hy everybody,

I've a DHCPD running i my server.
Within the dhcp.conf, we have dedicated static IP's for a specific MAC addresses
i.e:
Quote:
host s6 {
hardware ethernet E8:50:8B:F3:98:F6;
fixed-address 192.168.0.254;
}


We have got about 100 static IP address.
Is it possible with a bash script, get in a simple text file two columns one IP's and the other for the corresponding MAC address?

i.e:
Quote:
192.168.0.254 E8:50:8B:F3:98:F6
192.168.0.253 xx:xx:xx:xx:xx:xx


Below is just the head of the dhcpd.conf file:

Quote:
#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

## by red: ICI on desire reserver des adresses IP fixes a des machines ##

group {
use-host-decl-names true;


#### ICI on defini toutes les machines, Administration by red 05/07/2011 ####

host s6 {
hardware ethernet E8:50:8B:F3:98:F6;
fixed-address 192.168.0.254;
}

................................................
.............................................
..............................................


Thanks a lot for helping me.


Top
 Profile  
 PostPosted: Sat Nov 04, 2017 5:02 am   

Joined: Thu Nov 02, 2017 7:43 am
Posts: 3
Well, lets make it simple ;;)

Using a bash script, how to grep only the ip address and the mac address from below and put them in a file:.

Quote:
host s6 {
hardware ethernet aa:aa:aa:aa:aa;
fixed-address 192.168.0.254;
}

host s5 {
hardware ethernet xx:xx:xx:xx:xx:xx;
fixed-address 192.168.0.253;
}


At the end i mean we will get a file with inside:
192.168.0.254 aa:aa:aa:aa:aa:aa
192.168.0.253 xx:xx:xx:xx:xx:xx:xx

Thanks for helping :)


Top
 Profile  
 PostPosted: Sun Nov 05, 2017 1:58 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Code:
sed -rn '/host.+\{/ {
            n
            H
            n
            H
            x
            s/\n/ /g
            s/;//g
            s/.+ (([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}) .+ (([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}).*/\1 \3/
            p
            s/.*//
            x
         }
    ' dhcp.conf

sed is your friend.
-rn we use extended-regexes and want -no automatic printing of each line.
/host.+\{/ if we read a line having host somewhere in the line followed by .+ any chars and a \{ literally curly brace
n we ignore this line and read immediately the next line.
H we add this line to the holdspace
n and read the next line, we append
H as well to the hold space
Now we got two lines in the holdspace; one with the MAC address, one with the IP address.
x we exchange the holdspace with the patternspace, effectively having our 2 lines now in the patternspace.
s/\//g we substitute \newline globally with // nothing, which deletes all newlines occuring in the patternspace.
s/;//g we do the same for semicolas (not really needed)
Now we have a single line with the words the MAC address and the IP address and we use the Extended Regexes to get MAC and IP only.
Again we use the subtitute/regex/replacement/ command.

s/.+ (([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}) .+ (([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}).*/\1 \3/
The regex part is a bit longer, so we first explain the replacement part.

/\1 \3/ The replacement is just the chars fetched by the \1 first group followed by a blank and the \3 third group fetched.
A group within a regex is formed by a pair of braces (somethin) would denote the char sequence somethin. Numbering is done from left to right by counting the opening brace.
The regex is easy:
.+ (([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}) .+ (([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}).*
We say: the line starts with
.+ any amount of any chars, followed by an blank and then a MAC address
(([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}) followed by
.+ any sequence of at least one chars followed by a blank followed by an IP address
(([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}) followed by
.* zero or any sequence of any chars.

The MAC address (([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}) starts with two (( braces.
[[:xdigit:]]{2} says [ a single hexadecimal digit ] occuring {2}times
We need 5 pairs of sequence of two hexadecimal digits followed by a : semicolon
(([[:xdigit:]]{2}:){5} does this. Five Times of two hexadecimal digits followed by a semicolon.
(([[:xdigit:]]{2}:){5})[[:xdigit:]]{2} we just have to add the last two hexadecimal digits and end up with a proper denoted MAC address.
The MAC address is now in the first \1 group.
Think about it: The first group starts and immediately the second group ist started as well, but the second group stops before the last two hexadecimal digits. Or in other words: The first group holds the entire MAC address, the second group only the first five pairs of digits including a semicolon.
The IP address is catched in same way ending up in group \3

Now we
p print the line which consists now of the MAC address followed by a blank followed by the IP address.

and we have to delete the holdspace. Otherwise the we have the last read line still in the holdspace. Remember that we exchanged the last line.
(Holdspace has gotten the 2 lines while the last line read was still in the patternspace. We then exchanged hold and pattern space)

so we just use again the s// command to delete each and every char in the line.
s/.*// substitute .* any char with nothing.
x and exchange again hold and pattern space. (Writing the now empty line into the hold space.)

At this point the first line of MAC address blank IP address is printed. Hold space is empty and pattern space has a line we do not need anymore.
sed just continues to read the next line and does nothing with it, as nothing is specified for sed to do with the line consisting of the closing } brace.
It reads line for line until it hits a line starting with host ....{ and the procedure starts again.

sed ist the appropriate tool for this job.
Of course this can be written in bash only too.
To the price of being very slowly and ugly.


Top
 Profile  
 PostPosted: Sun Nov 05, 2017 2:15 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Just change the replacement part from /\1 \3/ to /\3 \1/ to get the desired output format.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP