Linux下libpcap的安装

来源:互联网 发布:海量数据和数据港 编辑:程序博客网 时间:2024/06/07 06:06

1, 环境: Ubuntu16.0 + libpcap-1.4.0.tar.gz

2,Libpcap下载: 官方地址-http://www.tcpdump.org/#latest-release 即可下载最新版本的libpcap。

3,解压libpcap-1.4.0.tar.gz: tar -zxvf libpcap-1.4.0.tar.gz

4,配置生成makefile文件:cd到libpcap解压目录libpcap-1.4.0, 执行 ./configure。执行到这一步可能会遇到缺少flex包的问题:

configure: error: Your operating system's lex is insufficient to compile
 libpcap.  flex is a lex replacement that has many advantages, including
 being able to compile libpcap.  For more information, see
 http://www.gnu.org/software/flex/flex.html .

解决办法:安装flex包---- sudo apt-get install flex

5,make

可能会遇到yacc包错误:

yacc -d grammar.y
make: yacc: Command not found
Makefile:460: recipe for target 'grammar.c' failed
make: *** [grammar.c] Error 127

解决办法:安装byacc包--- sudo apt-get install -y byacc

6,安装: sudo make install

7,完成安装: 可用 man pcap查看帮助

main.c

    #include <pcap.h>      #include <stdio.h>            int main()      {        char errBuf[PCAP_ERRBUF_SIZE], * device;                device = pcap_lookupdev(errBuf);                if(device)        {          printf("success: device: %s\n", device);        }        else        {          printf("error: %s\n", errBuf);        }                return 0;      }  

可以成功编译,不过运行的时候却提示找不到libpcap.so.1,因为libpcap.so.1默认安装到了/usr/local/lib下,我们做一个符号链接到/usr/lib/下即可:ln -s /usr/local/lib/libpcap.so.1  /usr/lib/


test.c

    #include<stdio.h>    #include<string.h>    #include<pcap.h>    #include<sys/socket.h>    #include<netinet/in.h>    #include<netinet/if_ether.h>    #include<netinet/ip.h>    #include<netinet/udp.h>    #include<netinet/tcp.h>    #include<netinet/ip_icmp.h>    #define max 1024   int main(int argc,char *argv[])    {        pcap_if_t *alldev;        pcap_if_t *p;        char error[100];                int i=0,num;         if(pcap_findalldevs(&alldev,error)==-1)        {            printf("find all devices is error\n");            return 0;        }        for(p=alldev;p;p=p->next)        {            printf("%d:%s\n",++i,p->name);            if(p->description)            {                printf("%s\n",p->description);            }        }        return 1;    }


2 0
原创粉丝点击