JSch基本使用
JSch 是SSH2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。你可以将它的功能集成到你自己的 程序中。同时该项目也提供一个J2ME版本用来在手机上直连SSHD服务器。
官网:http://www.jcraft.com/jsch/中有很多例子http://www.jcraft.com/jsch/examples/,这里先采用(已做修改)其中2个来进行简单论述,希望对大家有所帮助。
本文采用的jsch版本是0.1.51. 下载地址:http://sourceforge.net/projects/jsch/files/jsch/0.1.54/jsch-0.1.54.zip/download。
本文采用的Linux操作系统是CentOS6.5.
TIPS: 查看Linux操作系统(内核)版本可以使用:uname -a; uname -r; cat /etc/issue; cat /etc/redhat-release等命令。
package com.lls.jsch; import com.jcraft.jsch.*; import java.io.*; import java.util.Properties; public class JschTest { public Session getSshSession(SshInfo sshInfo ){ JSch jSch = new JSch(); Session session=null; try{ session = jSch.getSession(sshInfo.getUserName(),sshInfo.getIp(),sshInfo.getPort()); session.setPassword(sshInfo.getPassword()); Properties conf = new Properties(); conf.setProperty("StrictHostKeyChecking","no"); session.setConfig(conf); session.connect(); } catch (JSchException e) { e.printStackTrace(); } return session; } /** * 其他操作系统执行linux 命令 * @param sshInfo * @param linuxCmd */ public void exeLinuxCmd(SshInfo sshInfo ,String linuxCmd){ Session session = null; ChannelExec exec = null; try{ session = getSshSession(sshInfo); exec = (ChannelExec) session.openChannel("exec"); exec.setCommand(linuxCmd); exec.setInputStream(null); exec.setErrStream(System.err); exec.connect(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(exec.getInputStream())); String msg = null; while ((msg = bufferedReader.readLine()) != null){ System.out.println(String.format("执行命令$%1$s返回消息%2$s",linuxCmd,msg)); } } catch (JSchException | FileNotFoundException | SftpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(exec != null && exec.isConnected()){ exec.disconnect(); } if(session != null && session.isConnected()){ session.disconnect(); } } } /** * 其他操作系统向linux 系统传文件 * @param sshInfo * @param localFilePath * @param remoteDirPath * @throws IOException */ public void uploadFile(SshInfo sshInfo,String localFilePath,String remoteDirPath) throws IOException { Session session = null; ChannelSftp sftp = null; InputStream inputStream = null; try{ session = getSshSession(sshInfo); sftp = (ChannelSftp) session.openChannel("sftp"); sftp.connect(); sftp.cd(remoteDirPath); File file = new File(localFilePath); if(file.exists()){ inputStream = new FileInputStream(file); sftp.put(inputStream,file.getName()); } } catch (JSchException | FileNotFoundException | SftpException e) { e.printStackTrace(); }finally { if(inputStream != null){ inputStream.close(); } if(sftp != null && sftp.isConnected()){ sftp.disconnect(); } if(session != null && session.isConnected()){ session.disconnect(); } } } class SshInfo{ private String userName; private String password; private String ip; private int port; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } } }