如何构建编译gopacket包的测试用例?(gopacket抓取数据包、类似于tcpdump、Wireshark)

本篇文章介绍一下,如何构建gopacket测试用例

1、构建过程中遇到的问题

问题1:

-bash: ./gopacket-main: /lib/ld-musl-x86_64.so.1: bad ELF interpreter: No such file or directory

问题2:

#14 3.150 # github.com/google/gopacket/pcap
#14 3.150 /go/pkg/mod/github.com/google/[email protected]/pcap/pcap.go:30:22: undefined: pcapErrorNotActivated
#14 3.150 /go/pkg/mod/github.com/google/[email protected]/pcap/pcap.go:52:17: undefined: pcapTPtr
#14 3.150 /go/pkg/mod/github.com/google/[email protected]/pcap/pcap.go:64:10: undefined: pcapPkthdr
#14 3.150 /go/pkg/mod/github.com/google/[email protected]/pcap/pcap.go:103:6: undefined: pcapBpfProgram
#14 3.150 /go/pkg/mod/github.com/google/[email protected]/pcap/pcap.go:110:7: undefined: pcapPkthdr
#14 3.150 /go/pkg/mod/github.com/google/[email protected]/pcap/pcap.go:268:33: undefined: pcapErrorActivated
#14 3.150 /go/pkg/mod/github.com/google/[email protected]/pcap/pcap.go:269:33: undefined: pcapWarningPromisc
#14 3.150 /go/pkg/mod/github.com/google/[email protected]/pcap/pcap.go:270:33: undefined: pcapErrorNoSuchDevice
#14 3.150 /go/pkg/mod/github.com/google/[email protected]/pcap/pcap.go:271:33: undefined: pcapErrorDenied
#14 3.150 /go/pkg/mod/github.com/google/[email protected]/pcap/pcap.go:748:14: undefined: pcapTPtr
#14 3.150 /go/pkg/mod/github.com/google/[email protected]/pcap/pcap.go:271:33: too many errors

如果解决上面的问题呢?

2、测试环境说明

本次构建是在Mac环境下构建,构建的二进制文件在centos7.5环境下使用。

Mac和centos都是x86环境。

3、测试用例目录结构

如何构建编译gopacket包的测试用例?(gopacket抓取数据包、类似于tcpdump、Wireshark)_第1张图片

4、gopacket的测试用例

vim main.go

package main

import (
	"fmt"
	"github.com/google/gopacket/pcap"
)

func main() {
	devs, err := pcap.FindAllDevs()
	if err != nil {
		fmt.Errorf(err.Error())
	}

	for _, dev := range devs {
		fmt.Printf("----dev.Name:%v\n", dev.Name)
	}
}

5、构建方式

通过docker的方式进行构建,不会影响本地环境

5.1、Dockerfile

vim Dockerfile

FROM golang:1.18-alpine

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

RUN apk add --no-cache \
    libpcap-dev \
    g++ \
    curl \
    build-base \
    binutils-gold \
    bash \
    clang \
    llvm \
    libbpf-dev \
    linux-headers \
    net-tools \
    vim

WORKDIR /root
ADD main.go /root

ENV CGO_ENABLED=1 GOOS=linux GOARCH=amd64
ENV GOPROXY https://goproxy.cn

COPY go.mod .
COPY go.sum .
RUN go mod download

RUN go build -ldflags="-extldflags=-static -s -w" -o gopacket-main main.go

其中, libpcap-dev必须安装;其他 未测试。

文章一开始的问题,是如何引起的?

如何构建编译gopacket包的测试用例?(gopacket抓取数据包、类似于tcpdump、Wireshark)_第2张图片

如何构建编译gopacket包的测试用例?(gopacket抓取数据包、类似于tcpdump、Wireshark)_第3张图片

5.2、Makefile

参考Makefile的内容
vim Makefile

build:
	docker build -t golang:1.18-alpine-libpcap .

docker:
	docker run --rm -v /xxxxxxx/data:/app golang:1.18-alpine-libpcap cp gopacket-main /app

scp:
	scp data/gopacket-main [email protected]:/root

all:
	make build
	make docker
	make scp

如何构建编译gopacket包的测试用例?(gopacket抓取数据包、类似于tcpdump、Wireshark)_第4张图片

5.3、构建

如何构建编译gopacket包的测试用例?(gopacket抓取数据包、类似于tcpdump、Wireshark)_第5张图片

6、登录到centos上测试

如何构建编译gopacket包的测试用例?(gopacket抓取数据包、类似于tcpdump、Wireshark)_第6张图片

有价值的参考文档
https://blog.51cto.com/u_12617333/5779129



已发表的技术专栏(订阅即可观看所有专栏,直接点击题目查看)
0  grpc-go、protobuf、multus-cni 技术专栏 总入口

1  grpc-go 源码剖析与实战  文章目录

2  零入门Kubernetes网络实战

3  Protobuf介绍与实战 图文专栏  文章目录

4  multus-cni   文章目录(k8s多网络实现方案)

5  grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录

你可能感兴趣的:(kubernetes网络实战,kubernetes,flannel,calico,网络)