arp_linux.go 695 B

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