Java使用JSch远程连接SSH,模拟sshClient

1. SSH简介

        SSH是Secure Shell的缩写,一种建立在应用层和传输层基础上的安全协议。SSH在连接和传送过程中会加密所有数据,可以用来在不同系统或者服务器之间进行安全连接。SSH提供两种的安全验证方式:基于密码的认证和基于密匙的认证。其中,基于密码的认证比较简单,只要知道远程主机的用户名和密码,就可以进行登录。基于密匙的认证比较麻烦,而且连接比较耗时,这里不详细介绍。
        有很多基于SSH协议的客户端,例如: PuTTY、 OpenSSH、Xshell 4等,可以远程连接几乎所有UNIX平台。同时,可以通过Linux命令行ssh uername@host连接到某主机。
        在项目中,如何利用代码实现SSH,远程执行Shell脚本呢?JSch是Java Secure Channel的缩写,是一个SSH2功能的纯Java实现,具体信息可以参考 JSch官网。它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,同时你也可以集成它的功能到你自己的应用程序。在使用前,需要下载并导入JSch包: jsch-0.1.50.jar。

2. 实现原理

        1. 根据远程主机的IP地址,用户名和端口,建立会话(Session);
        2. 设置用户信息(包括密码和Userinfo),然后连接session;
        3. 在session上建立指定类型的通道(Channel),本文示例中采用ChannelExec类型的;
        4. 设置channel上需要远程执行的Shell脚本,连接channel,就可以远程执行该Shell脚本;
        5. 可以读取远程执行Shell脚本的输出,然后依次断开channel和session的连接。

3. 示例代码及分析

  • SSHCommandExecutor.java:
[java]  view plain copy
  1. import java.io.BufferedReader;  
  2. import java.io.InputStreamReader;  
  3. import java.util.Vector;  
  4.   
  5. import com.jcraft.jsch.Channel;  
  6. import com.jcraft.jsch.ChannelExec;  
  7. import com.jcraft.jsch.JSch;  
  8. import com.jcraft.jsch.JSchException;  
  9. import com.jcraft.jsch.Session;  
  10.   
  11. /** 
  12.  * This class provide interface to execute command on remote Linux. 
  13.  */  
  14.   
  15. public class SSHCommandExecutor {  
  16.     private String ipAddress;  
  17.   
  18.     private String username;  
  19.   
  20.     private String password;  
  21.   
  22.     public static final int DEFAULT_SSH_PORT = 22;  
  23.   
  24.     private Vector stdout;  
  25.   
  26.     public SSHCommandExecutor(final String ipAddress, final String username, final String password) {  
  27.         this.ipAddress = ipAddress;  
  28.         this.username = username;  
  29.         this.password = password;  
  30.         stdout = new Vector();  
  31.     }  
  32.   
  33.     public int execute(final String command) {  
  34.         int returnCode = 0;  
  35.         JSch jsch = new JSch();  
  36.         MyUserInfo userInfo = new MyUserInfo();  
  37.   
  38.         try {  
  39.             // Create and connect session.  
  40.             Session session = jsch.getSession(username, ipAddress, DEFAULT_SSH_PORT);  
  41.             session.setPassword(password);  
  42.             session.setUserInfo(userInfo);  
  43.             session.connect();  
  44.   
  45.             // Create and connect channel.  
  46.             Channel channel = session.openChannel("exec");  
  47.             ((ChannelExec) channel).setCommand(command);  
  48.   
  49.             channel.setInputStream(null);  
  50.             BufferedReader input = new BufferedReader(new InputStreamReader(channel  
  51.                     .getInputStream()));  
  52.   
  53.             channel.connect();  
  54.             System.out.println("The remote command is: " + command);  
  55.   
  56.             // Get the output of remote command.  
  57.             String line;  
  58.             while ((line = input.readLine()) != null) {  
  59.                 stdout.add(line);  
  60.             }  
  61.             input.close();  
  62.   
  63.             // Get the return code only after the channel is closed.  
  64.             if (channel.isClosed()) {  
  65.                 returnCode = channel.getExitStatus();  
  66.             }  
  67.   
  68.             // Disconnect the channel and session.  
  69.             channel.disconnect();  
  70.             session.disconnect();  
  71.         } catch (JSchException e) {  
  72.             // TODO Auto-generated catch block  
  73.             e.printStackTrace();  
  74.         } catch (Exception e) {  
  75.             e.printStackTrace();  
  76.         }  
  77.         return returnCode;  
  78.     }  
  79.   
  80.     public Vector getStandardOutput() {  
  81.         return stdout;  
  82.     }  
  83.   
  84.     public static void main(final String [] args) {  
  85.         SSHCommandExecutor sshExecutor = new SSHCommandExecutor("xx.xx.xx.xx""username""password");  
  86.         sshExecutor.execute("uname -s -r -v");  
  87.           
  88.         Vector stdout = sshExecutor.getStandardOutput();  
  89.         for (String str : stdout) {  
  90.             System.out.println(str);  
  91.         }  
  92.     }  
  93. }  
        getSession()只是创建一个session,需要设置必要的认证信息之后,调用connect()才能建立连接。
        调用openChannel(String type) 可以在session上打开指定类型的channel。该channel只是被初始化,使用前需要先调用connect()进行连接。
        Channel的类型可以为如下类型:
  • shell - ChannelShell 
  • exec - ChannelExec 
  • direct-tcpip - ChannelDirectTCPIP 
  • sftp - ChannelSftp 
  • subsystem - ChannelSubsystem
        其中,ChannelShell和ChannelExec比较类似,都可以作为执行Shell脚本的Channel类型。它们有一个比较重要的区别:ChannelShell可以看作是执行一个交互式的Shell,而ChannelExec是执行一个Shell脚本。
  • MyUserInfo:
