如何使用Java exec通过tee管道telnet

时间:2016-04-05 10:23:49

标签: java linux command-line-interface telnet tee

为什么没有输出?目标是使用以下命令通过tee管道telnet:

sh -c telnet rainmaker.wunderground.com 3000 | tee weather.txt

用于查看使用Java的telnet输出和日志服务器响应。 (虽然有telnet库我试图使用系统telnet与exec,或类似的,而不是库。)

下面正确回显了命令,但没有输出显示:

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和telnet的问题。

另见:

  

陷阱的解决方案是简单地控制重定向   分别处理外部进程的标准输出流   Runtime.exec()方法。

http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2

1 个答案:

答案 0 :(得分:1)

唯一理解|的是shell。你需要执行一个shell来理解这个管道命令。