github.com/gopacket/gopacket@v1.1.0/examples/snoopread/main.go (about)

     1  // Copyright 2019 The GoPacket Authors. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style license
     4  // that can be found in the LICENSE file in the root of the source
     5  // tree.
     6  
     7  // snoopread is a example for read a snoop file using
     8  // gopacket and its subpackages and output the decoded data with a package count
     9  package main
    10  
    11  import (
    12  	"fmt"
    13  	"log"
    14  	"os"
    15  
    16  	"github.com/gopacket/gopacket"
    17  	"github.com/gopacket/gopacket/pcapgo"
    18  )
    19  
    20  func main() {
    21  	//download snoop from https://wiki.wireshark.org/SampleCaptures
    22  	f, err := os.Open("example.snoop")
    23  	if err != nil {
    24  		log.Fatal(err)
    25  		return
    26  	}
    27  	defer f.Close()
    28  	handle, err := pcapgo.NewSnoopReader(f)
    29  	if err != nil {
    30  		log.Fatal(err)
    31  		return
    32  	}
    33  
    34  	lt, err := handle.LinkType()
    35  	if err != nil {
    36  		log.Fatal(err)
    37  		return
    38  	}
    39  	packetSource := gopacket.NewPacketSource(handle, lt)
    40  
    41  	cnt := 0
    42  	for packet := range packetSource.Packets() {
    43  		fmt.Println(packet)
    44  		cnt++
    45  	}
    46  	fmt.Printf("Packet count: %d\n", cnt)
    47  }