簡體   English   中英

如何使用Java exec通過Tee通過telnet傳送telnet

[英]how to pipe telnet through tee with Java exec

為什么沒有輸出? 目標是使用以下命令通過tee通過telnet傳送telnet:

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

為了查看telnet輸出並使用Java記錄服務器響應。 (雖然有遠程登錄庫,但我正在嘗試使用具有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中使用tee的問題。

也可以看看:

陷阱的解決方案是通過獨立於Runtime.exec()方法處理外部進程的標准輸出流來簡單地控制重定向。

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

唯一了解的內容| 是一個殼。 您需要執行shell才能理解此管道命令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM