为什么没有输出?我们的目标是要管的telnet通过tee用这个命令:如何通过Java管道通过tee管道telnet
sh -c telnet rainmaker.wunderground.com 3000 | tee weather.txt
对于看到的telnet输出和记录与Java服务器响应的目的。 (虽然有远程登录库我试图使用该系统的telnet以exec,或类似的,而不是库)。
命令被正确地回荡在下面,但没有输出显示:
[email protected]:~$
[email protected]:~$ java -jar NetBeansProjects/T/dist/T.jar
Apr 05, 2016 4:36:56 AM net.bounceme.mordor.telnet.Main run
INFO: starting..
Apr 05, 2016 4:36:56 AM net.bounceme.mordor.telnet.Telnet <init>
INFO: connecting..
Apr 05, 2016 4:36:56 AM net.bounceme.mordor.telnet.PropertiesReader getConnection
INFO: starting..
Apr 05, 2016 4:36:56 AM net.bounceme.mordor.telnet.PropertiesReader getConnection
INFO: {weather.port=3000, weather.host=rainmaker.wunderground.com}
Apr 05, 2016 4:36:56 AM net.bounceme.mordor.telnet.Telnet getAddress
INFO: sh -c telnet rainmaker.wunderground.com 3000 | tee weather.txt
a
b
c
^CApr 05, 2016 4:37:01 AM net.bounceme.mordor.telnet.Telnet read
SEVERE: exiting.. 130
[email protected]:~$
而且,也没有创建weather.txt
:
[email protected]:~$
[email protected]:~$ nl weather.txt
nl: weather.txt: No such file or directory
[email protected]:~$
代码:
package net.bounceme.mordor.telnet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Telnet {
private final static Logger LOG = Logger.getLogger(Telnet.class.getName());
public Telnet() {
LOG.info("connecting..");
}
private String getAddress() {
Properties props = PropertiesReader.getConnection();
String host = props.getProperty("weather.host");
String port = props.getProperty("weather.port");
String tee = " | tee weather.txt";
String address = "sh -c telnet " + host + " " + port + tee;
LOG.info(address);
return address;
}
private void read(Process process) throws IOException, InterruptedException {
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
int exitVal = process.waitFor();
LOG.log(Level.SEVERE, "exiting.. {0}", exitVal);
}
private void useProcessBuilder() throws IOException, InterruptedException {
LOG.info("using ProcessBuilder..");
ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c", getAddress());
Process process = processBuilder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
int exitVal = process.waitFor();
LOG.log(Level.SEVERE, "done {0}", exitVal);
}
public void tryProcessBuilder() {
try {
useProcessBuilder();
} catch (IOException | InterruptedException ex) {
Logger.getLogger(Telnet.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void connect() throws IOException, InterruptedException {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(getAddress());
read(process);
}
public void tryConnect() {
try {
connect();
} catch (IOException | InterruptedException ex) {
Logger.getLogger(Telnet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
尽管可以通过将管道留给三通来生成输出,但这并不能解决与telnet一起使用tee的问题。
也参见:
到陷阱的解决方案是简单地通过 控制重定向从 所述的Runtime.exec()方法单独处理外部进程的标准输出流。
http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2
你可以试试[Runtime.html#exec](https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String []) ) –
@KarthikeyanVithithilingam非常好奇的评论在这里:http://stackoverflow.com/a/19383655/262852我做了一个后续问题:http://stackoverflow.com/questions/36429067/sending-a-cmdarray-for-exec -to-process-hello-world,但没有直接检查“exec”的源代码。你能详细说明吗? – Thufir