SSH无法使用apache commons-exec工作

时间:2016-10-10 09:24:46

标签: java ssh command-line apache-commons-exec

我正在尝试使用ssh在远程计算机上运行命令。不知道为什么,但似乎没有执行命令。 以下是我构建命令的方法:

    CommandLine cmdLine = new CommandLine("ssh");
    cmdLine.addArgument("-i");
    cmdLine.addArgument("${key}");
    cmdLine.addArgument("root@myhostname.com");
    cmdLine.addArgument("rm");
    cmdLine.addArgument("${dest}");

    Map map = ImmutableMap.builder()
            .put("key", /a/mykey.key)
            .put("dest", /a/myfile.file)
            .build();

    cmdLine.setSubstitutionMap(map);

    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    executor.execute(cmd);

我尝试在执行命令之前复制命令并手动执行它并且工作正常。我还尝试在addArgument语句中添加handleQuoting = false但它不起作用。 奇怪的是,使用scp执行类似命令的工作原理如下:

    CommandLine cmdLine = new CommandLine("scp");
    cmdLine.addArgument("-i");
    cmdLine.addArgument("${key}");
    cmdLine.addArgument("${file}");
    cmdLine.addArgument("root@myhostname.com:/somepath");

    Map map = ImmutableMap.builder()
            .put("key", /a/mykey.key)
            .put("file", /a/myfile.file)
            .build();

    cmdLine.setSubstitutionMap(map);

另外,我注意到使用Runtime.exec(...)做同样的事情。

知道为什么没有使用commons-exec执行ssh命令?

0 个答案:

没有答案