[java]  view plain copy
  1. import com.jcraft.jsch.UserInfo;  
  2.   
  3. /** 
  4.  * This class provide interface to feedback information to the user. 
  5.  */  
  6. public class MyUserInfo implements UserInfo {  
  7.     private String password;  
  8.   
  9.     private String passphrase;  
  10.   
  11.     @Override  
  12.     public String getPassphrase() {  
  13.         System.out.println("MyUserInfo.getPassphrase()");  
  14.         return null;  
  15.     }  
  16.   
  17.     @Override  
  18.     public String getPassword() {  
  19.         System.out.println("MyUserInfo.getPassword()");  
  20.         return null;  
  21.     }  
  22.   
  23.     @Override  
  24.     public boolean promptPassphrase(final String arg0) {  
  25.         System.out.println("MyUserInfo.promptPassphrase()");  
  26.         System.out.println(arg0);  
  27.         return false;  
  28.     }  
  29.   
  30.     @Override  
  31.     public boolean promptPassword(final String arg0) {  
  32.         System.out.println("MyUserInfo.promptPassword()");  
  33.         System.out.println(arg0);  
  34.         return false;  
  35.     }  
  36.   
  37.     @Override  
  38.     public boolean promptYesNo(final String arg0) {  
  39.         System.out.println("MyUserInfo.promptYesNo()");  
  40.         System.out.println(arg0);  
  41.         if (arg0.contains("The authenticity of host")) {  
  42.             return true;  
  43.         }  
  44.         return false;  
  45.     }  
  46.   
  47.     @Override  
  48.     public void showMessage(final String arg0) {  
  49.         System.out.println("MyUserInfo.showMessage()");  
  50.     }  
  51. }  
        MyUserInfo实现了接口UserInfo,主要是为获得运行执行的用户信息提供接口。大部分实现方法中,没有做实质性的工作,只是输出一下trace信息,帮助判断哪个方法被执行过。

4. 执行结果分析

        1. 如果不设置UserInfo,会抛出JSchException异常,提示找不到host:


        2. 如果MyUserInfo实现了接口UserInfo,但是只是@Override一些函数,会出现如下错误:
Java使用JSch远程连接SSH,模拟sshClient_第1张图片

             虽然可以找到host,但是会拒绝访问host。发现所有@Override函数中,get方法返回的都是null,prompt方法返回的都是false。

        3. 为了判断这些Override的methods中,那些是有用的,在每个method中加入trace信息。发现只有promptYesNo(final String)会被调用到,输出如下图所示:
Java使用JSch远程连接SSH,模拟sshClient_第2张图片

        4. promptYesNo(final String)是向用户提出一个yes或者no的问题,来决定是否允许连接远程主机。这才是决定连接是否成功的一个关键函数。如果返回值为true,则允许连接;如果返回值为false,则拒绝连接。最后正确连接后的输出入下图所示:


Reference

        JSch官网 
        JSch API

你可能感兴趣的:(【Java,EE,后台开发】)