2013-12-08 68 views
0

目前我正在创建的是用户输入范围的ping扫描。Ping扫描,能够输入范围

我已经尝试过各种网络资源,但它们都非常模糊,或者我只是不能让它们工作,在脑海中我很感兴趣。

一个源我试图完全打出来,但似乎无法得到的要努力位于 - 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很陌生,如果有人能指出我的正确方向,那将是一个很大的帮助。

我也试过使用这个涵盖端口扫描和Ping清扫的YouTube教程,但没有太多的运气。

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

任何帮助将不胜感激,

感谢,

杰米

回答

1

此代码,这是我从这里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); 
    } 
    } 
+0

这似乎为我工作,谢谢,出于好奇,我会检索MAC从ping扫描不会忽略作为即时通讯不知道从哪里开始,就看着它,但我硬是在这里得到这个职位上stackflow。 – user3080543

+0

如果您不介意mac地址位,您应该将答案标记为正确,请查看 http://www.mkyong.com/java/how-to-get-mac-address-in-java/ –

+0

如何修改此代码的范围ips? – Grant