[ruby] Mac Address Randomizer

No replies
spider
Offline
NeoApprentice
Joined: 2009/07/12

this is just an old script i wrote when i was starting to learn ruby, all it does it gives you a random mac address

#!/usr/bin/ruby

def RandomMac()
    hexList =["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
    srand();
    res ="00"; #assign the first two digits to 00 (this makes it look more legit Tongue
    for i in (0..4)
        res +=":" +hexList[rand(hexList.length)] +hexList[rand(hexList.length)];
    end
    return res;
end

def Execute(cmd)
    output =IO.popen(cmd) { |io| io.read }
    return output
end

if(ARGV[0] ==nil)
    print("Usage:\n\tmacRan.rb interface\n");
else
    Execute("ifconfig " +ARGV[0] +" down");
    Execute("ifconfig " +ARGV[0] +" hw ether " +RandomMac());
    Execute("ifconfig " +ARGV[0] +" up");

    #find the new mac address and then display it to the user
    newMac =Execute("ifconfig " +ARGV[0] +" | grep HWaddr").split(" ");
    print(newMac[newMac.length-1], "\n");
end