I am trying to write a perl script for generating mac address for a specific project and i am stuck at a point.

Project: generate mac addresses with in a given range where the base OUI is predefined and the end range is 2f:ff:ff. the start mac address would be: 00:96:14:00:00:00 and the end mac address would be: 00:96:14:2f:ff:ff

I am able to generate a random mac address but the 4 bit is not normally within the "2f" range.

my random ff:ff:ff code is
sub mac { $cnt = 3; $mac[0] = "00"; $mac[1] = "96"; $mac[2] = "14"; while ($cnt < 6) { $rand = rand(255); if ($rand < 5) { $rand += 2; $hex = sprintf("%X", $rand); } else { $hex = sprintf("%X", $rand); } $mac[$cnt] = $hex; $cnt++; }
my conditional code for 2f:ff:ff is:
sub mac { $cnt = 3; $mac[0] = "00"; $mac[1] = "96"; $mac[2] = "14"; while ($cnt < 6) { if ($cnt ==4) { $rand = rand(47); } else { $rand = rand(255); if ($rand < 5) { $rand += 2; $hex = sprintf("%X", $rand); } else { $hex = sprintf("%X", $rand); } $mac[$cnt] = $hex; $cnt++; } }
The problem is that the above conditional loop gets stuck when executing. any help is higly appreciated.

Note: i am newbie and have been researching perl docs online and the above has been written from various similar online snippets and reference perl scripts.

In reply to how to use conditional loops when using while loop by pcrew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.