概述
1.下载libpcap
www.tcpdump.org
2.安装 GNU M4
命令: sudo apt-get install m4这个是编译flex 必备的环境,否则会提示“GNU M4 1.4 is required” 的错误
3.安装 flex
命令: sudo apt-get install flex
没有flex ,直接安装libpcap 会提示“Your operating system's lex is insufficient to compile libpcap” 错误。
4.编译 bison
命令: sudo apt-get install bison
在安装flex 后直接安装libpcap 会提示“don't have both flex and bison;reverting to lex/yacc” 错误,前面安装的是flex ,就需要搭配bison
5.编译 libpcap
上面四步完成后,就可以使用下面三个指令安装libpcap 环境: 切换到libpcap 目录下( 具体可查看libcap 目 录下官方提供的install 文档)
./configure
make
sudo make install
6.测试程序
#include <string.h>
#include <stdlib.h>
#include <pcap.h>
#define MAXBYTE2CAPTURE 2048
void processPacket(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet) {
int i = 0, *counter = (int *)arg;
printf("Packet Count: %dn", ++(*counter));
printf("Received Packet Size: %dn", pkthdr->len);
printf("Payload:n");
for (i = 0; i < pkthdr->len; i++) {
if (isprint(packet[i]))
printf("%c ", packet[i]);
else
printf(". ");
if ((i % 16 == 0 && i != 0) || i == pkthdr->len-1)
printf("n");
}
return;
}
int main() {
int i = 0, count = 0;
pcap_t *descr = NULL;
char errbuf[PCAP_ERRBUF_SIZE], *device = NULL;
memset(errbuf, 0, PCAP_ERRBUF_SIZE);
/* Get the name of the first device suitable for capture */
device = pcap_lookupdev(errbuf);
printf("Opening device %sn", device);
/* Open device in promiscuous mode */
descr = pcap_open_live(device, MAXBYTE2CAPTURE, 1, 512, errbuf);
/* Loop forever & call processPacket() for every received packet */
pcap_loop(descr, -1, processPacket, (u_char *)&count);
return 0;
}
7.编译
gcc -o device device.c -lpcap
出现error while loading shared libraries: libpcap.so.1: cannot open shared object file: No such file or directory
需要 sudo ln -s /usr/local/lib/libpcap.so.1 /usr/lib/libpcap.so.1
最后
以上就是追寻电话为你收集整理的linux安装libpcap的全部内容,希望文章能够帮你解决linux安装libpcap所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复