arp/arp.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package arp import ( "fmt" "network-scan/definitions" "os/exec" "regexp" "runtime" "strings" ) func GetDevices() ([]definitions.Device, error) { if runtime.GOOS == "windows" { return nil, fmt.Errorf("this program is not supported on Windows") } cmd := exec.Command("arp", "-a") output, err := cmd.Output() if err != nil { return nil, err } return parseARPTable(string(output)), nil } func parseARPTable(arpTable string) []definitions.Device { var devices []definitions.Device pattern := regexp.MustCompile(`(\d+\.\d+\.\d+\.\d+).*?(\S+:\S+:\S+:\S+:\S+:\S+)`) lines := strings.Split(arpTable, "\n") for _, line := range lines { matches := pattern.FindStringSubmatch(line) if len(matches) == 3 { device := definitions.Device{ IP: matches[1], MAC: matches[2], } devices = append(devices, device) } } return devices } |