I'm trying to parse a dhcpd.conf file that looks like this:
authoritative;
subnet x.x.x.x netmask x.x.x.x {
range x.x.x.x x.x.x.x;
deny unknown-clients;
default-lease-time 86400;
max-lease-time 86400;
option domain-name "bla";
option domain-name-servers x.x.x.x;
option broadcast-address x.x.x.x;
option subnet-mask x.x.x.x;
option routers x.x.x.x;
host host1 {
hardware ethernet 00:e1:4c:68:00:53;
fixed-address 1.1.1.1;
}
host host2 {
hardware ethernet 01:e2:4d:69:01:54;
fixed-address 2.2.2.2;
}
host host3 {
hardware ethernet 02:e3:4e:70:02:55;
fixed-address 3.3.3.3;
}
host host4 {
hardware ethernet 03:e4:4f:71:03:56;
fixed-address 4.4.4.4;
}
host host5 {
hardware ethernet 04:e5:5f:72:04:57;
fixed-address 5.5.5.5;
}
}
In the end I need to loop though the host blocks (no matter their name) and assign the MAC address and IP address to variables in order to process the combination. So far I managed to do this with only one variable:
for MAC in `cat /etc/dhcp/dhcpd.conf | grep "hardware ethernet" | awk '{ print $3 }' | tr ";" " "`
do
echo "Found MAC address: " $MAC "Found IP: <I need the IP Variable here...>"
done
Maybe it's better to somehow "grep" the host blocks in order to loop through these, but I don't know how to do this.
Could anybody give me a hint on how to do this?
Thanks
Given that the input file is in the exact format (MAC followed by IP), following
sed
one liner will give a list of "MAC,IP" csv pairs. You can parse it and do what ever you want.Output:
To produce the exact output as in your example,
Output:
EDIT
You can extract MAC and IP from each pair and do something with them as follows.