如何使用golang/net包在Linux中读取虚拟IP?

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

How to read virtual IP in linux using golang/net package

问题

我正在尝试读取特定接口及该接口上的所有虚拟IP。

以下是Linux命令ifconfig -a | grep -A eth0的示例输出:

ifconfig -a | grep -A2 eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 100.180.89.148  netmask 255.255.255.224  broadcast 100.180.89.140
        inct6 fe80::ac16:2dff:fead:a321  prefixlen 64  scopeid 0x20<link>
--
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 100.180.89.150  netmask 255.255.255.255  broadcast 100.180.89.150
        ether cc:16:2d:ad:a3:20  txqueuelen 1000  (Ethernet)
--
eth0:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 100.180.89.151  netmask 255.255.255.255  broadcast 100.180.89.151
        ether ac:16:2d:ad:a3:20  txqueuelen 1000  (Ethernet)
--
eth0:3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 100.180.89.152  netmask 255.255.255.255  broadcast 100.180.89.152
        ether ac:16:2d:ad:a3:20  txqueuelen 1000  (Ethernet)

我在Go代码中尝试了以下方式:

// nwkIfName = eth0
func networkInterfaces(nwkIfName string) ([]Interfaces, error) {
	ifaces, err := net.Interfaces()
	if err != nil {
		return nil, fmt.Errorf("failed to get network interfaces: %w", err)
	}

	for _, nwIf := range ifaces {
		fmt.Println(nwIf.Name)
		if nwIf.Name == nwkIfName {
			fmt.Println(nwIf.Addrs())
		}
	}
	return nil, nil
}

输出结果:

[100.180.89.148/27 100.180.89.150/32 100.180.89.151/32 100.180.89.152/32 fe80::ae16:2dff:fead:a320/64] <nil>

如何读取eth0:2的IP地址?

谢谢,James

英文:

I am trying to read a particular interface and all the virtual IP in that interface.

Here is and example of the linux ifconfig -a | grep -A eth0

ifconfig -a | grep -A2 eth0
eth0: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500
        inet 100.180.89.148  netmask 255.255.255.224  broadcast 100.180.89.140
        inct6 fe80::ac16:2dff:fead:a321  prefixlen 64  scopeid 0x20&lt;link&gt;
--
eth0:1: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500
        inet 100.180.89.150  netmask 255.255.255.255  broadcast 100.180.89.150
        ether cc:16:2d:ad:a3:20  txqueuelen 1000  (Ethernet)
--
eth0:2: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500
        inet 100.180.89.151  netmask 255.255.255.255  broadcast 100.180.89.151
        ether ac:16:2d:ad:a3:20  txqueuelen 1000  (Ethernet)
--
eth0:3: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500
        inet 100.180.89.152  netmask 255.255.255.255  broadcast 100.180.89.152
        ether ac:16:2d:ad:a3:20  txqueuelen 1000  (Ethernet)

I tried like this in go code:


// nwkIfName = eth0
func networkInterfaces(nwkIfName string) ([]Interfaces, error) {
	ifaces, err := net.Interfaces()
	if err != nil {
		return nil, fmt.Errorf(&quot;failed to get network interfaces: %w&quot;, err)
	}

	for _, nwIf := range ifaces {
		fmt.Println(nwIf.Name)
		if nwIf.Name == nwkIfName {
			fmt.Println(nwIf.Addrs())
		}
	}
	return nil, nil
}

Output:

[100.180.89.148/27 100.180.89.150/32 100.180.89.151/32 100.180.89.152/32 fe80::ae16:2dff:fead:a320/64] &lt;nil&gt;

How can I read the IP address of eth0:2 ?

Thanks James

答案1

得分: 1

调用Interface.Addrs()来获取接口的地址,并从地址中进行类型断言*net.IPNet

使用其IPNet.IP字段来获取IP地址(类型为net.IP),如果需要IPv4地址,则使用其IP.To4()方法。

for _, nwIf := range ifaces {
    fmt.Println(nwIf.Name)
    if nwIf.Name == nwkIfName {
        fmt.Println(nwIf.Addrs())
        addrs, err := nwIf.Addrs()
        if err != nil {
            return nil, fmt.Errorf(
                "failed to get addresses of interface: %s, err: %w",
                nwIf.Name, err,
            )
        }
        for _, addr := range addrs {
            if ipNet, ok := addr.(*net.IPNet); ok {
                fmt.Println("\tIP:", ipNet.IP)
            } else {
                fmt.Println("\tnot IPNet:", addr)
            }
        }
    }
}
英文:

Call Interface.Addrs() to get the interface's addresses, and type-assert *net.IPNet from the addresses.

Use its IPNet.IP field to get just the IP address (of type net.IP), and its IP.To4() method if you need an IPv4 address.

for _, nwIf := range ifaces {
	fmt.Println(nwIf.Name)
	if nwIf.Name == nwkIfName {
		fmt.Println(nwIf.Addrs())
		addrs, err := nwIf.Addrs()
		if err != nil {
			return nil, fmt.Errorf(
				&quot;failed to get addresses of interface: %s, err: %w&quot;,
				nwIf.Name, err,
			)
		}
		for _, addr := range addrs {
			if ipNet, ok := addr.(*net.IPNet); ok {
				fmt.Println(&quot;\tIP:&quot;, ipNet.IP)
			} else {
				fmt.Println(&quot;\tnot IPNet:&quot;, addr)
			}
		}
	}
}

huangapple
  • 本文由 发表于 2022年7月27日 00:13:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/73126689.html
匿名

发表评论

匿名网友

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

确定