in reply to Increment MAC address

Mosh,
I chose to use the Perl substr() function.

I take a decimal string, convert it into a hex value,
and format it to 12 hex digits (6 octets for Mac Addy).
I segment the hex string by using the substr() function which
takes a string, start value, and distance argument. Then I
shove it into an array, and use join() to fuse it back together
into MAC Address format.

A for loop is then issued to increment the MAC address for the
iteration agrument value that is passed into the script.

Usage: macIncrementor.pl <MAC_START> <MAC_INCREMENT> <ITERATIONS>

The script is as follows:

#!/bin/perl #********************************************************* #***** 02/18/08 MAC Incrementor #***** Author: wilcoxc #***** Email: wilcoxc_26149@yahoo.com #***** #***** Requires 3 Arguments #***** #***Usage: macIncrementor.pl <MAC_START> <MAC_INCREMENT> <ITERATIONS> #***** #***** Purpose: This script was designed to take a decimal #***** string, convert it into a hex value, and #***** format the octets of the MAC address. #***** A for loop is then issued to increment the MAC #***** address for the iteration agrument value #***** that is passed into the script. #***** #********************************************************* my $macStart = $ARGV[0]; my $macInc = $ARGV[1]; my $macIter = $ARGV[2]; my @mac_address = qw( 00 00 00 00 00 00 ); for ($i=0; $i<$macIter; $i++) { my $hex = sprintf("%012X","$macStart"); $mac_address[0] = substr($hex, 0, 2); $mac_address[1] = substr($hex, 2, 2); $mac_address[2] = substr($hex, 4, 2); $mac_address[3] = substr($hex, 6, 2); $mac_address[4] = substr($hex, 8, 2); $mac_address[5] = substr($hex, 10, 2); my $mac_string = join(":",@mac_address); print "$mac_string\n"; $macStart+=$macInc; }

This gives you the following output:

C:\Perl\macIncrementor.pl 99 1 10
00:00:00:00:00:63
00:00:00:00:00:64
00:00:00:00:00:65
00:00:00:00:00:66
00:00:00:00:00:67
00:00:00:00:00:68
00:00:00:00:00:69
00:00:00:00:00:6A
00:00:00:00:00:6B
00:00:00:00:00:6C

Enjoy!!!! Let me know how it works for you!

Replies are listed 'Best First'.
Re: MAC Address Incrementor
by ikegami (Patriarch) on Mar 10, 2008 at 18:30 UTC
    • You have an off-by-one error. Specifying one increment shows the start value and no increments.

      >perl macIncrementor.pl 65535 1 2 00:00:00:00:FF:FF <-- Should be 00:00:00:01:00:00 00:00:00:01:00:00 <-- Should be 00:00:00:01:00:01
    • It really sucks to have to convert the address to decimal before being able to use the program.

      >perl macIncrementor.pl 999 1 2 00:00:00:00:03:E7 00:00:00:00:03:E8 <-- Should be 00:00:00:00:09:9A
    • And most importantly, it only works for 0.000015 of the address space on a 32-bit Perl.

      >perl macIncrementor.pl 17179869184 1 2 00:00:FF:FF:FF:FF 00:00:FF:FF:FF:FF <-- Should be 00:04:00:00:00:01
      Ikegami, thanks a ton for the comments!

      I designed this script for testing where I wanted to use contiguous MAC addresses on a traffic generator. To give it an easy naming convention, I based it on a decimal for a subnet feel. I had intended to print the start value into a hex/MAC format, and then increment from there.
      My solution for the one-off error will be to not include the start value as an increment iteration.

      Any ideas on how to correct the conversion beyond 17179869184. I'm not sure
      how to fix this one.

      Again, I greatly appreciate the feedback!!!