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

时间:2020-06-18 18:03:06

标签: java ssh runtime exec

我有一个Java代码,其中它使用var counter=0; function check_counter() { if($('#CustomProgressMessage').val()!="" || $('#campaignNameTxt').val()!="" || $('#campaignImg').val()!="" || $('#campaignCollection').val()!="" || $('#campaignProduct').val()!="" || $('#campaignStartDate').val()!="" || $('#campaignExpireDate').val()!="" || $('#campaignDescription').val()!="" ) { counter++ } $i=1; while($i<=6) { var tierEnabled = $('table.campaign-tiers-table td.td-tier-status input.tier_active.'.concat($i)).prop('checked'); if( tierEnabled == true ){ counter++ } if($('#ProgressBar'.concat($i)).prop('checked') == true) { counter++ } $i++ } if(is_save==1) { counter=0 } } function areYouSure() { check_counter() console.log(counter) if(allowPrompt){ if(counter!=0) return "Are you sure you want to close it"; }else{ allowPrompt = true; } } var allowPrompt = true; window.onbeforeunload = areYouSure; 运行ssh命令。 下面是代码:

Runtime.getRuntime().exec

我得到以下输出:

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

在Java代码中,如果我将命令参数更改为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 而不是ls,则可以正常工作。 另外,如果我使用sudo ls /root运行,则会遇到与退出状态 127 相同的问题。 所有root权限活动均不起作用。请帮忙。

2 个答案:

答案 0 :(得分:0)

Java中exec的问题通常是由使用exec(String)版本的调用以及使用bash / SHELL命令引起的。因此,首先尝试将命令传递为String []版本,这样可以确定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”,或显式引用外壳程序“ / bin / bash -c'sudo ls / root'”可能会有帮助。

答案 1 :(得分:-1)

请使用ProcessBuilder类而不是运行时类来执行任何批处理/ sh文件或OS命令。

ProcessBuilder processBuilder = new ProcessBuilder(comandAndargList);