On any given computer (OSX, WIn, Lin, etc) there are any number of connected network adapters... whether it's Wi-Fi, BlueTooth, Ethernet or other... And depending on the routing there may be multiple active devices.
In my NARROW use case I want to know what the current CONNECTED default adapter type (Wi-Fi etc...) although once that's known there are easily some others as well as some details.
Here is an example shell script that mostly works and converting it to Go is easy enough... it just seems to me there must be a native GO way.
You can use Interfaces() from net package
Sample:
package main
import (
"fmt"
"net"
)
func main() {
l, err := net.Interfaces()
if err != nil {
panic(err)
}
for _, f := range l {
if f.Flags&net.FlagUp > 0 {
fmt.Printf("%s is up
", f.Name)
}
}
}
On Linux, you'd need to use the RTNETLINK interface, and there appears to be a number of packages which implement wrappers around this layer.
Basically, you'd issue an RTM_GETLINK
request and then look at the type of each interface (ifi_type
field in that manual). The available types are here — you'd need those which are 802.2 (Ethernet) and 802.11 (Wi-Fi).
You could also try to implement a "low-tech" approach by first using net.Interfaces()
and then querying their classes by trying to read a file named "/sys/class/net/{iface_name}/type" (as explained here). That type is what the ifi_type
field described above contains.
Note that this approach has some drawbacks:
Interfaces might came and go anytime, so there's an inherent race between obtaining their list via net.Interfaces()
and querying each interface via that /sys
virtual filesystem.
That /sys
filesystem might not be mounted on a particular system. That would be quite unusual for a typical desktop or a server system but not so much for some "weird" installation (such as embedded etc).