Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would one generate a MAC address in Javascript?

Tags:

javascript

I need to generate a random MAC address for a project of mine, and I can not get it to work. Below is my current code (that is not working).

function genMAC(){
// Make a new array with all available HEX options.
var colours = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
// Make variable to hold 6 character HEX array
partA = new Array(1);
partB = new Array(1);
partC = new Array(1);
partD = new Array(1);
partE = new Array(1);
partF = new Array(1);
mac-address="";
for (i=0;i<2;i++){
    // Loop for partA
    partA[i]=colours[Math.round(Math.random()*14)];
}
    for (i=0;i<2;i++){
    // Loop through 6 times, randomising the letter added to the array
    partB[i]=colours[Math.round(Math.random()*14)];
}
    for (i=0;i<2;i++){
    // Loop through 6 times, randomising the letter added to the array
    partC[i]=colours[Math.round(Math.random()*14)];
}
    for (i=0;i<2;i++){
    // Loop through 6 times, randomising the letter added to the array
    partD[i]=colours[Math.round(Math.random()*14)];
}
    for (i=0;i<2;i++){
    // Loop through 6 times, randomising the letter added to the array
    partE[i]=colours[Math.round(Math.random()*14)];
}
    for (i=0;i<2;i++){
    // Loop through 6 times, randomising the letter added to the array
    partF[i]=colours[Math.round(Math.random()*14)];
}
// Returns like "a10bc5". It is likely that you may need to add a "#".
mac-address = partA + ":" + partB + ":" + partC + ":" + partD + ":" + partE + ":" + partF;
return mac-address;

}

Ugly. I'm fairly new to JS and I am wondering if there is an easier way to do this that would work.

like image 629
Kaz Wolfe Avatar asked Jul 07 '14 23:07

Kaz Wolfe


People also ask

Can you get MAC address using JavaScript?

No you cannot get the MAC address in JavaScript, mainly because the MAC address uniquely identifies the running computer so it would be a security vulnerability. Now if all you need is a unique identifier, I suggest you create one yourself using some cryptographic algorithm and store it in a cookie.

How do I create a virtual MAC address?

To create a virtual MAC, you need to first create a Virtual Router ID (VRID) and bind it to an interface. (In an HA setup, you need to bind the VRID to the interfaces on both nodes.) Once the VRID is bound to an interface, the system generates a virtual MAC with the VRID as the last octet.

How do I find the MAC address of a Web application?

To get the MAC address, pass the parameter 'getmac' which returns the MAC address of the client. 'getmac' is a CMD command to get the MAC address.


2 Answers

"XX:XX:XX:XX:XX:XX".replace(/X/g, function() {
  return "0123456789ABCDEF".charAt(Math.floor(Math.random() * 16))
});

jsfiddle http://jsfiddle.net/guest271314/qhbC9/

like image 71
guest271314 Avatar answered Oct 20 '22 01:10

guest271314


Here is a bit more of a "clean" version of your code, which takes the number of loops down to one. On each iteration of the loop, it appends two random characters to the macAddress variable, and if it's not the last iteration, it also appends a colon. Then it returns the generated address.

function genMAC(){
    var hexDigits = "0123456789ABCDEF";
    var macAddress = "";
    for (var i = 0; i < 6; i++) {
        macAddress+=hexDigits.charAt(Math.round(Math.random() * 15));
        macAddress+=hexDigits.charAt(Math.round(Math.random() * 15));
        if (i != 5) macAddress += ":";
    }

    return macAddress;
}

Aside from being more complicated than necessary, there were two problems with your code. The first was that mac-address is an invalid variable name due to the dash. That caused the code to not work at all. With that fixed, the other problem was that when setting the MAC address variable at the end, you appended arrays to a string. That caused each of the arrays containing two hex digits to be converted to a string, which meant a comma was put between the two digits.

like image 44
Ashley Strout Avatar answered Oct 19 '22 23:10

Ashley Strout