如何使用Golang获取我的WiFi IP地址?

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

How to use Golang to get my wifi ip address?

问题

func getLocalIP() ([]string, error) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return nil, err
	}
	IPs := make([]string, 0)
	for _, a := range addrs {
		if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
			if ipNet.IP.To4() != nil && ipNet.IP.IsGlobalUnicast() {
				IPs = append(IPs, ipNet.IP.String())
			}
		}
	}
	return IPs, nil
}
func TestGetLocalIP() {
	addrs, _ := getLocalIP()
	for _, a := range addrs {
		fmt.Println(a)
	}
}

我使用了这段代码,它给我返回了一个IP地址列表。

我只想获取我的Wi-Fi本地地址,应该如何做到?

英文:
func getLocalIP() ([]string, error) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return nil, err
	}
	IPs := make([]string, 0)
	for _, a := range addrs {
		if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
			if ipNet.IP.To4() != nil {
				IPs = append(IPs, ipNet.IP.To4().String())
			}
		}
	}
	return IPs, nil
}
func TestGetLocalIP() {
	addrs, _ := getLocalIP()
	for _, a := range addrs {
		fmt.Println(a)
	}
}


I used this,but it give me a list of ip address.

I just want to get my wifi local address,how to do that?

答案1

得分: 0

你需要至少了解以下两个信息之一:

  1. 正确接口的名称(通常在 Mac 上是 en0,但可能因情况而异)
  2. 无线网络的地址,包括掩码的长度 - 例如 192.168.0.0/16 或 192.168.0.0/24 是常见的,但你需要提前弄清楚这个信息。

如果你只知道接口名称:

ifs, _ := net.Interfaces()
for _, ifi := range ifs {
  if ifi.Name == "en0" { // 或者你想要的其他过滤器
    addrs, _ := ifi.Addresses()
    for _, a := range addrs {
      // 过滤以获取你想要的地址,通常是单播 IP4
    }
  }
}

更简单的方法:

if, _ := net.InterfaceByName("en0")
addrs, _ := if.Addresses()
for _, a := range addrs {
  // 如前所述,过滤以获取你想要的地址
}

如果你知道无线网络的网络地址

// 遍历接口,获取地址,然后遍历地址并获取其中有 IP 的地址
if ip.Mask(net.CIDRMask(16, 32)) == myNetworkAddress {
  // 你获得此 IP 的接口可能是你的无线接口
}

选择哪种方法

依赖于接口具有特定名称通常不具备可移植性。所以,尽管我假设你只是想在自己的工作站上使其工作,但如果这是为了在多个主机上运行的某个东西,你可能需要至少从正确的网络地址开始匹配。还有其他技巧 - 如果你知道你将在一个服务器群中运行,每个主机都有来自制造商 XXX 的网卡,你可以查找 MAC 地址并查看它是否来自该制造商 - 你可以在这里尝试一下。你也可以使用其他过滤器,但这些都将非常特定于你的个别用例。

英文:

You need to know at least one of two pieces of information going in:

  1. the name of the correct interface (typically en0 on a Mac, e.g., but YMMV)
  2. the address for your wireless network, including the length of the mask - something like 192.168.0.0/16 or 192.168.0.0/24 is pretty common, but you'll have to figure this out ahead of time.

If you only know the interface name:

ifs, _ := net.Interfaces()
for _, ifi := range ifs {
  if ifi.Name == "en0" { // Or whatever other filter you want
    addrs, _ := ifi.Addresses()
    for _, a := range addrs {
      // Filter to get the one you want, typically unicast IP4
    }
  }
}

Still simpler:

if, _ := net.InterfaceByName("en0")
addrs, _ := if.Addresses()
for _, a := range addrs {
  // As before, filter for the address you want
}

If you know the network address for your wireless network

// Loop over interfaces, get addrs, then loop over addresses and get IPs for those that have them
if ip.Mask(net.CIDRMask(16, 32)) == myNetworkAddress {
  // The interface you got this IP from is probably your wifi interface
}

Which method to choose

Depending on the interface having a specific name is generally not going to be portable. So while my assumption was that you were just trying to get this working on your own workstation, if this is for something which needs to run across multiple hosts, you may need to start off at least with matching against the correct network address. There are other tricks as well - if you know e.g. that you are going to be running in a server farm in which every host has a NIC from manufacturer XXX, you can look up the MAC address and see if it comes from that manufacturer - you can try this out here. You can use other filters as well, but those are going to be pretty specific to your individual use case.

huangapple
  • 本文由 发表于 2023年1月28日 21:37:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75268039.html
匿名

发表评论

匿名网友

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

确定