linux安装libpcap

来源:互联网 发布:电脑画画用什么软件 编辑:程序博客网 时间:2024/06/06 05:04

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


0 0