sikofitt/generate-mac

生成虚拟MAC地址

v1.0.0 2021-01-20 23:50 UTC

This package is auto-updated.

Last update: 2024-09-14 20:20:13 UTC


README

小型库,用于生成唯一的私有MAC地址

pipeline status coverage report

安装

需要PHP v8.0,对于与PHP >= 7.3兼容的版本,请使用0.x分支。

composer

composer require sikofitt/generate-mac

使用方法

use Sikofitt\GenerateMac\Mac;

$mac = new Mac(); // default separator is ':'
// or
$mac->setSeparator(Mac::SEPARATOR_COLON);
$address = $mac->getAddress(); // ab:cd:ef:01:23:45

$mac = new Mac(Mac::SEPARATOR_DASH);
// or
$mac->setSeparator(Mac::SEPARATOR_DASH);
$address = $mac->getAddress(); // ab-cd-ef-01-23-45

$mac = new Mac(Mac::SEPARATOR_NONE);
// or
$mac->setSeparator(Mac::SEPARATOR_NONE);
$address = $mac->getAddress(); // abcdef012345

如果你不介意它是否唯一,你可以移除对私有MAC前缀的检查。

$mac = new Mac(Mac::SEPARATOR_COLON, false);
// or
$mac->setUnique(false);

$address = $mac->getAddress();

// '52:54:00:ab:cd:ef',  QEMU virtual NIC prefix 52:54:00
// It's really not likely there will be a collision though.

生成多个MAC地址

$addresses = $mac->getAddresses(10);

var_dump($addresses);
/*
 *   array (
 *       0 => '8a:20:0b:b7:c4:62',
 *       1 => '56:7d:47:56:e8:bd',
 *       2 => '2a:ae:7b:44:6f:9d',
 *       3 => '6a:36:1a:7c:04:3a',
 *       4 => '3a:3d:93:f5:a6:12',
 *       5 => '8a:85:ce:11:2c:a2',
 *       6 => '06:54:6f:b1:11:48',
 *       7 => 'c6:fe:9d:86:38:dd',
 *       8 => 'ba:39:b3:a2:a1:fa',
 *       9 => '32:73:c0:b3:62:27',
 *   );
 */

// if you call this with 1 as the count it will still
// return an array [0 => '32:73:c0:b3:62:27']

使用控制台组件

控制台脚本需要 symfony/console

user@localhost:~/generate-mac$ bin/generate-mac --count (int) --output (json|plain|string) --separator (none|colon|dash)
  • --count 生成 {count} 个MAC地址
  • --output 以所选格式输出
    • string: (默认) 以格式化方式输出MAC地址
    • plain: 无格式输出MAC地址
    • json: 以json格式输出MAC地址
  • --separator 以所选操作符输出
    • colon: ':' (默认)
    • dash: '-'
    • none: ''

请参阅 bin/generate-mac --help

测试

user@localhost:~/generate-mac$ vendor/bin/phpunit

许可证

GPL-3.0