gpt4 book ai didi

java - Ping Sweep,能够进入某个范围

转载 作者:行者123 更新时间:2023-12-01 13:44:24 24 4
gpt4 key购买 nike

目前我正在尝试为用户输入的范围创建 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com