-
Notifications
You must be signed in to change notification settings - Fork 0
/
caputure_dhcp.go
56 lines (48 loc) · 1.18 KB
/
caputure_dhcp.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
// +build ignore
package main
import (
"fmt"
"os"
"time"
"log"
"path/filepath"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/layers"
)
var (
device string
snapshotLen int32 = 1024
promiscuous bool = false
err error
timeout time.Duration = 30 * time.Second
handle *pcap.Handle
)
func main(){
if len(os.Args) != 2{
fmt.Printf("Usage: %s <interface>\n", filepath.Base(os.Args[0]))
return
}
device = os.Args[1]
fmt.Printf("interface: %s\n", device)
handle, err = pcap.OpenLive(device, snapshotLen, promiscuous, timeout)
if err != nil {
log.Fatal(err)
}
defer handle.Close()
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
printDHCP(packet)
// fmt.Println(packet)
}
}
func printDHCP(packet gopacket.Packet){
dhcpLayer := packet.Layer(layers.LayerTypeDHCPv4)
if dhcpLayer != nil {
fmt.Println(time.Now())
dhcpPacket, _ := dhcpLayer.(*layers.DHCPv4)
fmt.Println("Operation: ", dhcpPacket.Operation)
fmt.Println("Options: ", dhcpPacket.Options)
fmt.Println("-------------------------------------------------")
}
}