目前,我正在尝试创建的是用户输入范围的ping扫描。
我已经尝试了互联网上的各种资源,但是它们非常模糊,或者我似乎无法使它们正常工作,因此请记住我对Java非常陌生。
我试图完全键入但无法正常工作的一个来源位于-http://www.coderanch.com/t/205721/sockets/java/Determining-computers-network
我尝试使用的代码是...
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();
}
}
上面的代码在我尝试使用它时有很多错误,不能完全确定我在做什么错。
同样,我对Java还是很陌生,如果有人可以指出正确的方向,那将对您有很大的帮助。
我也尝试过使用这个YouTube教程,该教程涵盖了端口扫描和Ping扫描,但是运气不佳。
http://www.youtube.com/watch?v=FvycwyAat6s
任何帮助将不胜感激,
谢谢,
杰米
最佳答案
我从这里https://stackoverflow.com/a/14110872/868040获得的这段代码可对设备执行ping操作。您可以轻松地对此进行修改以对一系列设备执行ping操作。
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);
}
}
关于java - Ping Sweep,具有输入范围的能力,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20457802/