Skip to content

Instantly share code, notes, and snippets.

@jvehent
Created July 13, 2014 15:16
Show Gist options
  • Save jvehent/bdc646b16424f03cab39 to your computer and use it in GitHub Desktop.
Save jvehent/bdc646b16424f03cab39 to your computer and use it in GitHub Desktop.
Portable packet capture in Go
// To build, you will need:
// $ sudo yum install libpcap-devel
// and
// $ go get -u code.google.com/p/gopacket/pcap
// then go build pcap.go and copy the resulting pcap binary anywhere
// (on the same platform you build on)
package main
import (
"fmt"
"code.google.com/p/gopacket"
"code.google.com/p/gopacket/pcap"
)
func main() {
if handle, err := pcap.OpenLive("eth0", 1600, true, 0); err != nil {
panic(err)
} else if err := handle.SetBPFFilter("tcp and port 80"); err != nil { // optional
panic(err)
} else {
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
fmt.Println(packet.String())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment