Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: How to increment a MAC Address?

by Forsaken (Friar)
on May 23, 2005 at 09:15 UTC ( [id://459475]=note: print w/replies, xml ) Need Help??


in reply to How to increment a MAC Address?

my way to tackle this would be to split the 6 groups of digits using my @numbers = split(/:/, $string); after which you can access each of the sets as $numbers[0] through to $numbers[5]. Whenever you need them back in string form you can put it back together using join(':', @numbers);

Now the real question is, last time I checked MAC addresses were in hexadecimal, so you're still in need of ABCDEF on top of the 0 through 9? Besides that, why would you want to iterate through every possible MAC address in the first place?


Remember rule one...

Replies are listed 'Best First'.
Re^2: How to increment a MAC Address?
by mda2 (Hermit) on Jun 16, 2005 at 17:04 UTC
    To increment a MAC Address, you need to convert HEX to DEC, Add AND convert DEC to HEX...

    I propose this code.

    sub addmac { my ( $mac, $add ) = @_; $mac =~ s/://g; my $dec = hex($mac); $mac = sprintf("%012x", $dec+$add); return join(":", $mac =~ /(..)/g ); } my $mac = '00:00:00:00:00:f1'; foreach ( 1 .. 32 ) { $mac = addmac($mac,$_*4); print "$mac\n"; }
    I think others improve are possible, but it do a task.
    ... 00:00:00:00:01:a5 00:00:00:00:01:cd 00:00:00:00:01:f9 00:00:00:00:02:29 00:00:00:00:02:5d ... 00:00:00:00:03:9d 00:00:00:00:03:e9 00:00:00:00:04:39 00:00:00:00:04:8d

    --
    Marco Antonio
    Rio-PM

        I did this:
        # clear separators to obtain a hex string $mac =~ s/://g; # convert hex string into a dec number my $dec = hex($mac); # revert into a hex string $mac = sprintf("%012x", $dec+$add);

        I don't see error here. I think it's used by context...

        --
        Marco Antonio
        Rio-PM

      Close, but not quite. You're trying to store a 48bit number into a 32bit variable. (Well, it's 32 bit on my Windows machine.) You have an overflow problem.

      my $dec = hex('FF0000000000'); printf("%012x", $dec); __END__ Integer overflow in hexadecimal number at !.pl line 1 0000ffffffff

      You could use Math::BigInt, or you could use floating point numbers (doubles) as I've previously detailed.

Re^2: How to increment a MAC Address?
by aditya.singh (Acolyte) on Jun 16, 2005 at 12:04 UTC
    You code prints: 00:00:00:00:00:99 00:00:00:00:01:00 00:00:00:00:01:01 OP wants: 00:00:00:00:00:99 00:00:00:00:01:99 00:00:00:00:02:99 and so on. Note: Once the pair of digit reaches 99, it does not roll back to 00.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://459475]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-24 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found