gopacket解析FTP数据包

直接解析TCP

package main

import (
	"fmt"

	"github.com/google/gopacket/layers"

	"github.com/google/gopacket"
	"github.com/google/gopacket/pcap"
)

func main() {
	handle, _ := pcap.OpenOffline("ftp.pcap")
	defer handle.Close()
	packetSource := gopacket.NewPacketSource(
		handle,
		handle.LinkType(),
	)

	for packet := range packetSource.Packets() {

		ipLayer := packet.Layer(layers.LayerTypeIPv4)

		if ipLayer != nil {
			//ip, _ := ipLayer.(*layers.IPv4)

			tcpLayer := packet.Layer(layers.LayerTypeTCP)
			if tcpLayer != nil {
				fmt.Println(string(tcpLayer.LayerPayload()))
			}

		}

	}
	for {
	}
}

gopacket解析FTP数据包_第1张图片
gopacket解析FTP数据包_第2张图片

你可能感兴趣的:(Go相关,gopacket,TCP)