Skip to content

Instantly share code, notes, and snippets.

@aeppert
Created February 10, 2017 22:24
Show Gist options
  • Select an option

  • Save aeppert/f00f49bc1eef9f6eff5f7c656f5f47ed to your computer and use it in GitHub Desktop.

Select an option

Save aeppert/f00f49bc1eef9f6eff5f7c656f5f47ed to your computer and use it in GitHub Desktop.
Count Packets in a PCAP
package main
import (
"flag"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
"log"
)
var (
pcapFile = flag.String("r", "", "Filename to read from")
handle *pcap.Handle
err error
)
func main() {
flag.Parse()
packet_count = 0
// Open file instead of device
handle, err = pcap.OpenOffline(*pcapFile)
if err != nil {
log.Fatal(err)
}
defer handle.Close()
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for range packetSource.Packets() {
packet_count++
}
fmt.Printf("%d\n", packet_count)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment