Noe*_*Yap 5 java ssh stdin pipe
在命令行上一切正常,但是当我将我想要的内容翻译成Java时,接收过程永远不会在stdin上得到任何东西.
这就是我所拥有的:
private void deployWarFile(File warFile, String instanceId) throws IOException, InterruptedException {
Runtime runtime = Runtime.getRuntime();
// FIXME(nyap): Use Jsch.
Process deployWarFile = runtime.exec(new String[]{
"ssh",
"gateway",
"/path/to/count-the-bytes"});
OutputStream deployWarFileStdin = deployWarFile.getOutputStream();
InputStream deployWarFileStdout = new BufferedInputStream(deployWarFile.getInputStream());
InputStream warFileInputStream = new FileInputStream(warFile);
IOUtils.copy(warFileInputStream, deployWarFileStdin);
IOUtils.copy(deployWarFileStdout, System.out);
warFileInputStream.close();
deployWarFileStdout.close();
deployWarFileStdin.close();
int status = deployWarFile.waitFor();
System.out.println("************ Deployed with status " + status + " file handles. ************");
}
Run Code Online (Sandbox Code Playgroud)
脚本'count-the-bytes'只是:
#!/bin/bash
echo "************ counting stdin bytes ************"
wc -c
echo "************ counted stdin bytes ************"
Run Code Online (Sandbox Code Playgroud)
输出表明该函数在'wc -c'行挂起 - 它永远不会到达'count stdin bytes'行.
这是怎么回事?会用Jsch帮忙吗?
您可以在期望 wc -c 返回之前尝试关闭输出流。
IOUtils.copy(warFileInputStream, deployWarFileStdin);
deployWarFileStdin.close();
IOUtils.copy(deployWarFileStdout, System.out);
warFileInputStream.close();
deployWarFileStdout.close();
Run Code Online (Sandbox Code Playgroud)