2016-04-05 23 views
0

出力がないのはなぜですか?目標は、このコマンドでteeを通じてパイプのtelnetに次のとおりです。telnetの出力を見てとJavaを使用してサーバーの応答をログ出力のためにJava execを使用してtee経由でtelnetをパイプする方法

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

。 (私は幹部、または類似のではなく、ライブラリとシステムのtelnetを使用しようとしているtelnetのライブラリがありますが。)

コマンドが正常に以下のエコーされ、まだ出力が示しています

[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); 
     } 
    } 
} 

出力を生成することは可能ですが、teeにパイプを残しておくと、telnetでteeを使用するという問題は解決しません。

も参照:落とし穴に

溶液は単に のRuntime.exec()メソッドとは別に、外部プロセスの標準出力ストリームを処理 によってリダイレクトを制御することでした。

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

+0

[Runtime.html#exec](https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String [])を試すことができますか? ) –

+0

@ KarthikeyanVaithilingamここに非常に興味深いコメント:http://stackoverflow.com/a/19383655/262852私は、フォローアップの質問をしました:http://stackoverflow.com/questions/36429067/sending-a-cmdarray-for-exec -to-process-hello-worldであるが、 'exec'のソースコードを直接調べていない。あなたは精緻化できますか? – Thufir

答えて

1

|を理解している唯一のものはシェルです。このパイプコマンドを理解するには、シェルを実行する必要があります。

+0

"exec a shell"は何を意味しますか? – Thufir

+0

'sh -c'をコマンドの先頭に追加します。 – EJP

関連する問題