Java PING一系列IP地址
ping单个IP地址的Java程序Java PING一系列IP地址,java,networking,ping,Java,Networking,Ping,ping单个IP地址的Java程序 import java.io.BufferedReader; import java.io.InputStreamReader; public class PingIP { public static void runSystemCommand(String command) { try { Process p = Runtime.getRuntime().exec(command);
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class PingIP {
public static void runSystemCommand(String command) {
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String s = "";
// reading output stream of the command
while ((s = inputStream.readLine()) != null) {
System.out.println(s);
}
....
}
但是我可以用java ping一个IP地址范围吗。如果您只是试图执行一个ping一个范围的bash命令,最好使用
nmap
一个有效的例子:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
final List<String> command = new ArrayList<>();
command.add("nmap");
command.add("-T5");
command.add("-sP");
command.add("172.19.0.0-255");
executeCommand(command);
}
private static int executeCommand(final List<String> command) {
try {
final ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash").command(command);
processBuilder.redirectErrorStream(true);
System.out.println("executing: " + processBuilder.command().toString());
final Process process = processBuilder.start();
final InputStream inputStream = process.getInputStream();
final InputStream errorStream = process.getErrorStream();
readStream(inputStream);
readStream(errorStream);
return process.waitFor();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return -1;
}
private static void readStream(final InputStream iStream) {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(iStream))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
公共班机{
公共静态void main(字符串[]args)引发IOException、InterruptedException{
final List命令=new ArrayList();
添加命令(“nmap”);
命令。添加(“-T5”);
添加(“-sP”);
添加命令(“172.19.0.0-255”);
executeCommand(命令);
}
私有静态int executeCommand(最终列表命令){
试一试{
final ProcessBuilder ProcessBuilder=new ProcessBuilder(“/bin/bash”).command(command);
processBuilder.redirectErrorStream(true);
System.out.println(“正在执行:”+processBuilder.command().toString());
最终流程=processBuilder.start();
final InputStream InputStream=process.getInputStream();
final InputStream errorStream=process.getErrorStream();
读取流(输入流);
readStream(errorStream);
返回进程。waitFor();
}捕获(例外e){
System.out.println(e.getMessage());
}
返回-1;
}
私有静态void readStream(最终输入流为流){
try(BufferedReader BufferedReader=new BufferedReader(new InputStreamReader(iStream))){
弦线;
而((line=bufferedReader.readLine())!=null){
系统输出打印项次(行);
}
}捕获(IOE异常){
System.out.println(e.getMessage());
}
}
}
这可以被重构为更加具体,但对于您的基本用户案例,这提供了一些灵活性 “With Java”与此无关。您只是在搜索一个可以执行的命令。