代码找不到任何设备,我想知道pcap_lookupdev()是做什么的? 谢谢
#include <pcap.h> int main(int argc, char *argv[]) { pcap_t *handle; char *dev;// = "eth0"; char errbuf[PCAP_ERRBUF_SIZE]; dev = pcap_lookupdev(errbuf); if (dev == NULL) { fprintf(stderr, "Couldn't find default device: %s\n", errbuf); return(2); } printf("Device: %s\n", dev); return(0); handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf); if (handle == NULL) { fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf); return(2); } }
pcap_lookupdev似乎只是返回它可以找到的第一个设备(如果有的话),除了回环设备。
你是以root身份运行吗? 普通用户将不被允许打开或检查这些设备。
就我个人而言,我发现pcap_lookupdev毫无用处,因为您无法控制它所提供的设备。
当你正在编译pcap编程。 您需要在具有root权限的程序文件末尾提到-lpcap
。 例如。
#gcc YourFileName.c -lpcap #./a.out or #gcc -Wall -o YourFileName YourFileName.c -lpcap #./YourFileName
这里#表示root用户,gcc是GNU编译器集合,-lpcap表示使用libpcap库
代码必须以root身份编译,链接和执行:
sudo gcc -o dev-find dev-find.c lpcap
为我工作。