我想运行netstat -an | grep 12345
并检索输出以查明端口是否为空并且可以使用。我试过了
TRY1:
System.out.println("RUNNING ==> "+command);
try
{
Process process = Runtime.getRuntime().exec("netstat -an | grep 4324");
InputStream in = process.getInputStream();
File tmp = File.createTempFile("allConnections","txt");
byte[] buf = new byte[256];
OutputStream outputConnectionsToFile = new FileOutputStream(tmp);
int numbytes = 0;
while ((numbytes = in.read(buf, 0, 256)) != -1)
{
outputConnectionsToFile.write(buf, 0, numbytes);
}
System.out.println("File is present at "+tmp.getAbsolutePath());
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
我看到12k的结果,就像我正在netstat -an
没有grep
TRY2:
public static ArrayList<String> exec_command_dont_wait(String command) throws IOException, InterruptedException
{
//String[] arrayExplodedArguments = command.split(" ");
ArrayList<String> returnHolder = new ArrayList<String>();
List<String> listCommands = new ArrayList<String>();
String[] arrayExplodedCommands = command.split(" ");
for(String element : arrayExplodedCommands)
{
listCommands.add(element);
System.out.println(element+"\n");
}
System.exit(0);
ProcessBuilder ps = new ProcessBuilder(listCommands);
ps.redirectErrorStream(true);
Process p = ps.start();
BufferedReader buffer = new BufferedReader(new InputStreamReader(p.getInputStream()));
//p.waitFor();
String line = new String();
while((line = buffer.readLine()) != null)
{
returnHolder.add(line);
//System.out.println(line);
}
return returnHolder;
}
我看到12k的结果,就像我在netstat -an
没有grep
的情况一样,永远不要提到我必须发表评论//p.waitFor();
而我不知道为什么。
netstat
创建一个简单的grep
命令并检索结果?PHP
中的简单命令/功能并检索结果?就像'exec()'一样,真的很喜欢这个功能。p.waitFor()
和没有JAVA
之间的差异是什么?我的意思是我理解netstat
等待过程消失,但curl
似乎永远不会完成,我等了2分钟。一个简单的netstat -an | grep 4352
tcp 0 0 192.99.3.11:43529 118.32.42.29:22 ESTABLISHED
tcp 0 0 192.99.3.11:43522 15.139.118.57:22 ESTABLISHED
tcp 0 0 192.99.3.11:43521 116.322.199.10:22 ESTABLISHED
结束了。示例
{{1}}
谢谢。