我使用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)
有人可以说如何解决这个问题。
答案 0 :(得分:0)
试试这个:
CustomTask ct1 = new ExecShellScript("/home/user/test", "./AUTOMATE_RUN.sh");
ssh.connect();
ssh.exec(ct1);
net.neoremind.sshxcute.core.Result res = ssh.exec(ct1);
或者
CustomTask ct1 = new ExecShellScript("/home/user/test/AUTOMATE_RUN.sh");
ssh.connect();
ssh.exec(ct1);
net.neoremind.sshxcute.core.Result res = ssh.exec(ct1);