英文:
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地址中的01
和00
被简化为只有1
和0
。似乎允许的格式是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 (
"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 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 < 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()
}
try it here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论