Skip to content

Instantly share code, notes, and snippets.

@rohan-molloy
Created August 21, 2021 05:10
Show Gist options
  • Save rohan-molloy/c5608cd18a2d5112b16fbee29e2fd7c3 to your computer and use it in GitHub Desktop.
Save rohan-molloy/c5608cd18a2d5112b16fbee29e2fd7c3 to your computer and use it in GitHub Desktop.
generate a random ipv6 address
// Generate a Random Mac
function randomMac() {
return "XX:XX:XX:XX:XX:XX".replace(/X/g, function() {
return "0123456789ABCDEF".charAt(Math.floor(Math.random() * 16))
}
)
};
// Generate IPv6 EUI64
function ip6eui(prefix,mac) {
return "".concat (prefix,
":",
mac.split(":")[0] ^ 2, mac.split(":")[1],
":",
mac.split(":")[2], "ff",
":",
"fe", mac.split(":")[3],
":",
mac.split(":")[4], mac.split(":")[5]
);
};
prefix='2001:db8:'
console.log(ip6eui(prefix,randomMac() ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment