Java实践 — SSH远程执行Shell脚本http://
原文地址: http://blog.youkuaiyun.com/jmyue/article/details/14003783 作者:supereagle
1. SSH简介
2. 实现原理
3. 示例代码及分析
- SSHCommandExecutor.java:
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Vector;
- import com.jcraft.jsch.Channel;
- import com.jcraft.jsch.ChannelExec;
- import com.jcraft.jsch.JSch;
- import com.jcraft.jsch.JSchException;
- import com.jcraft.jsch.Session;
- /**
- * This class provide interface to execute command on remote Linux.
- */
- public class SSHCommandExecutor {
- private String ipAddress;
- private String username;
- private String password;
- public static final int DEFAULT_SSH_PORT = 22;
- private Vector<String> stdout;
- public SSHCommandExecutor(final String ipAddress, final String username, final String password) {
- this.ipAddress = ipAddress;
- this.username = username;
- this.password = password;
- stdout = new Vector<String>();
- }
- public int execute(final String command) {
- int returnCode = 0;
- JSch jsch = new JSch();
- MyUserInfo userInfo = new MyUserInfo();
- try {
- // Create and connect session.
- Session session = jsch.getSession(username, ipAddress, DEFAULT_SSH_PORT);
- session.setPassword(password);
- session.setUserInfo(userInfo);
- session.connect();
- // Create and connect channel.
- Channel channel = session.openChannel("exec");
- ((ChannelExec) channel).setCommand(command);
- channel.setInputStream(null);
- BufferedReader input = new BufferedReader(new InputStreamReader(channel
- .getInputStream()));
- channel.connect();
- System.out.println("The remote command is: " + command);
- // Get the output of remote command.
- String line;
- while ((line = input.readLine()) != null) {
- stdout.add(line);
- }
- input.close();
- // Get the return code only after the channel is closed.
- if (channel.isClosed()) {
- returnCode = channel.getExitStatus();
- }
- // Disconnect the channel and session.
- channel.disconnect();
- session.disconnect();
- } catch (JSchException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return returnCode;
- }
- public Vector<String> getStandardOutput() {
- return stdout;
- }
- public static void main(final String [] args) {
- SSHCommandExecutor sshExecutor = new SSHCommandExecutor("xx.xx.xx.xx", "username", "password");
- sshExecutor.execute("uname -s -r -v");
- Vector<String> stdout = sshExecutor.getStandardOutput();
- for (String str : stdout) {
- System.out.println(str);
- }
- }
- }
- shell - ChannelShell
- exec - ChannelExec
- direct-tcpip - ChannelDirectTCPIP
- sftp - ChannelSftp
- subsystem - ChannelSubsystem
- MyUserInfo:
- import com.jcraft.jsch.UserInfo;
- /**
- * This class provide interface to feedback information to the user.
- */
- public class MyUserInfo implements UserInfo {
- private String password;
- private String passphrase;
- @Override
- public String getPassphrase() {
- System.out.println("MyUserInfo.getPassphrase()");
- return null;
- }
- @Override
- public String getPassword() {
- System.out.println("MyUserInfo.getPassword()");
- return null;
- }
- @Override
- public boolean promptPassphrase(final String arg0) {
- System.out.println("MyUserInfo.promptPassphrase()");
- System.out.println(arg0);
- return false;
- }
- @Override
- public boolean promptPassword(final String arg0) {
- System.out.println("MyUserInfo.promptPassword()");
- System.out.println(arg0);
- return false;
- }
- @Override
- public boolean promptYesNo(final String arg0) {
- System.out.println("MyUserInfo.promptYesNo()");
- System.out.println(arg0);
- if (arg0.contains("The authenticity of host")) {
- return true;
- }
- return false;
- }
- @Override
- public void showMessage(final String arg0) {
- System.out.println("MyUserInfo.showMessage()");
- }
- }
4. 执行结果分析
Reference
1万+



被折叠的 条评论
为什么被折叠?



