public String ping(String ip) {
try {
String command = "ping "+ip;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
CommandLine commandline = CommandLine.parse(command);
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValues(null);
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);
exec.setStreamHandler(streamHandler);
exec.execute(commandline);
String out = outputStream.toString("gbk");
String error = errorStream.toString("gbk");
return out+error;
} catch (Exception e) {
log.error("ping task failed.",e);
return e.toString();
}
}官方教程参考:
http://commons.apache.org/exec/tutorial.html
利用Apache Commons Exec调用命令行并取得命令行的输出(实例)
最新推荐文章于 2024-01-05 00:16:59 发布
本文介绍了如何使用Apache Commons Exec库中的ping方法实现网络连通性检查,包括构造命令、执行ping操作并获取输出结果。
&spm=1001.2101.3001.5002&articleId=8029020&d=1&t=3&u=644abe975aa0415c81e4dc8d79b4a6e6)
1751

被折叠的 条评论
为什么被折叠?



