Ubuntu10

环境为Ubuntu10.10+libpcap-1.1.1.tar.gz

1、Libpcap下载

  Libpcap的官方网站是http://tcpdump.org/release,可以从该网站下载最新版本,当前系统使用的Libpcap安装文件是libpcap-1.1.1.tar.gz

2、解压

   tar zxvf libpcap-1.1.1.tar.gz

3、配置

  切换近libpcap解压目录libpcap-1.1.1,使用configure配置命令生成Makefile文件。

  ./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 .

  解决方法:

?
sudo apt-get install flex

  

4、编译

  make

    可能会遇到yacc错误:

?
yacc -d ./parse.y<br>make: yacc: Command not found<br>make: *** [parse.c] Error 127

  解决方法:

 

?
sudo apt-get install -y byacc

5、安装

  sudo make install

   注意:要加上sudo,不然会出现权限问题。

6、示例

  testlibpcap.c文件,目的是为了查询网络设备。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
#include<stdlib.h>
#include<pcap.h>
#include<errno.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
 
int main( int argc, char *argv[])
{
     char *dev;
     char *net;
     char *mask;
     int ret;
     char errbuf[PCAP_ERRBUF_SIZE];
     bpf_u_int32 netp;
     bpf_u_int32 maskp;
     struct in_addr addr;
     dev = pcap_lookupdev(errbuf);
     if (dev ==NULL)
     {
         printf ( "%s\n" ,errbuf);
         exit (1);
     }
     printf ( "设备名:%s\n" ,dev);
     return 0;
}

  然后编译:

?
gcc  testlibpcap.c -lpcap -o testlibpcap

    gcc编译后,运行./testlibpcap会出错:

?
: error while loading shared libraries: libpcap.so.1: cannot open shared object file: No such file or directory

    解决方法:

     到/usr/lib下找到libpcap.so.1.x.x文件,复制一个备份,重命名为libpcap.so.1,将libpcap.so.1后拷贝文件到/usr/lib目录下,就可以了。

 还有一点需要注意:

  如果直接这样运行./testlibpcap,结果肯定是“no suitable device found”,原因是权限不够,所以,还需要这样运行才能结果正常。

  sudo  ./testlibpcap

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值