我该如何区分它们呢?我不想使用虚拟的。
下面是我使用net.Interfaces()得到的结果
[[
{1 65536 lo up|loopback}
{2 1450 eth0 fa:16:3e:12:39:9c up|broadcast|multicast}
{3 1500 docker0 02:42:b4:9d:fb:54 up|broadcast|multicast}
{4 1500 br-dbabe92ec60c 02:42:e3:7f:96:13 up|broadcast|multicast}
{6 1500 veth4bb5809 e6:a3:38:2f:f7:e4 up|broadcast|multicast}
{8 1500 vethcada9d2 16:8c:c9:37:8d:76 up|broadcast|multicast}
{10 1500 veth1e10eaa da:37:f9:9a:1d:10 up|broadcast|multicast}
]]我找不到他们之间的区别。
发布于 2021-08-05 09:44:43
像下面这样的东西应该可以工作。代码中的注释解释了为什么这个faq也是如此。
package main
import (
"fmt"
"net"
"os"
)
func main(){
ifaces, err := net.Interfaces()
if err != nil {
fmt.Println(err)
os.Exit(0)
}
for _, iface := range ifaces {
// If the second least significant bit of the first octet of the MAC address is 1,
// then it's a locally administered address so we omit it.
// If HardwareAddr is nil, it's a loopbook, so omit.
if (iface.HardwareAddr==nil) || (iface.HardwareAddr[0]&2 == 2) {
continue
}
fmt.Println(iface)
}
}我相信你有理由使用MAC作为uuid,但如果我没有提到你可能想要考虑做一些像UUIDv4这样的事情,那就是我的疏忽。
发布于 2021-08-05 09:41:58
如果硬件设备已向https://macvendors.com/注册,则可以使用其应用编程接口
我在你的数据中添加了另一个mac地址,因为上面的这些似乎都不是真正的硬件。
package main
import (
"fmt"
"net/http"
"net/url"
"io"
"time"
)
type idata struct {
index int
mtu int
name string
mac string
flags string
}
func main() {
data := []idata{
{1, 65536, "lo", "", "up|loopback"},
{2, 1450, "eth0", "", "up|broadcast|multicast"},
{3, 1500, "docker0", "02:42:b4:9d:fb:54", "up|broadcast|multicast"},
{4, 1500, "br-dbabe92ec60c", "02:42:e3:7f:96:13", "up|broadcast|multicast"},
{6, 1500, "veth4bb5809", "e6:a3:38:2f:f7:e4", "up|broadcast|multicast"},
{8, 1500, "vethcada9d2", "16:8c:c9:37:8d:76", "up|broadcast|multicast"},
{10, 1500, "veth1e10eaa", "da:37:f9:9a:1d:10", "up|broadcast|multicast"},
{11, 1500, "en8", "00:e0:4c:36:01:d4", "up|broadcast|multicast"},
}
for _, iv := range data {
// skip items (like localhost) with no mac address
if iv.mac == "" {
continue
}
// url encode the mac address
mac := url.QueryEscape(iv.mac)
resp, err := http.Get("https://api.macvendors.com/" + mac)
defer resp.Body.Close()
if err != nil {
panic(err)
}
body, err := io.ReadAll(resp.Body)
fmt.Println(iv.mac)
fmt.Println(string(body))
fmt.Println("-----------------")
// sleep to stop the API rate limit being exceeded
time.Sleep( 5 * time.Second)
}
}https://stackoverflow.com/questions/68661614
复制相似问题