简体   繁体   English

Eclipse中的JNetPcap无法打印错误…Ubuntu 12.04

[英]JNetPcap in eclipse does not print error… Ubuntu 12.04

Have some problems with JNetPcap. JNetPcap有一些问题。

I uses Ubuntu 12.04, and trying to make packet snipper that based in java language. 我使用Ubuntu 12.04,并尝试制作基于Java语言的数据包监听器。

What I did is below. 我所做的如下。

  1. I have downloaded JNetPcap 1.3.0. 我已经下载了JNetPcap 1.3.0。

  2. And as tutorial said built a java project. 并按照教程中的说明构建了一个Java项目。 http://jnetpcap.com/examples/dumper <- this is the link. http://jnetpcap.com/examples/dumper <-这是链接。

  3. I typed just like that link and I got my first problem. 我输入的内容与该链接一样,但遇到了第一个问题。 PcapHandler Class is deprecated. 不推荐使用PcapHandler类。 So I find the document and replace it with ByteBufferHandler. 所以我找到了文档,并用ByteBufferHandler替换了它。

  4. Now I compile this project and got an unsatifiedLinked Error. 现在,我编译该项目,并出现了unsatifiedLinked错误。 I have tried with static block to load that library. 我尝试用静态块加载该库。 After some attempts I copied "libjnetpcap.so" to /usr/lib/ 经过一些尝试后,我将“ libjnetpcap.so”复制到了/ usr / lib /

  5. now I remove unsatisfiedLinked Error. 现在,我删除了不满意的链接错误。 but somehow it stops in 1st Error check. 但以某种方式在“第一个错误检查”中停止。 It prints "1st error check : ", then exit automatically. 打印“ 1st error check:”,然后自动退出。

    public static void main(String[] args) { 公共静态void main(String [] args){

     List<PcapIf> alldevs = new ArrayList<PcapIf>(); StringBuilder errbuff = new StringBuilder(); int r = Pcap.findAllDevs(alldevs, errbuff); //============1st check if(r == Pcap.NOT_OK || alldevs.isEmpty()){ System.err.printf("1st error check : %s\\n", errbuff.toString()); return; } PcapIf device = alldevs.get(1); //===================== END int snaplen = 64 * 1024; int flags = Pcap.MODE_PROMISCUOUS; int timeout = 10 * 1000; Pcap pcap = Pcap.openLive(device.getName(),snaplen, flags, timeout, errbuff); //============2nd check if(pcap == null){ System.err.printf("2nd error check : %s\\n", errbuff.toString()); return; } //===================== END String ofile = "/home/juneyoungoh/tmp_capture_file.cap"; final PcapDumper dumper = pcap.dumpOpen(ofile); ByteBufferHandler<PcapDumper> handler = new ByteBufferHandler<PcapDumper>() { @Override public void nextPacket(PcapHeader arg0, ByteBuffer arg1, PcapDumper arg2) { dumper.dump(arg0, arg1); } }; pcap.loop(10,handler, dumper); File file = new File(ofile); System.out.printf("%s file has %d bytes in it!\\n", ofile, file.length()); dumper.close(); pcap.close(); if(file.exists()){ file.delete(); } 

    } }

if is there any good reference or wonderful idea, please share. 如果有什么好的参考或好主意,请分享。

Thanks. 谢谢。

On Linux, a program will probably have to run as root, or with sufficient privileges granted in some other fashion, in order to be able to open any devices, and, currently, pcap_findalldevs() , which is presumably what the Pcap.findAllDevs method uses, tries to open each of the devices it finds, and only returns the devices it can open. 在Linux上,程序可能必须以root用户身份运行,或者具有以其他某种方式授予的足够特权,才能打开任何设备,并且当前要打开pcap_findalldevs() ,这大概就是Pcap.findAllDevs方法使用,尝试打开找到的每个设备,仅返回可以打开的设备。

So you'll have to run your Java program as root, or will somehow have to arrange that it have sufficient privileges (CAP_NET_RAW and CAP_NET_ADMIN) to get a list of network adapters and open those adapters. 因此,您必须以root用户身份运行Java程序,或者必须以某种方式安排它具有足够的特权(CAP_NET_RAW和CAP_NET_ADMIN)以获取网络适配器列表并打开这些适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM