Knowledge Base ISC Main Website Ask a Question/Contact ISC
Formatting MAC addresses in dhcpd (or 'why does binary-to-ascii strip leading zeroes?')
Author: Glenn Satchell Reference Number: AA-01039 Views: 15900 Created: 2013-09-02 15:48 Last Updated: 2017-06-08 12:45 100 Rating/ 1 Voters

This question arises from time to time when an administrator uses binary-to-ascii in dhcpd.conf, hoping to obtain the hardware address of a device that has been assigned a lease.  The problem is that where a component of the address has a leading zero, this zero will be omitted in the output.

Here's a typical example of usage that doesn't work quite as expected:

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

Instead of the expected hardware address format - for example f8:0c:f3:**:**:**, foo will instead contain f8:c:f3:**:**:**.

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.)

© 2001-2018 Internet Systems Consortium

For assistance with problems and questions for which you have not been able to find an answer in our Knowledge Base, we recommend searching our community mailing list archives and/or posting your question there (you will need to register there first for your posts to be accepted). The bind-users and the dhcp-users lists particularly have a long-standing and active membership.

ISC relies on the financial support of the community to fund the development of its open source software products. If you would like to support future product evolution and maintenance as well having peace of mind knowing that our team of experts are poised to provide you with individual technical assistance whenever you call upon them, then please consider our Professional Subscription Support services - details can be found on our main website.

Feedback 1
  • #
    [ rohit]: caution 2013-12-09 15:06

    while adding the code to allow zero to print remember all the lines written in a one line is what worked for me

Quick Jump Menu