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

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

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

问题

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

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

  1. > arp -a
  2. ? (192.168.1.1) at 0:22:7:4a:21:d5 on en0 ifscope [ethernet]
  3. ? (224.0.0.251) at 1:0:5e:0:0:fb on en0 ifscope permanent [ethernet]
  4. ? (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函数来解决省略前导零的问题:

  1. // FixMacOSMACNotation 修复 macOS 上的 MAC 地址表示法。
  2. // 例如:1:0:5e:7f:ff:fa 变为 01:00:5e:7f:ff:fa
  3. func FixMacOSMACNotation(s string) string {
  4. var newString string
  5. split := strings.Split(s, ":")
  6. for i, s := range split {
  7. if i != 0 {
  8. newString += ":"
  9. }
  10. if len(s) == 1 {
  11. newString += "0" + s
  12. } else {
  13. newString += s
  14. }
  15. }
  16. return newString
  17. }

然后可以在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:

  1. > arp -a
  2. ? (192.168.1.1) at 0:22:7:4a:21:d5 on en0 ifscope [ethernet]
  3. ? (224.0.0.251) at 1:0:5e:0:0:fb on en0 ifscope permanent [ethernet]
  4. ? (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:

  1. // FixMacOSMACNotation fixes the notation of MAC address on macOS.
  2. // For instance: 1:0:5e:7f:ff:fa becomes 01:00:5e:7f:ff:fa
  3. func FixMacOSMACNotation(s string) string {
  4. var newString string
  5. split := strings.Split(s, ":")
  6. for i, s := range split {
  7. if i != 0 {
  8. newString += ":"
  9. }
  10. if len(s) == 1 {
  11. newString += "0" + s
  12. } else {
  13. newString += s
  14. }
  15. }
  16. return newString
  17. }

Which can then be used in net.ParseMAC successfully.

答案1

得分: 1

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

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

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. inputs := []string{
  8. "1:0:5e:7f:ff:fa",
  9. "1:0:5e:7f:ff:f",
  10. "1:0:5e:7f:ff:",
  11. "1:0:5e::ff:",
  12. }
  13. for _, input := range inputs {
  14. fmt.Println()
  15. fmt.Println("FixMacOSMACNotation ", FixMacOSMACNotation(input))
  16. }
  17. }
  18. // FixMacOSMACNotation修复了macOS上MAC地址的表示方式。
  19. // 例如:1:0:5e:7f:ff:fa变为01:00:5e:7f:ff:fa
  20. func FixMacOSMACNotation(s string) string {
  21. var e int
  22. var sb strings.Builder
  23. for i := 0; i < len(s); i++ {
  24. r := s[i]
  25. if r == ':' {
  26. for j := e; j < 2; j++ {
  27. sb.WriteString("0")
  28. }
  29. sb.WriteString(s[i-e : i])
  30. sb.WriteString(":")
  31. e = 0
  32. continue
  33. }
  34. e++
  35. }
  36. for j := e; j < 2; j++ {
  37. sb.WriteString("0")
  38. }
  39. sb.WriteString(s[len(s)-e:])
  40. return sb.String()
  41. }

这里尝试一下。

英文:

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.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;strings&quot;
  5. )
  6. func main() {
  7. inputs := []string{
  8. &quot;1:0:5e:7f:ff:fa&quot;,
  9. &quot;1:0:5e:7f:ff:f&quot;,
  10. &quot;1:0:5e:7f:ff:&quot;,
  11. &quot;1:0:5e::ff:&quot;,
  12. }
  13. for _, input := range inputs {
  14. fmt.Println()
  15. fmt.Println(&quot;FixMacOSMACNotation &quot;, FixMacOSMACNotation(input))
  16. }
  17. }
  18. // FixMacOSMACNotation fixes the notation of MAC address on macOS.
  19. // For instance: 1:0:5e:7f:ff:fa becomes 01:00:5e:7f:ff:fa
  20. func FixMacOSMACNotation(s string) string {
  21. var e int
  22. var sb strings.Builder
  23. for i := 0; i &lt; len(s); i++ {
  24. r := s[i]
  25. if r == &#39;:&#39; {
  26. for j := e; j &lt; 2; j++ {
  27. sb.WriteString(&quot;0&quot;)
  28. }
  29. sb.WriteString(s[i-e : i])
  30. sb.WriteString(&quot;:&quot;)
  31. e = 0
  32. continue
  33. }
  34. e++
  35. }
  36. for j := e; j &lt; 2; j++ {
  37. sb.WriteString(&quot;0&quot;)
  38. }
  39. sb.WriteString(s[len(s)-e:])
  40. return sb.String()
  41. }

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:

确定