forked from mdlayher/arp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (48 loc) · 1.26 KB
/
main.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Command arpc provides a simple ARP client which can be used to retrieve
// hardware addresses of other machines in a LAN using their IPv4 address.
package main
import (
"flag"
"fmt"
"log"
"net"
"net/netip"
"time"
"github.com/mdlayher/arp"
)
var (
// durFlag is used to set a timeout for an ARP request
durFlag = flag.Duration("d", 1*time.Second, "timeout for ARP request")
// ifaceFlag is used to set a network interface for ARP requests
ifaceFlag = flag.String("i", "eth0", "network interface to use for ARP request")
// ipFlag is used to set an IPv4 address destination for an ARP request
ipFlag = flag.String("ip", "", "IPv4 address destination for ARP request")
)
func main() {
flag.Parse()
// Ensure valid network interface
ifi, err := net.InterfaceByName(*ifaceFlag)
if err != nil {
log.Fatal(err)
}
// Set up ARP client with socket
c, err := arp.Dial(ifi)
if err != nil {
log.Fatal(err)
}
defer c.Close()
// Set request deadline from flag
if err := c.SetDeadline(time.Now().Add(*durFlag)); err != nil {
log.Fatal(err)
}
// Request hardware address for IP address
ip, err := netip.ParseAddr(*ipFlag)
if err != nil {
log.Fatal(err)
}
mac, err := c.Resolve(ip)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s -> %s", ip, mac)
}