简体   繁体   English

如何使用Java(线程)ping SNMP设备的IP地址范围?

[英]How to ping the snmp devices for a range of IP addresses using java (thread)?

I need to create a thread pool to ping a range of ip addresses simultaneously using java language. 我需要创建一个线程池来使用Java语言同时ping范围内的IP地址。

Please help me out. 请帮帮我。

Edited 1 编辑1

If there are lots of thread created, then whether we have to call stop() method explicitly to stop the thread? 如果创建了很多线程,那么是否必须显式调用stop()方法来停止线程? or will it be taken care? 还是会注意?

Here's an implementation of the pingig subtask. 这是pingig子任务的实现 Creating a thread pool and threads with the referenced payload shouldn't be too complicated. 用引用的有效负载创建线程池和线程应该不会太复杂。

Edit 编辑

If you can modify the client code on these devices, I suggest a custom protocol instead of using the echo port - something like a heartbeat where you frequently send a small message to the client (on the standard or a different port) and expect an answer within a defined time. 如果您可以在这些设备上修改客户端代码,则建议您使用自定义协议,而不要使用回显端口-像心跳一样,您经常在标准或其他端口上向客户端发送一条小消息并期望得到答案在规定的时间内。

Edit 2 编辑2

For the thread basics, I really suggest looking at a Java tutorial. 对于线程基础知识,我真的建议您阅读Java教程。 To start with: implement a class like public class PingThread extends Thread and place the code from the link inside a while(true) {...} loop in the run method. 首先,实现一个类似public class PingThread extends Thread并将链接中的代码放在run方法的while(true) {...}循环内。 Use Thread.sleep() to add a wait time between to pings in the the same loop. 使用Thread.sleep()在同一循环中的ping之间添加等待时间。 If you really need a ThreadGroup, override the constructor Thread(ThredGroup group, String name) so that a PingThread can be created in your specified group. 如果确实需要ThreadGroup,请重写构造函数Thread(ThredGroup group, String name)以便可以在指定的组中创建PingThread。

You may want to implement a switch to stop a PingThread (should be covered by almost every Java tutorial), 您可能想实现一个开关来停止PingThread(几乎所有Java教程都应该介绍),

Java has no implementation of ICMP by default, so it's not possible to ping a host from Java with the standard library. Java默认情况下没有实现ICMP,因此无法使用标准库从Java ping主机。 Your other options are to look for a Java ICMP implementation (I don't know if one exists), or to call the 'ping' executable on your system from Java and parse the output. 您的其他选择是寻找Java ICMP实现(我不知道是否存在),或者从Java调用系统上的“ ping”可执行文件并解析输出。

Edit: Andreas_D's link indicates that InetAddress.isReachable() uses an ICMP echo request to ping a host, so that's how you can implement the ping. 编辑:Andreas_D的链接指示InetAddress.isReachable()使用ICMP回显请求来ping通主机,因此这就是实现ping的方式。

You can take the code for the ReachableTest from that page and change the ReachableTest class into a Runnable which you can then run in its own thread or by using the executor service from java.util.concurrent: 您可以从该页面获取ReachableTest的代码,并将ReachableTest类更改为Runnable,然后可以在其自己的线程中运行,也可以使用java.util.concurrent中的执行程序服务运行该Runnable:

public class ReachableTest implements Runnable {
  private String host;

  public ReachableTest(String host) {
    this.host = host;
  }

  public void run() {
    try {
      InetAddress address = InetAddress.getByName(host);
      System.out.println("Name: " + address.getHostName());
      System.out.println("Addr: " + address.getHostAddress());
      System.out.println("Reach: " + address.isReachable(3000));
    }
    catch (UnknownHostException e) {
      System.err.println("Unable to lookup " + host);
    }
    catch (IOException e) {
      System.err.println("Unable to reach " + host);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM