Posted by rvermeulen98 12 hours ago
When reading some blog posts, I found often a solution where it sends out an UDP dial to for example 8.8.8.8:53 because you can then get the network interface back from the connection it's local address. As fallback I implemented to pick the first non-loopback interface that is up.
Would be open to suggestions to do this in a better way!
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. # 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}'Great work.
Couldn't run it on macOS Tahoe. I believe this requires me lowering the security to allow it, which is something I would rather not doing.
xattr -c `which whosthere`