您现在的位置是:首页 >其他 >如何构建编译gopacket包的测试用例?(gopacket抓取数据包、类似于tcpdump、Wireshark)网站首页其他
如何构建编译gopacket包的测试用例?(gopacket抓取数据包、类似于tcpdump、Wireshark)
简介如何构建编译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/gopacket@v1.1.19/pcap/pcap.go:30:22: undefined: pcapErrorNotActivated
#14 3.150 /go/pkg/mod/github.com/google/gopacket@v1.1.19/pcap/pcap.go:52:17: undefined: pcapTPtr
#14 3.150 /go/pkg/mod/github.com/google/gopacket@v1.1.19/pcap/pcap.go:64:10: undefined: pcapPkthdr
#14 3.150 /go/pkg/mod/github.com/google/gopacket@v1.1.19/pcap/pcap.go:103:6: undefined: pcapBpfProgram
#14 3.150 /go/pkg/mod/github.com/google/gopacket@v1.1.19/pcap/pcap.go:110:7: undefined: pcapPkthdr
#14 3.150 /go/pkg/mod/github.com/google/gopacket@v1.1.19/pcap/pcap.go:268:33: undefined: pcapErrorActivated
#14 3.150 /go/pkg/mod/github.com/google/gopacket@v1.1.19/pcap/pcap.go:269:33: undefined: pcapWarningPromisc
#14 3.150 /go/pkg/mod/github.com/google/gopacket@v1.1.19/pcap/pcap.go:270:33: undefined: pcapErrorNoSuchDevice
#14 3.150 /go/pkg/mod/github.com/google/gopacket@v1.1.19/pcap/pcap.go:271:33: undefined: pcapErrorDenied
#14 3.150 /go/pkg/mod/github.com/google/gopacket@v1.1.19/pcap/pcap.go:748:14: undefined: pcapTPtr
#14 3.150 /go/pkg/mod/github.com/google/gopacket@v1.1.19/pcap/pcap.go:271:33: too many errors
如果解决上面的问题呢?
2、测试环境说明 |
本次构建是在Mac环境下构建,构建的二进制文件在centos7.5环境下使用。
Mac和centos都是x86环境。
3、测试用例目录结构 |
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
", 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必须安装;其他 未测试。
文章一开始的问题,是如何引起的?
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 root@10.211.55.130:/root
all:
make build
make docker
make scp
5.3、构建 |
6、登录到centos上测试 |
有价值的参考文档
https://blog.51cto.com/u_12617333/5779129
已发表的技术专栏(订阅即可观看所有专栏,直接点击题目查看)
0 grpc-go、protobuf、multus-cni 技术专栏 总入口
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。