0

On my dhcp configuration, i have on commit hook to save device information. My problem is some of the mac addresses becomes invalid:

8:7c:39:cf:b6:3f - this should start with zero

8:d0:b7:52:f9:68 - also this

my dhcpd.conf

set clientmac = binary-to-ascii(16,8,":",substring(hardware,1,6));

hodl
  • 101
  • 1

1 Answers1

1

It depends on what you use to parse it, one can argue that it is completely valid to leave out prefix zeroes, and that is what we do most of the time because there is no definition of how many digits there should be.

However if we skip over the part about if this is being invalid and why or not and instead ask "How do I get this in desired format" we can provide a answer.

In this case isc has a KB article about it

This is not a bug. The problem is that the binary-to-ascii function doesn't "know" anything about the intended use of the converted binary digits and it's unusual to include leading zeroes when printing numeric values.

However, with a bit of additional manipulation it's still possible to get the desired result:

set foo = concat (
suffix (concat ("0", binary-to-ascii (16, 8, "", substring(hardware,1,1))),2), ":",
suffix (concat ("0", binary-to-ascii (16, 8, "", substring(hardware,2,1))),2), ":",
suffix (concat ("0", binary-to-ascii (16, 8, "", substring(hardware,3,1))),2), ":",
suffix (concat ("0", binary-to-ascii (16, 8, "", substring(hardware,4,1))),2), ":",
suffix (concat ("0", binary-to-ascii (16, 8, "", substring(hardware,5,1))),2), ":",
suffix (concat ("0", binary-to-ascii (16, 8, "", substring(hardware,6,1))),2)
);

(It operates by converting each "component" separately, adding a leading zero to it (in case one is needed); taking the last two hex characters, and then concatenating them all together again.)

NiKiZe
  • 1,246
  • 8
  • 20
  • Thank you for your time answering this issue. Good solution. – hodl Jul 29 '21 at 04:15
  • If this solves your question, please mark it as answer ;) – NiKiZe Jul 29 '21 at 05:12
  • No yet, I am still looking for a better solution. I am currently using my alternative solution which is `if mac_address is invalid then do arp ip_address` which is currently shorter than concatenating strings. But you have a good solution anyway. – hodl Jul 29 '21 at 05:30
  • The question is about MAC address being "invalid" and how to have them with prefixed zeros? Hope you find a good solution for your real issue then, even tho that seems different from this question. – NiKiZe Jul 29 '21 at 06:45