简体   繁体   English

sshxcute框架使用Java在远程服务器上执行Shell脚本

[英]sshxcute framework to execute shell script on a remote server using java

I am using a sshxcute framework to execute shell script on a remote server using java. 我正在使用sshxcute框架在使用Java的远程服务器上执行Shell脚本。 Following is the code snippet: 以下是代码段:

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();       
                }

    }


}

Note: Normal execution of commands are successful .. how ever my program returned the following status and error. 注意:命令的正常执行是成功的..我的程序返回以下状态和错误的方式。 Can some one say how to fix this? 有人可以说如何解决这个问题吗?

OUTPUT: 输出:

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)

Can some one say how to fix this issue. 有人可以说如何解决此问题。

Try this : 尝试这个 :

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

Or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM