Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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
在bash中使用sshpass可以从命令行工作,而不是从javaexec工作_Java_Bash_Ssh_Exec_Runtime.exec - Fatal编程技术网

在bash中使用sshpass可以从命令行工作,而不是从javaexec工作

在bash中使用sshpass可以从命令行工作,而不是从javaexec工作,java,bash,ssh,exec,runtime.exec,Java,Bash,Ssh,Exec,Runtime.exec,我有一个bash脚本,它使用sshpass和ssh自动登录到不同的机器并触发命令。当从命令行触发时,bash脚本工作正常,但当从java应用程序调用它时,它无法继续 sshpass -p 'password' ssh user@XXX.XXX.XXX.XXX './SleepDisplay && exit' bash脚本做了很多其他事情,我没有办法直接在java中实现ssh登录。我似乎无法理解它为什么会失败。除了ssh,其他一切都运行良好。您可以尝试使用ganymed-ssh2

我有一个bash脚本,它使用sshpass和ssh自动登录到不同的机器并触发命令。当从命令行触发时,bash脚本工作正常,但当从java应用程序调用它时,它无法继续

sshpass -p 'password' ssh user@XXX.XXX.XXX.XXX './SleepDisplay && exit'

bash脚本做了很多其他事情,我没有办法直接在java中实现ssh登录。我似乎无法理解它为什么会失败。除了ssh,其他一切都运行良好。

您可以尝试使用
ganymed-ssh2
java库,它让您能够使用java执行和执行shell脚本等等。。。 下面显示了使用此库的示例:

{
    String hostname = "127.0.0.1";
    String username = "joe";
    String password = "joespass";

    try
    {
        /* Create a connection instance */

        Connection conn = new Connection(hostname);

        /* Now connect */

        conn.connect();

        /* Authenticate.
         * If you get an IOException saying something like
         * "Authentication method password not supported by the server at this stage."
         * then please check the FAQ.
         */

        boolean isAuthenticated = conn.authenticateWithPassword(username, password);

        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");

        /* Create a session */

        Session sess = conn.openSession();

                    // here execute which command separate for ";" 
        sess.execCommand("uname -a && date && uptime && who");

        System.out.println("Here is some information about the remote host:");

        /* 
         * This basic example does not handle stderr, which is sometimes dangerous
         * (please read the FAQ).
         */

        InputStream stdout = new StreamGobbler(sess.getStdout());

        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

        while (true)
        {
            String line = br.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }

        /* Show exit status, if available (otherwise "null") */

        System.out.println("ExitCode: " + sess.getExitStatus());

        /* Close this session */

        sess.close();

        /* Close the connection */

        conn.close();

    }
    catch (IOException e)
    {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}

首先打开一个shell并执行命令。请尝试以下操作:

 String COMMAND = "sshpass -p 'password' ssh user@XXX.XXX.XXX.XXX './SleepDisplay && exit'";
 String[] SHELL_COMMAND = { "/bin/sh", "-c", COMMAND };
 ...
 Runtime runtime = Runtime.getRuntime();
 Process process = runtime.exec(SHELL_COMMAND);

希望我能给你一个有用的提示。

通过
Runtime.exec()
执行命令时,第一个元素是可执行文件,然后在数组的其余部分分别传递所有其他参数

但是,您(可能)将整个linux命令作为可执行文件传入,这是行不通的

试试这个:

String[] cmdarray = {"sshpass", "-p", "'password'", "ssh", "user@XXX.XXX.XXX.XXX", "'./SleepDisplay && exit'"};
Runtime.getRuntime().exec(cmdarray);

如果说
它不起作用
,你将得不到任何帮助。什么不起作用?密码在哪里?你如何执行命令?考虑编辑你的问题,在你的java上下文中包含行,以及(更重要的)你可以捕获的任何错误消息。java中的调用是否返回失败状态?能否将shell调试嵌入到上述命令中,即
set-vx;sshpass…
。JAVA是否与运行shell代码的终端具有相同的路径。考虑添加<代码>回声$路径;set-vx;sshpass…验证路径是否正确。祝你好运。问题是exec()只调用bash脚本
Runtime.getRuntime().exec('bashscriptname')
sshpass在脚本中,还有很多其他命令,可以正常工作。