35 lines
710 B
Go
35 lines
710 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"time"
|
||
|
|
||
|
"github.com/google/gopacket"
|
||
|
"github.com/google/gopacket/pcap"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
device string = "netrave0"
|
||
|
snapshot_len int32 = 2048
|
||
|
promiscuous bool = false
|
||
|
err error
|
||
|
timeout time.Duration = 30 * time.Second
|
||
|
handle *pcap.Handle
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
// Open device
|
||
|
handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
defer handle.Close()
|
||
|
|
||
|
// Use the handle as a packet source to process all packets
|
||
|
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
|
||
|
for packet := range packetSource.Packets() {
|
||
|
fmt.Println(packet) // Do something with a packet here.
|
||
|
}
|
||
|
}
|