通过使用远程SHELL执行框架 sshxcute 实现远程登录Linux并执行命令。
sshxcute 项目地址:
http://code.google.com/p/sshxcute/
示例代码:
import net.neoremind.sshxcute.core.ConnBean;
import net.neoremind.sshxcute.core.Result;
import net.neoremind.sshxcute.core.SSHExec;
import net.neoremind.sshxcute.task.CustomTask;
import net.neoremind.sshxcute.task.impl.ExecCommand;
public class ConnectWithSSH {
public static void main(String[] args) {
ConnBean cb = new ConnBean("ip", "username", "password");
CustomTask ct1 = new ExecCommand("ls -l /opt/");
SSHExec ssh = null;
try {
ssh = SSHExec.getInstance(cb);
ssh.connect();
Result res = ssh.exec(ct1);
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);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ssh.disconnect();
}
}
}
sshxcute 类结构: