netConfig.go
的源代码由三部分组成。netConfig.go
的第一部分:1package main23import (4"fmt"5"net"6)78func main() {9interfaces, err := net.Interfaces()10if err != nil {11fmt.Println(err)12return13}Copied!
net.Interfaces()
的作用是将当前计算机的所有接口信息通过一个切片数据结构返回,切片的数据元素类型为net.Interface
。此切片将用于获取接口的信息。netConfig.go
的第二个部分包含以下Go代码:1for _, i := range interfaces {2fmt.Printf("Interface: %v\n", i.Name)3byName, err := net.InterfaceByName(i.Name)4if err != nil {5fmt.Println(err)6}Copied!
net.Interface
类型来访问切片的每个元素,获取所需的信息。netConfig.go
会得到以下输出:1$ go run netConfig.go2Interface: lo03Interface Address #0: 127.0.0.1/84Interface Address #1: ::1/1285Interface Address #2: fe80::1/6467Interface: gif089Interface: stf01011Interface: XHC201213Interface: en014Interface Address #0: fe80::18fa:901a:ea9:eb5f/6415Interface Address #1: 192.168.1.200/2416Interface Address #2: 2a02:587:3006:b800:1cb8:bf1b:b154:4d0c/6417Interface Address #3: 2a02:587:3006:b800:d84a:f0c:c932:35d1/641819Interface: en12021Interface: p2p02223Interface: awdl02425Interface: en22627Interface: en32829Interface: bridge03031Interface: utun032Interface Address #0: fe80::2514:c3a3:ca83:e1c6/643334Interface: utun135Interface Address #0: fe80::4e0b:a9a6:9abe:81a4/643637Interface: en538Interface Address #0: fe80::1cb4:a29e:97bc:6fb5/6439Interface Address #1: 169.254.72.59/16Copied!
netConfig.go
返回了很多接口和IP地址信息。netConfig.go
会得到以下输出:1$ go run netConfig.go2Interface: lo3Interface Address #0: 127.0.0.1/84Interface Address #1: ::1/12856Interface: dummy078Interface: eth09Interface Address #0: 10.74.193.253/2410Interface Address #1: 2a01:7e00::f03c:91ff:fe69:1381/6411Interface Address #2: fe80::f03c:91ff:fe69:1381/641213Interface: teql01415Interface: tunl01617Interface: gre01819Interface: gretap02021Interface: erspan02223Interface: ip_vti02425Interface: ip6_vti02627Interface: sit02829Interface: ip6tnl03031Interface: ip6gre0Copied!
并非所有列出的网络接口都关联了真正的硬件网络设备。最典型的例子是lo0
接口,它是环回设备。环回设备是一种特殊的虚拟网络接口,主机可以通过该接口与自身通信。
netCapabilities.go
的代码也分为三部分。程序netCapabilities.go
的目的是打印UNIX操作系统的主机上每个网络接口的功能。netCapabilities.go
使用结构net.Interface
的字段,定义如下:1type Interface struct {2Index int3MTU int4Name string5HardwareAddr HardwareAddr6Flags Flags7}Copied!
netCapabilities.go
的第一部分如下:1package main23import (4"fmt"5"net"6)Copied!
netCapabilities.go
生成以下输出:1$ go run netCapabilities.go2Name: lo03Interface Flags: up|loopback|multicast4Interface MTU: 163845Interface Hardware Address:67Name: gif08Interface Flags: pointtopoint|multicast9Interface MTU: 128010Interface Hardware Address:1112Name: stf013Interface Flags: 014Interface MTU: 128015Interface Hardware Address:1617Name: XHC2018Interface Flags: 019Interface MTU: 020Interface Hardware Address:2122Name: en023Interface Flags: up|broadcast|multicast24Interface MTU: 150025Interface Hardware Address: 98:5a:eb:d7:84:cd2627Name: en128Interface Flags: up|broadcast|multicast29Interface MTU: 150030Interface Hardware Address: d0:03:4b:cf:84:d33132Name: p2p033Interface Flags: broadcast|multicast34Interface MTU: 230435Interface Hardware Address: 02:03:4b:cf:84:d33637Name: awdl038Interface Flags: broadcast|multicast39Interface MTU: 148440Interface Hardware Address: 02:ac:d4:3b:d9:294142Name: en243Interface Flags: up|broadcast|multicast44Interface MTU: 150045Interface Hardware Address: 0a:00:00:a5:32:b04647Name: en348Interface Flags: up|broadcast|multicast49Interface MTU: 150050Interface Hardware Address: 0a:00:00:a5:32:b15152Name: bridge053Interface Flags: up|broadcast|multicast54Interface MTU: 150055Interface Hardware Address: 0a:00:00:a5:32:b05657Name: utun058Interface Flags: up|pointtopoint|multicast59Interface MTU: 200060Interface Hardware Address:6162Name: utun163Interface Flags: up|pointtopoint|multicast64Interface MTU: 138065Interface Hardware Address:6667Name: en568Interface Flags: up|broadcast|multicast69Interface MTU: 150070Interface Hardware Address: 6e:72:e7:1b:cd:5fCopied!
netCapabilities.go
将输出类似的结果。shell
运行环境中执行netstat -nr
命令查看,或在Go程序中使用exec.Command()
执行该命令,并通过pipe
或exec.CombinedOutput()
以文本的形式获取其输出,并打印。然而,这种方式既不优雅也不完美。