- 浏览: 61964 次
- 性别:
- 来自: 上海
最新评论
-
mzba520:
你的图片都挂了
Android扫描条形码实现 -
jacking124:
如果你上图,效果会更好的。
jqGrid的使用之---subGird -
netcomm:
在看了Nagios等开源监控软件后,感觉在具体使用的时候 ...
开源监控软件 -
zhengwenyan:
网管软件的设计比较繁琐!!
开源监控软件 -
hewei870723:
topcloud 写道怎么能没有OpenNMS呢
我这里只是部 ...
开源监控软件
一、纯Java实现ICMP的ping命令
import java.io.*;
import java.net.*;
import java.nio.channels.*;
import java.util.*;
import java.util.regex.*;
public class Ping {
static int DAYTIME_PORT = 13;
static int port = DAYTIME_PORT;
static class Target {
InetSocketAddress address;
SocketChannel channel;
Exception failure;
long connectStart;
long connectFinish = 0;
boolean shown = false;
Target(String host) {
try {
address = new InetSocketAddress(InetAddress.getByName(host),
port);
} catch (IOException x) {
failure = x;
}
}
void show() {
String result;
if (connectFinish != 0)
result = Long.toString(connectFinish - connectStart) + "ms";
else if (failure != null)
result = failure.toString();
else
result = "Timed out";
System.out.println(address + " : " + result);
shown = true;
}
}
static class Printer extends Thread {
LinkedList pending = new LinkedList();
Printer() {
setName("Printer");
setDaemon(true);
}
void add(Target t) {
synchronized (pending) {
pending.add(t);
pending.notify();
}
}
public void run() {
try {
for (;;) {
Target t = null;
synchronized (pending) {
while (pending.size() == 0)
pending.wait();
t = (Target) pending.removeFirst();
}
t.show();
}
} catch (InterruptedException x) {
return;
}
}
}
static class Connector extends Thread {
Selector sel;
Printer printer;
LinkedList pending = new LinkedList();
Connector(Printer pr) throws IOException {
printer = pr;
sel = Selector.open();
setName("Connector");
}
void add(Target t) {
SocketChannel sc = null;
try {
sc = SocketChannel.open();
sc.configureBlocking(false);
boolean connected = sc.connect(t.address);
t.channel = sc;
t.connectStart = System.currentTimeMillis();
if (connected) {
t.connectFinish = t.connectStart;
sc.close();
printer.add(t);
} else {
synchronized (pending) {
pending.add(t);
}
sel.wakeup();
}
} catch (IOException x) {
if (sc != null) {
try {
sc.close();
} catch (IOException xx) {
}
}
t.failure = x;
printer.add(t);
}
}
void processPendingTargets() throws IOException {
synchronized (pending) {
while (pending.size() > 0) {
Target t = (Target) pending.removeFirst();
try {
t.channel.register(sel, SelectionKey.OP_CONNECT, t);
} catch (IOException x) {
t.channel.close();
t.failure = x;
printer.add(t);
}
}
}
}
void processSelectedKeys() throws IOException {
for (Iterator i = sel.selectedKeys().iterator(); i.hasNext();) {
SelectionKey sk = (SelectionKey) i.next();
i.remove();
Target t = (Target) sk.attachment();
SocketChannel sc = (SocketChannel) sk.channel();
try {
if (sc.finishConnect()) {
sk.cancel();
t.connectFinish = System.currentTimeMillis();
sc.close();
printer.add(t);
}
} catch (IOException x) {
sc.close();
t.failure = x;
printer.add(t);
}
}
}
volatile boolean shutdown = false;
void shutdown() {
shutdown = true;
sel.wakeup();
}
public void run() {
for (;;) {
try {
int n = sel.select();
if (n > 0)
processSelectedKeys();
processPendingTargets();
if (shutdown) {
sel.close();
return;
}
} catch (IOException x) {
x.printStackTrace();
}
}
}
}
public static void main(String[] args) throws InterruptedException,
IOException {
args = new String[] { "8888", "192.168.10.193" };
if (args.length < 1) {
System.err.println("Usage: java Ping [port] host...");
return;
}
int firstArg = 0;
if (Pattern.matches("[0-9]+", args[0])) {
port = Integer.parseInt(args[0]);
firstArg = 1;
}
Printer printer = new Printer();
printer.start();
Connector connector = new Connector(printer);
connector.start();
LinkedList targets = new LinkedList();
for (int i = firstArg; i < args.length; i++) {
Target t = new Target(args[i]);
targets.add(t);
connector.add(t);
}
Thread.sleep(2000);
connector.shutdown();
connector.join();
for (Iterator i = targets.iterator(); i.hasNext();) {
Target t = (Target) i.next();
if (!t.shown)
t.show();
}
}
}
二、JAVA调用外部EXE实现PING功能
import java.io.*;
import java.net.*;
import java.nio.channels.*;
import java.util.*;
import java.util.regex.*;
public class Ping {
static int DAYTIME_PORT = 13;
static int port = DAYTIME_PORT;
static class Target {
InetSocketAddress address;
SocketChannel channel;
Exception failure;
long connectStart;
long connectFinish = 0;
boolean shown = false;
Target(String host) {
try {
address = new InetSocketAddress(InetAddress.getByName(host),
port);
} catch (IOException x) {
failure = x;
}
}
void show() {
String result;
if (connectFinish != 0)
result = Long.toString(connectFinish - connectStart) + "ms";
else if (failure != null)
result = failure.toString();
else
result = "Timed out";
System.out.println(address + " : " + result);
shown = true;
}
}
static class Printer extends Thread {
LinkedList pending = new LinkedList();
Printer() {
setName("Printer");
setDaemon(true);
}
void add(Target t) {
synchronized (pending) {
pending.add(t);
pending.notify();
}
}
public void run() {
try {
for (;;) {
Target t = null;
synchronized (pending) {
while (pending.size() == 0)
pending.wait();
t = (Target) pending.removeFirst();
}
t.show();
}
} catch (InterruptedException x) {
return;
}
}
}
static class Connector extends Thread {
Selector sel;
Printer printer;
LinkedList pending = new LinkedList();
Connector(Printer pr) throws IOException {
printer = pr;
sel = Selector.open();
setName("Connector");
}
void add(Target t) {
SocketChannel sc = null;
try {
sc = SocketChannel.open();
sc.configureBlocking(false);
boolean connected = sc.connect(t.address);
t.channel = sc;
t.connectStart = System.currentTimeMillis();
if (connected) {
t.connectFinish = t.connectStart;
sc.close();
printer.add(t);
} else {
synchronized (pending) {
pending.add(t);
}
sel.wakeup();
}
} catch (IOException x) {
if (sc != null) {
try {
sc.close();
} catch (IOException xx) {
}
}
t.failure = x;
printer.add(t);
}
}
void processPendingTargets() throws IOException {
synchronized (pending) {
while (pending.size() > 0) {
Target t = (Target) pending.removeFirst();
try {
t.channel.register(sel, SelectionKey.OP_CONNECT, t);
} catch (IOException x) {
t.channel.close();
t.failure = x;
printer.add(t);
}
}
}
}
void processSelectedKeys() throws IOException {
for (Iterator i = sel.selectedKeys().iterator(); i.hasNext();) {
SelectionKey sk = (SelectionKey) i.next();
i.remove();
Target t = (Target) sk.attachment();
SocketChannel sc = (SocketChannel) sk.channel();
try {
if (sc.finishConnect()) {
sk.cancel();
t.connectFinish = System.currentTimeMillis();
sc.close();
printer.add(t);
}
} catch (IOException x) {
sc.close();
t.failure = x;
printer.add(t);
}
}
}
volatile boolean shutdown = false;
void shutdown() {
shutdown = true;
sel.wakeup();
}
public void run() {
for (;;) {
try {
int n = sel.select();
if (n > 0)
processSelectedKeys();
processPendingTargets();
if (shutdown) {
sel.close();
return;
}
} catch (IOException x) {
x.printStackTrace();
}
}
}
}
public static void main(String[] args) throws InterruptedException,
IOException {
args = new String[] { "8888", "192.168.10.193" };
if (args.length < 1) {
System.err.println("Usage: java Ping [port] host...");
return;
}
int firstArg = 0;
if (Pattern.matches("[0-9]+", args[0])) {
port = Integer.parseInt(args[0]);
firstArg = 1;
}
Printer printer = new Printer();
printer.start();
Connector connector = new Connector(printer);
connector.start();
LinkedList targets = new LinkedList();
for (int i = firstArg; i < args.length; i++) {
Target t = new Target(args[i]);
targets.add(t);
connector.add(t);
}
Thread.sleep(2000);
connector.shutdown();
connector.join();
for (Iterator i = targets.iterator(); i.hasNext();) {
Target t = (Target) i.next();
if (!t.shown)
t.show();
}
}
}
二、JAVA调用外部EXE实现PING功能
import java.io.*;
import java.lang.*;
public class Ping {
public Ping() {
}
public static void main(String args[])
{
if (args.length < 1)
{
System.out.println("syntax Error!");
}
else
{
String line = null;
try
{
Process pro = Runtime.getRuntime().exec("ping " + args[0]);
BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream()));
while((line = buf.readLine()) != null)
System.out.println(line);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
}
三、ICMP Ping in Java(JDK 1.5 and above)
Programatically using ICMP Ping is a great way to establish that a server is up and running. Previously you couldn't do ICMP ping (what ping command does in Linux/Unix & Windows) in java without using JNI or exec calls. Here is a simple and reliable method to do ICMP pings in Java without using JNI or NIO.
import java.lang.*;
public class Ping {
public Ping() {
}
public static void main(String args[])
{
if (args.length < 1)
{
System.out.println("syntax Error!");
}
else
{
String line = null;
try
{
Process pro = Runtime.getRuntime().exec("ping " + args[0]);
BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream()));
while((line = buf.readLine()) != null)
System.out.println(line);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
}
三、ICMP Ping in Java(JDK 1.5 and above)
Programatically using ICMP Ping is a great way to establish that a server is up and running. Previously you couldn't do ICMP ping (what ping command does in Linux/Unix & Windows) in java without using JNI or exec calls. Here is a simple and reliable method to do ICMP pings in Java without using JNI or NIO.
String host = "172.16.0.2"
int timeOut = 3000; // I recommend 3 seconds at least
boolean status = InetAddress.getByName(host).isReachable(timeOut)
status is true if the machine is reachable by ping; false otherwise. Best effort is made to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible. A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.
In Linux/Unix you may have to suid the java executable to get ICMP Ping working, ECHO REQUESTs will be fine even without suid. However on Windows you can get ICMP Ping without any issues whatsoever.
四、最简单的办法,直接调用CMD
try
{
Runtime.getRuntime().exec("cmd /c start ping 127.0.0.1");
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
ping的过程可以显示在本地的办法
try
{
Runtime.getRuntime().exec("cmd /c start ping 127.0.0.1");
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
ping的过程可以显示在本地的办法
import java.io.*;
public class Ping
{
public static void main(String args[])
{
String line = null;
try
{
Process pro = Runtime.getRuntime().exec("ping 127.0.0.1 ");
BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream()));
while ((line = buf.readLine()) != null)
System.out.println(line);
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
{
public static void main(String args[])
{
String line = null;
try
{
Process pro = Runtime.getRuntime().exec("ping 127.0.0.1 ");
BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream()));
while ((line = buf.readLine()) != null)
System.out.println(line);
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
五、模拟PING
利用InetAddress的isReachable方法可以实现ping的功能,里面参数设定超时时间,返回结果表示是否连上
try {
InetAddress address = InetAddress.getByName("192.168.0.113");
System.out.println(address.isReachable(5000));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
InetAddress address = InetAddress.getByName("192.168.0.113");
System.out.println(address.isReachable(5000));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
六、模拟TELNET
利用Socket的connect(SocketAddress endpoint, int timeout)方法可以实现telnet的功能,如果catch到异常说明telnet失败
try {
Socket server = new Socket();
InetSocketAddress address = new InetSocketAddress("192.168.0.113",
8080);
server.connect(address, 5000);
server.close();
Socket server = new Socket();
InetSocketAddress address = new InetSocketAddress("192.168.0.113",
8080);
server.connect(address, 5000);
server.close();
} catch (UnknownHostException e){
System.out.println("telnet失败");
} catch (IOException e){
System.out.println("telnet失败");
}
System.out.println("telnet失败");
} catch (IOException e){
System.out.println("telnet失败");
}
发表评论
-
Android扫描条形码实现
2011-06-09 11:34 7095扫描和识别条形码的android支持库 androi ... -
Ibaties使用实例二(spring+Ibaties)
2010-08-31 17:18 1485第一步:sql-map-spring-config.xml ... -
Ibaties使用实例一
2010-08-31 16:37 1050本实例是只使用ibaties进行的开发,后续会分享使用spri ... -
jqGrid的使用之---subGird
2010-08-04 17:06 1552直接上代码,其它与后台交互和一般Grid没有区别: html ... -
jqGrid 编辑传参 与后台交互
2010-08-04 17:03 5712一、前台: html代码: <table id= ... -
Jquery Grid插件jqGrid的使用demo
2010-07-30 09:52 4968第一步:引入文件,具体下载的地址网上很多,这里不在罗嗦。 & ... -
史上最全的MySql时间日期处理函数
2010-07-24 23:37 1250转载于【IT168 服务器学院】 这里是一个使用日期函 ... -
Log4j之WEB应用 配置使用利器
2010-04-07 11:23 1679java日志我这里分为两种:web应用和桌面应用。而我这里重点 ... -
JMS实例
2009-09-28 14:05 2475//创建连接工厂 ConnectionFacto ... -
JAVA操作Excel及其ExcelJAR包
2009-09-23 21:27 1781只要记住Excel的组成结构就很好做了,Excel--> ... -
一个最简单的XML解析
2009-09-15 17:11 1264近来很多项目用到XML,根据需要写了个简单的XML解析方法,跟 ...
相关推荐
此项目是spring boot+thymeleaf+maven的Java实现ping命令的功能以及端口检测是否开放的功能
java中ping命令ping工具类(循环ping) java ping ip ping命令 ping工具类 支持linux和windows等所有平台 Ping是Windows下的一个命令 在Unix和Linux下也有这个命令。 ping也属于一个通信协议,是TCP/IP协议的一部分 ...
很好的一个java实现的ping功能,适合初学者学习使用,相互学习,相互交流啊
java调用jpcap实现ping功能代码
基于java的Udp_Ping,很简单的计算机网络课设
用java实现ping的几种方式:纯Java实现ICMP的ping命令,JAVA调用外部EXE实现PING功能,ICMP Ping in Java(JDK 1.5 and above),最简单的办法,直接调用CMD
TCP-Ping_ping_TCP,IP_JAVAping_java_源码.zip
java 实现 ping 操作,检查网络情况
实现linux java ping 及traceroute 功能.rar
资源包含:课程报告word+源码 编程实现PING的服务器端和客户端,实现操作系统提供的ping命令的类似功能。详细介绍参考:https://blog.csdn.net/sheziqiong/article/details/127039936
TCP-Ping_ping_TCP,IP_JAVAping_java.zip
通过几个java包中的API实现了ping这样的功能,完整代码 请参考!
实现linux java ping 及traceroute 功能.zip
java定时ping命令程序
一段JAVA代码 实现ping功能 import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import java.nio.channels.SocketChannel;
Java简单实现Ping功能
java实现ping.pdf
PortPing.java ping 协议 源代码 java
用java 写的java ping程序,要输入一个ip段的啊,呵呵。