Created
July 13, 2014 15:16
-
-
Save jvehent/bdc646b16424f03cab39 to your computer and use it in GitHub Desktop.
Portable packet capture in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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