[sdnog] sniffing the network by using JNetPcap

AYA FATHI faya862 at gmail.com
Sat Oct 15 13:23:22 SAST 2016


Hi all :)

Am trying to write  code sniffing the network by using JNetPcap lib on
eclipse

while running the code their is no error but its not capturing any thing

any help

the code as show below



import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.Queue;

import java.util.concurrent.ArrayBlockingQueue;



import org.jnetpcap.ByteBufferHandler;

import org.jnetpcap.Pcap;

import org.jnetpcap.PcapHeader;

import org.jnetpcap.PcapIf;

import org.jnetpcap.packet.PcapPacket;

import org.jnetpcap.packet.PcapPacketHandler;





public class  wer {





    public static void main(String[] args) {

        List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled
with NICs

        StringBuilder errbuf = new StringBuilder(); // For any error msgs



        int r = Pcap.findAllDevs(alldevs, errbuf);



        if (r == Pcap.NOT_OK || alldevs.isEmpty()) {

            System.err.printf("Can't read list of devices, error is %s",
errbuf

                .toString());

            return;

        }



        System.out.println("Network devices found:");



        int i = 0;

        for (PcapIf device : alldevs) {

            String description = (device.getDescription() != null) ?
device.getDescription() : "No description available";

            System.out.printf("#%d: %s [%s]\n", i++, device.getName(),
description);

        }



        PcapIf device = alldevs.get(3); // We know we have atleast 1 device



        System.out.printf("\nChoosing '%s' on your behalf:\n",
(device.getDescription() != null) ? device.getDescription()  :
device.getName());




/***************************************************************************

         * Second we open up the selected device


 **************************************************************************/


        int snaplen =  2048;           // Capture all packets, no trucation


        int flags =  Pcap.MODE_PROMISCUOUS; // capture all packets

        int timeout = 60 * 10000;           // 10 seconds in millis



         Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags,
timeout, errbuf);

         if (pcap == null) {

             System.err.printf("Error while opening device for capture: "

                 + errbuf.toString());

             return;

         }









        PcapPacketHandler<String> jpacketHandler = new
PcapPacketHandler<String>() {



            public void nextPacket(PcapPacket packet, String user) {



                System.out.printf("Received packet at %s caplen=%-4d
len=%-4d %s\n",

                    new
Date(packet.getCaptureHeader().timestampInMillis()),

                    packet.getCaptureHeader().caplen(),  // Length actually
captured

                    packet.getCaptureHeader().wirelen(), // Original length


                    user                                 // User supplied
object

                    );

           System.out.print("\n fine"); }

        };



        pcap.loop(10, jpacketHandler, "jNetPcap rocks!");















       pcap.close();









    }




}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sdnog.sd/pipermail/sdnog/attachments/20161015/9f1b3391/attachment.html>


More information about the sdnog mailing list