1. 程式人生 > >在Ubuntu安裝測試libpcap(Install the libpcap in Ubuntu 11.04)

在Ubuntu安裝測試libpcap(Install the libpcap in Ubuntu 11.04)

1.Download the tar of libpcap

in site: http://www.tcpdump.org/

My File is libpcap-1.2.1.tar.gz.

tar -zxvf libpcap-1.3.0.tar.gz 

2.Now, We get the folder libpcap-1.3.0

cd libpcap-1.3.0

3.we need install flex
sudo apt-get install flex

if don't do this step will:

configure: error: Your operatingsystem'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 .
4. Configure
./configure

5.Install byacc

sudo apt-get install byacc

if not do this step will :
yacc -d grammar.y
make: yacc: Command not found
make: *** [grammar.c] Error 127

6. make

7. sudo make install

8. Test
.

vim testlibpcap.c

#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 errbuf[PCAP_ERRBUF_SIZE];
    dev = pcap_lookupdev(errbuf);
    if(dev ==NULL)
    {
        printf("%s\n",errbuf);
        exit(1);
    }
    printf("裝置名:%s\n",dev);
    return 0;
}
gcc  testlibpcap.c -o testlibpcap -lpcap -Wall

9 ln a link for libpcap
sudo ln -s /usr/lib/libpcap.1.X.X /usr/lib/libpcap.1

if don't do this step, 10th step will:

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

10 Run it!

sudo ./testlibpcap
If it show:

裝置名:XXX

Congratulation for  your success !