如何将MAC地址增加一个定义的值?

发布于 2024-10-06 12:29:29 字数 219 浏览 3 评论 0原文

我有一个 mac 地址字符串(其中没有“:”),我需要计算第二个下一个地址。

我尝试过:

$macDec = hexdec($mac);
$macDec += 2;
$newMac = dechex($macDec);

但这将十进制值修剪为整数范围并且计算出的 mac 不正确,我可以用比使用 BC Math 等更简单的方法来做到这一点吗?

I have a mac address string (without ':' in it) I need to calculate second next address.

I've tryed to:

$macDec = hexdec($mac);
$macDec += 2;
$newMac = dechex($macDec);

but this trims decimal value to integer range and calculated mac is incorrect, can i do this in any simple way than using BC Math etc. ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧情勿念 2024-10-13 12:29:29
$mac = increment_mac('00:00:00:00:00:fe', 2);

function increment_mac($mac_address, $increment = 1, $return_partition = ':')
{
    $mac_address = preg_replace('![^0-9a-f]!', '', $mac_address);

    $parts = str_split($mac_address, 2);
    foreach($parts as $i => $hex)
    {
        $parts[$i] = hexdec($hex);
    }

    $increase = true;
    if($increment < 0)
    {
        $increase = false;
    }

    $parts[5] += $increment;
    for($i = 5; $i >= 1; $i--)
    {
        if($increase)
        {
            while($parts[$i] > 255)
            {
                $parts[$i] -= 256;
                $parts[$i - 1] += 1;
            }
        }
        else
        {
            while($parts[$i] < 0)
            {
                $parts[$i] += 256;
                $parts[$i - 1] -= 1;
            }
        }
    }

    foreach($parts as $i => $dec)
    {
        $parts[$i] = str_pad(dechex($dec), 2, 0, STR_PAD_LEFT);
    }

    return implode($return_partition, $parts);
}
$mac = increment_mac('00:00:00:00:00:fe', 2);

function increment_mac($mac_address, $increment = 1, $return_partition = ':')
{
    $mac_address = preg_replace('![^0-9a-f]!', '', $mac_address);

    $parts = str_split($mac_address, 2);
    foreach($parts as $i => $hex)
    {
        $parts[$i] = hexdec($hex);
    }

    $increase = true;
    if($increment < 0)
    {
        $increase = false;
    }

    $parts[5] += $increment;
    for($i = 5; $i >= 1; $i--)
    {
        if($increase)
        {
            while($parts[$i] > 255)
            {
                $parts[$i] -= 256;
                $parts[$i - 1] += 1;
            }
        }
        else
        {
            while($parts[$i] < 0)
            {
                $parts[$i] += 256;
                $parts[$i - 1] -= 1;
            }
        }
    }

    foreach($parts as $i => $dec)
    {
        $parts[$i] = str_pad(dechex($dec), 2, 0, STR_PAD_LEFT);
    }

    return implode($return_partition, $parts);
}
蓝戈者 2024-10-13 12:29:29

好的,我找到了一个解决方案,因为事实上每个供应商都必须使用自己的 VendorID,并且标准 MTA MAC 是调制解调器 MAC + 2 我从 mac 中剥离供应商 ID 部分,进行简单的计算,并在前面添加供应商 ID

function mac2mtaMac($mac) {
    $mac = preg_replace('/[^0-9A-Fa-f]/', '', $mac);
    $macVendorID = substr($mac, 0, 6);
    $macDec = hexdec(substr($mac, 5));
    $macDec += 2;
    $macHex = dechex($macDec);
    $mtaMac = $macVendorID.str_repeat('0', 6 - strlen($macHex)).$macHex;
    return $mtaMac;
}

@Paul Norman:thx有关如何快速完成此操作的一些提示

OK, i found a solution, because of fact that each vendor has to use its own VendorID, and standard that MTA MAC is Modem MAC + 2 i strip vendorID part from mac, do a simple calculation, and prepend vendorID

function mac2mtaMac($mac) {
    $mac = preg_replace('/[^0-9A-Fa-f]/', '', $mac);
    $macVendorID = substr($mac, 0, 6);
    $macDec = hexdec(substr($mac, 5));
    $macDec += 2;
    $macHex = dechex($macDec);
    $mtaMac = $macVendorID.str_repeat('0', 6 - strlen($macHex)).$macHex;
    return $mtaMac;
}

@Paul Norman: thx for few hints on how to do that quickly

从此见与不见 2024-10-13 12:29:29

不要重新发明轮子,使用 netaddr 库。我编写此代码是为了根据序列号范围生成 MAC 地址列表。

from netaddr import *

# Create a list of serial numbers that need MAC addresses
#
sns=range(40125, 40192)

# Create a MAC address and seed it with the first available MAC address then convert 
# that MAC address to an integer for easy math
#
mac = EUI('00-11-22-33-02-D7').value

# Assign a MAC address to every serial number
#
for i in range(len(sns)):
    # Notice that we are creating a MAC address based on an integer
    #
    print "{0},{1}".format(sns[i], str(EUI(mac+i)).replace('-',' '))

Don't reinvent the wheel, use the netaddr library. I wrote this to generate a list of MAC addresses based on a serial number range.

from netaddr import *

# Create a list of serial numbers that need MAC addresses
#
sns=range(40125, 40192)

# Create a MAC address and seed it with the first available MAC address then convert 
# that MAC address to an integer for easy math
#
mac = EUI('00-11-22-33-02-D7').value

# Assign a MAC address to every serial number
#
for i in range(len(sns)):
    # Notice that we are creating a MAC address based on an integer
    #
    print "{0},{1}".format(sns[i], str(EUI(mac+i)).replace('-',' '))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文