Question

Currently what I am trying to create is a ping sweep for a user entered range.

I have tried various sources on the internet for this but they are very vague or i just cant seem to get them to work, baring in mind i am very new to java.

one source i tried to completely type out but cant seem to get to work is located at - http://www.coderanch.com/t/205721/sockets/java/Determining-computers-network

and the code i have tried to use is...

import java.net.Inet4Address; 
import java.net.InetAddress; 
import java.util.Arrays; 
import java.io.IOException; 
import java.io.*; 
import java.util.Scanner; 
import jpcap.*; 
import jpcap.packet.*; 

public class ARP{ 
public static void main(String[] args) throws IOException{ 

//Obtain the list of network interfaces 
NetworkInterface[] devices = JpcapCaptor.getDeviceList(); 

//for each network interface 
for (int i = 0; i < devices.length; i++) { 
//print out its name and description 
System.out.println(i+": "+devices[i].name + "(" + devices[i].description+")"); 

//print out its datalink name and description 
System.out.println(" datalink: "+devices[i].datalink_name + "(" + devices[i].datalink_description+")"); 

//print out its MAC address 
System.out.print(" MAC address:"); 
for (byte b : devices[i].mac_address) 
System.out.print(Integer.toHexString(b&0xff) + ":"); 
System.out.println(); 

//print out its IP address, subnet mask and broadcast address 
for (NetworkInterfaceAddress a : devices[i].addresses) 
System.out.println(" address:"+a.address + " " + a.subnet + " "+ a.broadcast); 
} 

//NetworkInterface[] devices = JpcapCaptor.getDeviceList(); 
int index =1; // set index of the interface that you want to open. 

//Open an interface with openDevice(NetworkInterface intrface, int snaplen, boolean promics, int to_ms) 
final JpcapCaptor captor=JpcapCaptor.openDevice(devices[index], 65535, false, 20); 









//JpcapCaptor captor=JpcapCaptor.openDevice(device[1], 65535, false, 20); 

//call processPacket() to let Jpcap call PacketPrinter.receivePacket() for every packet capture. 
//captor.processPacket(10,new PacketPrinter()); 
//System.out.println(packet); 
//captor.close(); 

} 
}

the code above has many errors when i have tried to use it, am not entirely sure what im doing wrong.

Again i'm very new to java and it would be a great help if someone could point me in the right direction.

I have also tried to use this youtube tutorial which covers Port Scanning and Ping Sweeping, but not had much luck.

http://www.youtube.com/watch?v=FvycwyAat6s

Any help would be greatly appreciated,

Thanks,

Jamie

Was it helpful?

Solution

This code, which I got from here https://stackoverflow.com/a/14110872/868040, pings a device. You can easily modify this to ping a range of devices.

import java.io.*;
import java.util.*;

public class JavaPingExampleProgram
{

  public static void main(String args[]) 
  throws IOException
  {
    // create the ping command as a list of strings
    JavaPingExampleProgram ping = new JavaPingExampleProgram();
    List<String> commands = new ArrayList<String>();
    commands.add("ping");
    commands.add("-c");
    commands.add("5");
    commands.add("74.125.236.73");
    ping.doCommand(commands);
  }

  public void doCommand(List<String> command) 
  throws IOException
  {
    String s = null;

    ProcessBuilder pb = new ProcessBuilder(command);
    Process process = pb.start();

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

    // read the output from the command
    System.out.println("Here is the standard output of the command:\n");
    while ((s = stdInput.readLine()) != null)
    {
      System.out.println(s);
    }

    // read any errors from the attempted command
    System.out.println("Here is the standard error of the command (if any):\n");
    while ((s = stdError.readLine()) != null)
    {
      System.out.println(s);
    }
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top