在MacOS上使用”arp -a”工具时,出现了奇怪的MAC地址格式。

huangapple go评论81阅读模式
英文:

Weird MAC address format using "arp -a" tool on MacOS

问题

我想在MacOS上使用Go语言中的net.ParseMAC函数解析arp -a命令的输出,但由于奇怪的格式,我遇到了错误。

arp -a命令的示例输出如下:

> arp -a 
? (192.168.1.1) at 0:22:7:4a:21:d5 on en0 ifscope [ethernet]
? (224.0.0.251) at 1:0:5e:0:0:fb on en0 ifscope permanent [ethernet]
? (239.255.255.250) at 1:0:5e:7f:ff:fa on en0 ifscope permanent [ethernet]

MAC地址的格式不符合预期,因为MAC地址中的0100被简化为只有10。似乎允许的格式是A:B:C:D:E:F,而不是AA:BB:CC:DD:EE:FF

我该如何将输出转换为后一种格式,以便可以被net.ParseMAC函数接受?


编辑:
我编写了一个简单的Go函数来解决省略前导零的问题:

// FixMacOSMACNotation 修复 macOS 上的 MAC 地址表示法。
// 例如:1:0:5e:7f:ff:fa 变为 01:00:5e:7f:ff:fa
func FixMacOSMACNotation(s string) string {
    var newString string
    split := strings.Split(s, ":")
    for i, s := range split {
        if i != 0 {
            newString += ":"
        }
        if len(s) == 1 {
            newString += "0" + s
        } else {
            newString += s
        }
    }
    return newString
}

然后可以在net.ParseMAC中成功使用它。

英文:

I want to parse output from the arp -a command on MacOS, in Go using the net.ParseMAC function, however I'm getting an error due to the weird formatting.
Sample output from arp -a command:

> arp -a 
? (192.168.1.1) at 0:22:7:4a:21:d5 on en0 ifscope [ethernet]
? (224.0.0.251) at 1:0:5e:0:0:fb on en0 ifscope permanent [ethernet]
? (239.255.255.250) at 1:0:5e:7f:ff:fa on en0 ifscope permanent [ethernet]

The MAC formatting is unexpected because instead of doing 01 and 00, the MAC addresses include just 1 and 0. It seems the formatting is allowed to be A:B:C:D:E:F instead of AA:BB:CC:DD:EE:FF.

How can I make the output in the latter format, so it can be accepted by the net.ParseMAC function?


Edit:
I made a simple Go function to solve the leaving off leading zeroes problem:

// FixMacOSMACNotation fixes the notation of MAC address on macOS.
// For instance: 1:0:5e:7f:ff:fa becomes 01:00:5e:7f:ff:fa
func FixMacOSMACNotation(s string) string {
	var newString string
	split := strings.Split(s, ":")
	for i, s := range split {
		if i != 0 {
			newString += ":"
		}
		if len(s) == 1 {
			newString += "0" + s
		} else {
			newString += s
		}
	}
	return newString
}

Which can then be used in net.ParseMAC successfully.

答案1

得分: 1

这个版本使用strings.Builder来修复OP所需的输入。

使用[strings.]Builder可以高效地构建字符串,使用Write方法。它最小化了内存复制。零值是可以直接使用的。不要复制非零的Builder。

package main

import (
	"fmt"
	"strings"
)

func main() {
	inputs := []string{
		"1:0:5e:7f:ff:fa",
		"1:0:5e:7f:ff:f",
		"1:0:5e:7f:ff:",
		"1:0:5e::ff:",
	}

	for _, input := range inputs {
		fmt.Println()
		fmt.Println("FixMacOSMACNotation    ", FixMacOSMACNotation(input))
	}
}

// FixMacOSMACNotation修复了macOS上MAC地址的表示方式。
// 例如:1:0:5e:7f:ff:fa变为01:00:5e:7f:ff:fa
func FixMacOSMACNotation(s string) string {
	var e int
	var sb strings.Builder
	for i := 0; i < len(s); i++ {
		r := s[i]
		if r == ':' {
			for j := e; j < 2; j++ {
				sb.WriteString("0")
			}
			sb.WriteString(s[i-e : i])
			sb.WriteString(":")
			e = 0
			continue
		}
		e++
	}
	for j := e; j < 2; j++ {
		sb.WriteString("0")
	}
	sb.WriteString(s[len(s)-e:])
	return sb.String()
}

这里尝试一下。

英文:

This version uses strings.Builder to fix the given input as desired by OP.

A [strings.]Builder is used to efficiently build a string using Write methods. It minimizes memory copying. The zero value is ready to use. Do not copy a non-zero Builder.

package main

import (
	&quot;fmt&quot;
	&quot;strings&quot;
)

func main() {
	inputs := []string{
		&quot;1:0:5e:7f:ff:fa&quot;,
		&quot;1:0:5e:7f:ff:f&quot;,
		&quot;1:0:5e:7f:ff:&quot;,
		&quot;1:0:5e::ff:&quot;,
	}

	for _, input := range inputs {
		fmt.Println()
		fmt.Println(&quot;FixMacOSMACNotation    &quot;, FixMacOSMACNotation(input))
	}
}

// FixMacOSMACNotation fixes the notation of MAC address on macOS.
// For instance: 1:0:5e:7f:ff:fa becomes 01:00:5e:7f:ff:fa
func FixMacOSMACNotation(s string) string {
	var e int
	var sb strings.Builder
	for i := 0; i &lt; len(s); i++ {
		r := s[i]
		if r == &#39;:&#39; {
			for j := e; j &lt; 2; j++ {
				sb.WriteString(&quot;0&quot;)
			}
			sb.WriteString(s[i-e : i])
			sb.WriteString(&quot;:&quot;)
			e = 0
			continue
		}
		e++
	}
	for j := e; j &lt; 2; j++ {
		sb.WriteString(&quot;0&quot;)
	}
	sb.WriteString(s[len(s)-e:])
	return sb.String()
}

try it here

huangapple
  • 本文由 发表于 2021年9月14日 02:21:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/69167416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定