Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用BufferedReader从ssh获取输出_Java_Ssh_Bufferedreader - Fatal编程技术网

Java 使用BufferedReader从ssh获取输出

Java 使用BufferedReader从ssh获取输出,java,ssh,bufferedreader,Java,Ssh,Bufferedreader,我使用以下脚本执行命令并获得输出: Runtime rt=Runtime.getRuntime() 如果执行“ls-la”或“ssh”,则会得到预期的输出。但是,尝试从远程设备(第1行)获取.txt内容失败,操作挂起。 执行“ssh”root@192.168.0.1命令行中的“cat a.txt”起作用并检索内容 为什么会这样?怎么能修好呢? 谢谢,因为您需要在单独的线程中读取这两个流,或者合并它们。在将任何内容写入stdtout之前,您不能假设进程将退出并因此关闭其stderr,或者更确切地说

我使用以下脚本执行命令并获得输出: Runtime rt=Runtime.getRuntime()

如果执行“ls-la”或“ssh”,则会得到预期的输出。但是,尝试从远程设备(第1行)获取.txt内容失败,操作挂起。 执行“ssh”root@192.168.0.1命令行中的“cat a.txt”起作用并检索内容

为什么会这样?怎么能修好呢?
谢谢,因为您需要在单独的线程中读取这两个流,或者合并它们。在将任何内容写入
stdtout
之前,您不能假设进程将退出并因此关闭其
stderr
,或者更确切地说,如果不对
stdout
写入很少的内容,它将不会阻塞。

请更新问题中的代码以向我们展示您是如何做到这一点的。您可能还需要关闭进程的输入流。
static Runtime rt;
static Process proc;

public static void main(String[] Args) throws IOException, InterruptedException {

    rt = Runtime.getRuntime();
        String[] commands = {"ssh.exe","-o BatchMode=yes",  "root@192.168.1.1", "cat /var/a.txt"};
    //  String[] commands = {"ssh.exe", "root@192.168.0.1"};
    //  String[] commands = { "ls", "-la" };

    proc = rt.exec(commands);

    new Thread("out") {
        public void run() {
            System.out.println("Thread: " + getName() + " running");
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));

            String s = null;
            // read the output from the command
            System.out.println("StdOut:\n");
            try {
                while ((s = stdInput.readLine()) != null) {
                    System.out.println("$ " + s);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }.start();

    new Thread("err") {
        public void run() {
            System.out.println("Thread: " + getName() + " running");
            BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

            String s = null;
            // read any errors from the attempted command
            System.out.println("StdErr:\n");
            try {
                while ((s = stdError.readLine()) != null) {
                    System.out.println("! " + s);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }.start();




    Thread.sleep(10000);
    System.out.println("end");

}