Perl Code‎ > ‎

rand_mac.pl

Here's a random bit of fun with MAC addresses and DHCP. Please don't use this to do something bad. I wrote it as a proof-of-concept at a job I was on a few years ago. The DHCP admin claimed there was no way we'd ever run out of IP addresses, even though we had some absurdly long DHCP lease period (15 days, if I remember correctly). I suggested that perhaps he should rethink that position, he essentially said "prove it." Well, if someone throws down the gauntlet, I'll always be the first to pick it up. So here's what I wrote. Enjoy!

#!/usr/bin/perl
################################################################################
# $Id: rand_mac.pl 77 2010-04-19 20:09:53Z v89326 $
# $URL: file:///S:/svn/utilities/trunk/rand_mac.pl $
################################################################################
#
# Title:   rand_mac.pl
# Author:  Kurt Kincaid
# VERSION: 0.0.1
#
################################################################################

use Getopt::Long;
use strict;

our ( $eth, @rand, $newMAC, $polite );

GetOptions( 'eth=s' => \$eth, 'polite' => \$polite );

$eth ||= "eth0";

while ( 1 ) {
    undef @rand;
    for ( 1 .. 6 ) { push( @rand, sprintf( "%02X", rand( 254 ) ) ); }
    $newMAC = join ":", @rand;
    system(
        "/sbin/ifdown $eth && /sbin/ifconfig $eth hw ether $newMAC && /sbin/ifup $eth"
    );
    if ( $polite ) {
        sleep int( rand( 10 ) ) + 10;
    }
}

################################################################################
# EOF

Comments