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 Runtime.getRuntime().exec命令失败,所有根活动的退出状态为127_Java_Ssh_Runtime_Exec - Fatal编程技术网

Java Runtime.getRuntime().exec命令失败,所有根活动的退出状态为127

Java Runtime.getRuntime().exec命令失败,所有根活动的退出状态为127,java,ssh,runtime,exec,Java,Ssh,Runtime,Exec,我有一个java代码,它使用Runtime.getRuntime.exec运行ssh命令。 代码如下: package somepackage import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Nodes {

我有一个java代码,它使用Runtime.getRuntime.exec运行ssh命令。 代码如下:

package somepackage

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Nodes {

    protected String ip = "1.2.3.4";
    private String privateKeyPath = "src/test/resources/devopskey";
    private String username = "devops";
    Logger log = LoggerFactory.getLogger(Class.class.getName());

    public void executeCommand(String command) {
        try {
            log.info("Executing command " + command + " on " + this.ip);
            String ssh = "ssh -i " + privateKeyPath + " " + username + "@" + this.ip;
            String cmd = ssh + " \"" + command + "\"";
            log.info("Executing: " + cmd);
            Process process = Runtime.getRuntime().exec(cmd);

            StringBuilder output = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line).append("\n");
            }

            log.info(output.toString());
            int exitStatus = process.waitFor();
            log.info("Exit status: " + exitStatus);
            if (exitStatus != 0) {
                log.error("Failed to execute command: " + command + " on ip: " + this.ip);
            }
            else
                log.info("Command executed successfully");


        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }


    }

    public static void main(String[] args) {
        new Nodes().executeCommand("sudo ls /root");
    }
}
我得到以下输出:

23:20:08.512 [main] INFO java.lang.Class - Executing command ls /root on 1.2.3.4
23:20:08.538 [main] INFO java.lang.Class - Executing: ssh -i src/test/resources/devopskey devops@1.2.3.4 "sudo ls /root"
23:20:10.078 [main] INFO java.lang.Class - 
23:20:10.084 [main] INFO java.lang.Class - Exit status: 127
23:20:10.084 [main] ERROR java.lang.Class - Failed to execute command: ls /root on ip: 1.2.3.4
如果在本地计算机上运行,则输出如下:

joshi-mac$ ssh -i src/test/resources/devopskey devops@1.2.3.4 "sudo ls /root"
buildPlane.deb
node-v10.16.3-linux-x64
node-v10.16.3-linux-x64.tar.xz
在java代码中,如果我将命令params改为justls,而不是sudols/root,那么效果很好。 另外,如果我使用ls/root运行,我会遇到与退出状态127相同的问题。
所有根权限活动都不工作。请提供帮助。

请使用ProcessBuilder类来执行任何批处理/sh文件或操作系统命令,而不是运行时类

ProcessBuilder processBuilder = new ProcessBuilder(comandAndargList);

Java中exec的问题通常是由于使用execString版本的调用和bash/SHELL命令引起的。因此,首先,请尝试将命令作为字符串[]版本传递,这样您就可以更确定Java正在将正确的参数传递给正在运行的命令:

String[] arr = new String[] {"ssh","-i ", privateKeyPath, username + "@" + this.ip, command};
Process process = Runtime.getRuntime().exec(arr);

// ... or as ProcessBuilder is used by Runtime.exec:
Process process = new ProcessBuilder(arr); 

您的下一个问题可能与远程计算机上的帐户/环境有关。尝试将ls作为完全限定路径/bin/ls或显式引用shell/bin/bash-c“sudo ls/root”可能会有所帮助。

此外,我无权访问root密码或devops用户密码