Skip to content

Instantly share code, notes, and snippets.

@Ultraporing
Last active January 3, 2021 22:12
Show Gist options
  • Save Ultraporing/899caff948114aeb985bfb8c65d4cf54 to your computer and use it in GitHub Desktop.
Save Ultraporing/899caff948114aeb985bfb8c65d4cf54 to your computer and use it in GitHub Desktop.
Physical network interfaces filtered by MAC address of popular VM's, Teredo Tunneling Pseudo-Interface and loopback interfaces. This allows you to get all/first physical network interfaces of this OS/machine. The macAddrPartsToCheck needs to get more MAC address parts added to support other non physical network interfaces.
package main
import (
"errors"
"fmt"
"net"
"strings"
)
// The main function, Only for demonstration
func main() {
allPhysI, err := GetAllPhysicalInterface()
if err != nil {
return
}
var firstPhysI *net.Interface
firstPhysI, err = GetFirstPhysicalInterface()
if err != nil {
return
}
prettyPrint(firstPhysI, allPhysI)
}
// Some pretty printing, Only for demonstration
func prettyPrint(first *net.Interface, all *[]net.Interface) {
fmt.Println("\n")
fmt.Println("First found physical interface:\n========================================")
fmt.Println("[",first.Index,"]",first.Name,"\n----------------------------------------\n\n")
fmt.Println("All found physical interfaces:\n========================================")
for _, in := range *all {
fmt.Println("[",in.Index,"]",in.Name,"\n----------------------------------------")
}
fmt.Println("\n")
}
// Compares characters until the end of lString is reached or a difference was found.
func rStrBeginsWithLstring(lString string, rString string, err *error) (bool) {
if len(rString) < len(lString) {
(*err) = errors.New("source is bigger than target string")
return false
}
ls := strings.ToLower(lString)
lt := strings.ToLower(rString)
for i := 0; i < len(lString); i++ {
if ls[i] != lt[i] {
return false
}
}
return true
}
// Filters the possible physical interface address by comparing it to known popular VM Software adresses
// and Teredo Tunneling Pseudo-Interface.
func isPhysicalInterface(addr *net.HardwareAddr) (bool) {
var err *error = nil
// Mac Address parts to look for, and identify non physical devices. There may be more, update me!
var macAddrPartsToCheck []string =
[]string{
"00:03:FF", // Microsoft Hyper-V, Virtual Server, Virtual PC
"0A:00:27", // VirtualBox
"00:00:00:00:00", // Teredo Tunneling Pseudo-Interface
"00:50:56", "00:1C:14", // VMware ESX 3, Server, Workstation, Player
"00:0C:29", "00:05:69", // VMware ESX 3, Server, Workstation, Player
"00:1C:42", // Microsoft Hyper-V, Virtual Server, Virtual PC
"00:0F:4B", // Virtual Iron 4
"00:16:3E", // Red Hat Xen, Oracle VM, XenSource, Novell Xen
"08:00:27", // Sun xVM VirtualBox
}
for _, macPart := range macAddrPartsToCheck {
if rStrBeginsWithLstring(macPart, (*addr).String(), err) {
return false
}
}
return true
}
// Gets the first physical interface based on filter results, ignoring Loopback interfaces
func GetFirstPhysicalInterface() (*net.Interface, error){
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, element := range ifaces {
if element.Flags & net.FlagLoopback == 0 {
if isPhysicalInterface(&element.HardwareAddr) {
return &element, nil
}
}
}
return nil, nil
}
// Gets all physical interfaces based on filter results, ignoring Loopback interfaces
func GetAllPhysicalInterface() (*[]net.Interface, error){
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var outInterfaces []net.Interface
for _, element := range ifaces {
if element.Flags & net.FlagLoopback == 0 {
if isPhysicalInterface(&element.HardwareAddr) {
outInterfaces = append(outInterfaces, element)
}
}
}
return &outInterfaces, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment