]> git.proxmox.com Git - pve-common.git/blobdiff - data/PVE/Tools.pm
add PVE::Tools::random_ether_addr()
[pve-common.git] / data / PVE / Tools.pm
index d80524013b040b3f0e2b1acdc028a4191484ed2f..5e8e7bf46460c1abfa5fd199e3bcca14c5176cbe 100644 (file)
@@ -12,6 +12,7 @@ use Fcntl qw(:DEFAULT :flock);
 use base 'Exporter';
 use URI::Escape;
 use Encode;
 use base 'Exporter';
 use URI::Escape;
 use Encode;
+use Digest::SHA1;
 
 our @EXPORT_OK = qw(
 lock_file 
 
 our @EXPORT_OK = qw(
 lock_file 
@@ -635,5 +636,27 @@ sub decode_text {
     return Encode::decode("utf8", uri_unescape($data));
 }
 
     return Encode::decode("utf8", uri_unescape($data));
 }
 
+sub random_ether_addr {
+
+    my $rand = Digest::SHA1::sha1_hex(rand(), time());
+
+    my $mac = '';
+    for (my $i = 0; $i < 6; $i++) {
+       my $ss = hex(substr($rand, $i*2, 2));
+       if (!$i) {
+           $ss &= 0xfe; # clear multicast
+           $ss |= 2; # set local id
+       }
+       $ss = sprintf("%02X", $ss);
+
+       if (!$i) {
+           $mac .= "$ss";
+       } else {
+           $mac .= ":$ss";
+       }
+    }
+
+    return $mac;
+}
 
 1;
 
 1;