5 ms·
I think this package does exactly what you need: https://pkg.go.dev/github.com/google/gopacket/routing https://pkg.go.dev/github.com/google/gopacket/routing. Wo
by fellerts 8mo ago
I think this package does exactly what you need: https://pkg.go.dev/github.com/google/gopacket/routing https://pkg.go.dev/github.com/google/gopacket/routing. Works on my machine (error handling left to the reader)
router, _ := routing.New()
iface, _, _, _ := router.Route(net.ParseIP("8.8.8.8"))
fmt.Println(iface.Name)
this prints my Ethernet interface as expected. It doesn't make any requests, it just figures out where to route a packet. I guess it interfaces with the OS routing table.
- rvermeulen98 8mo agoThanks for sharing! This is definitely something I will look into, I am all in favor to simplify the current implementation of finding the "default" OS network interface.
- contingencies 8mo agoYou'd better use the default route and not some random IP, particularly DNS IPs which people often meddle with. # IPv4 default route only uname Darwin$ route -n get 0.0.0.0 | grep interface | cut -d ':' -f2 Linux$ route -nv |grep ^0.0.0.0 | awk '{print $NF}'