Files

40 lines
900 B
Go

package scanner
import (
"net"
"net/netip"
"os/exec"
"regexp"
"runtime"
)
var arpLine = regexp.MustCompile(`(\d+\.\d+\.\d+\.\d+)\D+?((?:[0-9a-fA-F]{1,2}[:-]){5}[0-9a-fA-F]{1,2})`)
// readARPExec parses the output of arp(8), which looks similar enough on
// macOS, the BSDs and Windows.
func readARPExec() (map[netip.Addr]net.HardwareAddr, error) {
args := []string{"-an"}
if runtime.GOOS == "windows" {
args = []string{"-a"}
}
out, err := exec.Command("arp", args...).Output()
if err != nil {
return nil, err
}
return parseARPOutput(string(out)), nil
}
func parseARPOutput(out string) map[netip.Addr]net.HardwareAddr {
table := make(map[netip.Addr]net.HardwareAddr)
for _, m := range arpLine.FindAllStringSubmatch(out, -1) {
ip, err := netip.ParseAddr(m[1])
if err != nil {
continue
}
if mac, ok := parseLooseMAC(m[2]); ok {
table[ip] = mac
}
}
return table
}