Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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在远程服务器上执行shell脚本的sshxcute框架_Java_Linux_Ssh - Fatal编程技术网

使用java在远程服务器上执行shell脚本的sshxcute框架

使用java在远程服务器上执行shell脚本的sshxcute框架,java,linux,ssh,Java,Linux,Ssh,我使用sshxcute框架在远程服务器上使用java执行shell脚本。 以下是代码片段: import java.io.IOException; import javax.naming.spi.DirStateFactory.Result; import net.neoremind.sshxcute.core.ConnBean; import net.neoremind.sshxcute.core.IOptionName; import net.neoremind.sshxcute.core.

我使用sshxcute框架在远程服务器上使用java执行shell脚本。 以下是代码片段:

import java.io.IOException;
import javax.naming.spi.DirStateFactory.Result;
import net.neoremind.sshxcute.core.ConnBean;
import net.neoremind.sshxcute.core.IOptionName;
import net.neoremind.sshxcute.core.SSHExec;
import net.neoremind.sshxcute.exception.TaskExecFailException;
import net.neoremind.sshxcute.task.CustomTask;
import net.neoremind.sshxcute.task.impl.ExecCommand;
import net.neoremind.sshxcute.task.impl.ExecShellScript;

/**
 *
 * @author jp
 */
public class executesshssm {
    public static void main(final String[] args) throws IOException 
    {
           SSHExec ssh = null;
                try {
                        SSHExec.setOption(IOptionName.HALT_ON_FAILURE, true);
                        SSHExec.setOption(IOptionName.SSH_PORT_NUMBER, 22);
                        SSHExec.setOption(IOptionName.ERROR_MSG_BUFFER_TEMP_FILE_PATH, "E:\\123.err");
                        SSHExec.setOption(IOptionName.INTEVAL_TIME_BETWEEN_TASKS, 10);
                        SSHExec.setOption(IOptionName.TIMEOUT, 36000l);
                        SSHExec.showEnvConfig();

                        ConnBean cb = new ConnBean("systemIP", "USER","PWD");


                        ssh = SSHExec.getInstance(cb);          
                        CustomTask task3 = new ExecCommand("cd /home/user/test");

                       CustomTask ct1 = new ExecShellScript("./AUTOMATE_RUN.sh");
                        ssh.connect();
                        ssh.exec(task3);
                        net.neoremind.sshxcute.core.Result res = ssh.exec(ct1);
                        // Check result and print out messages.
                        if (res.isSuccess)
                        {
                            System.out.println("Return code: " + res.rc);
                            System.out.println("sysout: " + res.sysout);
                        }
                        else
                        {
                            System.out.println("Return code: " + res.rc);
                            System.out.println("error message: " + res.error_msg);
                        };
                      //  ssh.exec(task1);
                       // ssh.exec(task2);
                       // System.out.println("This should not print!");
                       // ssh.exec(task3);
                        //System.out.println("Task3 does not execute");
                } catch (TaskExecFailException e) {
                        System.out.println(e.getMessage());
                        e.printStackTrace();
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                        e.printStackTrace();
                } finally {
                        ssh.disconnect();       
                }

    }


}
注意:命令的正常执行是成功的。。我的程序如何返回以下状态和错误。有人能说怎么解决这个问题吗

输出:

Set system configuration parameter 'HALT_ON_FAILURE' to new value 'true'
Set system configuration parameter 'SSH_PORT_NUMBER' to new value '22'
Set system configuration parameter 'ERROR_MSG_BUFFER_TEMP_FILE_PATH' to new value 'E:\123.err'
Set system configuration parameter 'TIMEOUT' to new value '36000'
******************************************************
The list below shows sshxcute configuration parameter
******************************************************
TIMEOUT => 36000
HALT_ON_FAILURE => true
INTEVAL_TIME_BETWEEN_TASKS => 5000
SSH_PORT_NUMBER => 22
ERROR_MSG_BUFFER_TEMP_FILE_PATH => E:\123.err
SSHExec initializing ...
Session initialized and associated with user credential password
SSHExec initialized successfully
SSHExec trying to connect USER@IP
SSH connection established
Command is cd /home/user/test
Connection channel established succesfully
Start to run command
Connection channel closed
Check if exec success or not ... 
Execute successfully for command: cd /home/user/test
Now wait 5 seconds to begin next task ...
Connection channel disconnect
Command is ./AUTOMATE_RUN.sh  
Connection channel established succesfully
Start to run command
Connection channel closed
Check if exec success or not ... 
Execution failed while executing command: ./AUTOMATE_RUN.sh  
Error message: bash: ./AUTOMATE_RUN.sh: A file or directory in the path name does not exist.
The task has failed to execute :Exec shell script ./AUTOMATE_RUN.sh  . So program exit.
The task has failed to execute : Exec shell script ./AUTOMATE_RUN.sh  
net.neoremind.sshxcute.exception.TaskExecFailException: The task has failed to execute : Exec shell script ./AUTOMATE_RUN.sh  
SSH connection shutdown
    at net.neoremind.sshxcute.core.SSHExec.exec(SSHExec.java:214)
    at SplashDemo.executesshssm.main(executesshssm.java:46)
BUILD SUCCESSFUL (total time: 5 seconds)
有人能说出如何解决这个问题吗。

试试这个:

CustomTask ct1 = new ExecShellScript("/home/user/test", "./AUTOMATE_RUN.sh");
ssh.connect();
ssh.exec(ct1);
net.neoremind.sshxcute.core.Result res = ssh.exec(ct1);


能否显示./automation\u RUN.sh的内容?看起来里面有一条不存在的路径!
CustomTask ct1 = new ExecShellScript("/home/user/test/AUTOMATE_RUN.sh");
ssh.connect();
ssh.exec(ct1);
net.neoremind.sshxcute.core.Result res = ssh.exec(ct1);