linux安装libpcap

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: %d\n", ++(*counter));  
    printf("Received Packet Size: %d\n", 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 %s\n", 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值