日期:2014-05-16  浏览次数:20421 次

Jsch 深入浅出

?

如果大家熟悉Linux的话,一定对ssh,sftp,scp等命令非常熟悉。ssh是一个安全协议,用来在不同系统或者服务器之间进行安全连接。ssh 在连接和传送的过程中会加密所有的数据。具体的解释,大家可以参考百度百科的文档。地址为:http://baike.baidu.com/view/16184.htm

但是SSH一般是基于客户端的或者Linux命令行的。比如客户端的工具:OpenSSH,putty,SSH Tectia;在linux上大家可以通过ssh username@host连接到所要想连接的主机。但是如果在J2EE中,如何实现SSH呢?进而可以实现SCP,SFTP的功能呢?下面介绍的JSCH就可以实现下边的功能。

JSCH是一个纯粹的用java实现SSH功能的java ?library. 官方地址为:http://www.jcraft.com/jsch/

GitHub 地址为:https://github.com/vngx/vngx-jsch

mvn 配置如下:

<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.46</version> </dependency>

?

下面简单介绍下JSCH的特点:

1.基于DSA和RSA加密。

2.可以实现4中认证机制。分别是:

(1i): password

(2i): publickey(DSA,RSA)

(3i): keyboard-interactive

(4i): gss-api-with-mic

3.生成public/private key pair.

4.执行bash script 等脚本

5.可以通过HTTP/SOCK5 proxy

6.支持常见SSH1协议和SSH2协议

我们日常用到的JSCH主要是基于是密码认证和private key 认证。

基于密码认证的比较简单。简单代码如下:

?

?

public class JschHandler {
	private static final Logger log = LoggerFactory.getLogger(JschHandler.class);
	
	public static final String SFTP_PROTOCAL = "sftp";
	private String username;
	private String host;
	private int port;
	private String identity;
	private UserInfo userInfo;
	
	private JSch jsch = null;
	protected Session session = null;
	private boolean firstInit = false;
	private int authType = -1;
	
	/**
	 * Private/public key authorization
	 * @param username user account
	 * @param host	server host
	 * @param port	ssh port
	 * @param identity the path of private key file.
	 * @see http://www.jcraft.com/jsch/
	 */
	public JschHandler(String username,String host,int port,String identity){
		this.username = username;
		this.host = host;
		this.port = port;
		this.identity = identity;
		
		firstInit = false;
		jsch = new JSch();
		
		authType = 0 ;
	}
	
	/**
	 * Password authorization
	 * @param username
	 * @param host
	 * @param port
	 * @param userInfo User information for authorization
	 * @see com.jcraft.jsch.UserInfo
	 * @see http://www.jcraft.com/jsch/
	 */
	public JschHandler(String username,String host,int port,UserInfo userInfo){
		this.username = username;
		this.host = host;
		this.port = port;
		this.userInfo = userInfo;
		
		firstInit = false;
		jsch = new JSch();
		authType = 1;
	}
	/**
	 * 
	 * Initialize SSH session.
	 * When the parameters is not right, It will throw an JSchException.
	 * @throws MessageServicerException
	 * @see com.jcraft.jsch.JSch
	 */
	@SuppressWarnings("static-access")
	protected void init() throws JSchException{
		try {
			validate();
			log.info("JSCH identity:"+identity);
			jsch.setLogger(new JschLogger());
			jsch.setConfig("StrictHostKeyChecking", "no");
			
			if(authType==0) jsch.addIdentity(identity);
			session = jsch.getSession(username, host, port);
			
			if(authType==1) session.setUserInfo(userInfo);
			session.connect();
			
			log.info("JSCH session connect success.");
		} catch (JSchException e) {
			log.error(e.getMessage());
			throw e;
		}
	}
	
	/**
	 * Validate parameters
	 * @throws JSchException
	 */
	private void validate() throws JSchException{
		if(firstInit) return;
		if(username==null||username.isEmpty()){
			throw new JSchException("Parameter:username is empty.");
		}
		if(host==null||host.isE