英文:
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<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)
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("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
}
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] <nil>
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(
"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)
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论