arp_linux.go 721 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package neighbors
  2. import (
  3. "context"
  4. "encoding/json"
  5. "os/exec"
  6. "goa.design/clue/log"
  7. )
  8. const (
  9. arpCmd = "ip"
  10. )
  11. type (
  12. arpEntry struct {
  13. IPAddress string `json:"dst"`
  14. MACAddress string `json:"lladdr"`
  15. Interface string `json:"dev"`
  16. }
  17. )
  18. func (a *arp) entries(ctx context.Context, ifs Interfaces) (entries []arpEntry, err error) {
  19. cmd := exec.CommandContext(ctx, a.cmd, "-family", "inet", "-json", "neighbor", "show", "nud", "reachable")
  20. if len(ifs) == 1 {
  21. for ifi := range ifs {
  22. cmd.Args = append(cmd.Args, "dev", ifi)
  23. }
  24. }
  25. log.Debug(ctx, log.KV{K: "cmd", V: cmd})
  26. b, err := cmd.Output()
  27. if err != nil {
  28. return
  29. }
  30. if err = json.Unmarshal(b, &entries); err != nil {
  31. return
  32. }
  33. return
  34. }