golang 获取网卡的gateway示例代码
时间: 2023-03-14 22:26:59 浏览: 150
网卡抓包示例源码
可以参考下面的示例代码:package mainimport (
"fmt"
"net"
)func main() {
ifaces, _ := net.Interfaces()
for _, iface := range ifaces {
addrs, _ := iface.Addrs()
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
// process IP address
fmt.Println("IP: ", ip)
gatewayIP := ip.Mask(ip.DefaultMask())
fmt.Println("Gateway IP: ", gatewayIP)
}
}
}
阅读全文