【发布时间】:2016-07-25 05:40:17
【问题描述】:
为什么没有输出?目标是使用以下命令通过tee 进行远程登录:
sh -c telnet rainmaker.wunderground.com 3000 | tee weather.txt
用于查看 telnet 输出并使用 Java 记录服务器响应。 (虽然有 telnet 库,但我正在尝试使用带有 exec 或类似功能的系统 telnet,而不是库。)
命令在下面正确回显,但没有显示输出:
thufir@mordor:~$
thufir@mordor:~$ 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
thufir@mordor:~$
此外,没有创建 weather.txt:
thufir@mordor:~$
thufir@mordor:~$ nl weather.txt
nl: weather.txt: No such file or directory
thufir@mordor:~$
代码:
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);
}
}
}
虽然可以通过离开到 tee 的管道来生成输出,但这并不能解决将 tee 与 telnet 一起使用的问题。
另见:
解决这个问题的方法是简单地通过以下方式控制重定向 分别处理外部进程的标准输出流 Runtime.exec() 方法。
http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2
【问题讨论】:
-
你可以试试Runtime.html#exec
-
@KarthikeyanVaithilingam 在这里非常好奇的评论:stackoverflow.com/a/19383655/262852 我提出了一个后续问题:stackoverflow.com/questions/36429067/…,但没有直接检查
exec的源代码。你能详细说明一下吗?
标签: java linux command-line-interface telnet tee