Java处理SSH

JSch 登录

密码方式:session.setPassword(password);

公私秘钥方式:jsch.addIdentity("~/.ssh/id_rsaxxx");

SFTP简介

SFTP是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的加密方法。SFTP 为 SSH的一部份,是一种传输文件到服务器的安全方式。SFTP是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。

ChannelSftpAPI

ChannelSftp类是JSch实现SFTP核心类,它包含了所有SFTP的方法.

githubAPI地址 : ChannelSftp (JSch API)

sftp代码

pom依赖



  com.jcraft
  jsch
  0.1.55


  org.apache.commons
  commons-io
  1.3.2



  org.apache.logging.log4j
  log4j-slf4j-impl
  2.11.2

jar包百度自行下载

Java代码

SftpUtil

import com.jcraft.jsch.*;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.*;
import java.util.Vector;

/**
 * FTP服务器工具类:
 *      JSch类 通过 SFTP 协议上传文件到 freeSSHd 服务器
 * 
 */
public class SftpUtil {

    private Logger logger = LogManager.getLogger(SftpUtil.class);

    private ChannelSftp sftp;

    private Session session;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 秘钥
     */
    private String privateKey;

    /**
     * FTP服务器Ip
     */
    private String host;

    /**
     * FTP服务器端口号
     */
    private int port;

    /**
     * 构造器:基于密码认证sftp对象
     * @param username  用户名
     * @param password  密码
     * @param host      服务器ip
     * @param port      服务器端口号
     */
    public SftpUtil(String username, String password, String host, int port){
        this.username = username;
        this.password = password;
        this.host = host;
        this.port = port;
    }

    /**
     * 构造器:基于秘钥认证sftp对象
     * @param username   用户名
     * @param privateKey 秘钥
     * @param host       服务器ip
     * @param port       服务器端口号
     */
    public SftpUtil(String username, String privateKey, int port, String host){
        this.username = username;
        this.privateKey = privateKey;
        this.host = host;
        this.port = port;
    }

    /**
     * 连接SFTP服务器
     */
    public void login(){
        JSch jsch = new JSch();
        try{
            if(privateKey != null){
                //设置登陆主机的秘钥
                jsch.addIdentity(privateKey);
            }
            //采用指定的端口连接服务器
            session = jsch.getSession(username,host,port);
            if(password != null){
                //设置登陆主机的密码
                session.setPassword(password);
            }
            //优先使用 password 验证   注:session.connect()性能低,使用password验证可跳过gssapi认证,提升连接服务器速度
            session.setConfig("PreferredAuthentications","password");
            //设置第一次登陆的时候提示,可选值:(ask | yes | no)
            session.setConfig("StrictHostKeyChecking","no");
            session.connect();
            //创建sftp通信通道
            Channel channel = session.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            logger.info("sftp server connect success !!");
        }catch (JSchException e){
            logger.error("SFTP服务器连接异常!!", e);
        }
    }

    /**
     * 关闭SFTP连接
     */
    public void logout(){
        if(sftp != null){
            if(sftp.isConnected()){
                sftp.disconnect();
                logger.info("sftp is close already");
            }
        }
        if(session != null){
            if(session.isConnected()){
                session.disconnect();
                logger.info("session is close already");
            }
        }
    }

    /**
     * 将输入流上传到SFTP服务器,作为文件
     *
     * @param directory     上传到SFTP服务器的路径
     * @param sftpFileName  上传到SFTP服务器后的文件名
     * @param input         输入流
     * @throws SftpException
     */
    public void upload(String directory, String sftpFileName, InputStream input) throws SftpException{
        long start = System.currentTimeMillis();
        try{
            //如果文件夹不存在,则创建文件夹
            if(sftp.ls(directory) == null){
                sftp.mkdir(directory);
            }
            //切换到指定文件夹
            sftp.cd(directory);
        }catch (SftpException e){
            //创建不存在的文件夹,并切换到文件夹
            sftp.mkdir(directory);
            sftp.cd(directory);
        }
        sftp.put(input, sftpFileName);
        logger.info("文件上传成功!! 耗时:{}ms",(System.currentTimeMillis() - start));
    }

    /**
     * 上传单个文件
     *
     * @param directory     上传到SFTP服务器的路径
     * @param uploadFileUrl 文件路径
     */
    public void upload(String directory, String uploadFileUrl){
        File file = new File(uploadFileUrl);
        try{
            upload(directory, file.getName(), new FileInputStream(file));
        }catch (FileNotFoundException | SftpException e){
            logger.error("上传文件异常!", e);
        }
    }

    /**
     * 将byte[] 上传到SFTP服务器,作为文件
     *           注: 从String转换成byte[] 需要指定字符集
     *
     * @param directory     上传到SFTP服务器的路径
     * @param sftpFileName  上传SFTP服务器后的文件名
     * @param bytes         字节数组
     */
    public void upload(String directory, String sftpFileName, byte[] bytes){
        try{
            upload(directory, sftpFileName, new ByteArrayInputStream(bytes));
        }catch (SftpException e){
            logger.error("上传文件异常!", e);
        }
    }

    /**
     * 将字符串按照指定编码格式上传到SFTP服务器
     *
     * @param directory       上传到SFTP服务器的路径
     * @param sftpFileName    上传SFTP服务器后的文件名
     * @param dataStr         字符串
     * @param charsetName     字符串的编码格式
     */
    public void upload(String directory, String sftpFileName, String dataStr, String charsetName){
        try{
            upload(directory, sftpFileName, new ByteArrayInputStream(dataStr.getBytes(charsetName)));
        }catch (UnsupportedEncodingException | SftpException e){
            logger.error("上传文件异常!", e);
        }
    }

    /**
     * 下载文件
     *
     * @param directory     SFTP服务器的文件路径
     * @param downloadFile  SFTP服务器上的文件名
     * @param saveFile      保存到本地路径
     */
    public void download(String directory, String downloadFile, String saveFile){
        try{
            if(directory != null && !"".equals(directory)){
                sftp.cd(directory);
            }
            File file = new File(saveFile);
            sftp.get(downloadFile, new FileOutputStream(file));
        }catch (SftpException | FileNotFoundException e){
            logger.error("文件下载异常!", e);
        }
    }

    /**
     * 下载文件
     *
     * @param directory     SFTP服务器的文件路径
     * @param downloadFile  SFTP服务器上的文件名
     * @return              字节数组
     */
    public byte[] download(String directory, String downloadFile){
        try{
            if(directory != null && !"".equals(directory)){
                sftp.cd(directory);
            }
            InputStream inputStream = sftp.get(downloadFile);
            return IOUtils.toByteArray(inputStream);
        }catch (SftpException | IOException e){
            logger.error("文件下载异常!", e);
        }
        return null;
    }

    /**
     * 下载文件
     *
     * @param directory     SFTP服务器的文件路径
     * @param downloadFile  SFTP服务器上的文件名
     * @return              输入流
     */
    public InputStream downloadStream(String directory, String downloadFile){
        try{
            if(directory != null && !"".equals(directory)){
                sftp.cd(directory);
            }
            return sftp.get(downloadFile);
        }catch (SftpException e){
            logger.error("文件下载异常!", e);
        }
        return null;
    }

    /**
     * 删除文件
     *
     * @param directory         SFTP服务器的文件路径
     * @param deleteFileName    删除的文件名称
     */
    public void delete(String directory, String deleteFileName){
        try{
            sftp.cd(directory);
            sftp.rm(deleteFileName);
        }catch (SftpException e){
            logger.error("文件删除异常!", e);
        }
    }

    /**
     * 删除文件夹
     *
     * @param directory         SFTP服务器的文件路径
     */
    public void delete(String directory){
        Vector vector = listFiles(directory);
        vector.remove(0);
        vector.remove(0);
        for(Object v : vector){
            ChannelSftp.LsEntry lsEntry = (ChannelSftp.LsEntry)v;
            try{
                sftp.cd(directory);
                sftp.rm(lsEntry.getFilename());
            }catch (SftpException e){
                logger.error("文件删除异常!", e);
            }
        }
    }

    /**
     * 获取文件夹下的文件
     *
     * @param directory     路径
     * @return
     */
    public Vector listFiles(String directory){
        try{
            if(isDirExist(directory)){
                Vector vector =  sftp.ls(directory);
                //移除上级目录和根目录:"." ".."
                vector.remove(0);
                vector.remove(0);
                return vector;
            }
        }catch (SftpException e){
            logger.error("获取文件夹信息异常!", e);
        }
        return null;
    }

    /**
     * 检测文件夹是否存在
     *
     * @param directory     路径
     * @return
     */
    public boolean booleanUrl(String directory){
        try{
            if(sftp.ls(directory) == null){
                return false;
            }
        }catch (Exception e){
            logger.error("检测文件夹异常!", e);
        }
        return true;
    }

    /**
     * 创建一个文件目录
     *
     * @param createpath        路径
     * @return
     */
    public boolean createDir(String createpath) {
        try {
            if (isDirExist(createpath)) {
                this.sftp.cd(createpath);
                return true;
            }
            String pathArry[] = createpath.split("/");
            StringBuffer filePath = new StringBuffer("/");
            for (String path : pathArry) {
                if (path.equals("")) {
                    continue;
                }
                filePath.append(path + "/");
                if (isDirExist(filePath.toString())) {
                    sftp.cd(filePath.toString());
                } else {
                    // 建立目录
                    sftp.mkdir(filePath.toString());
                    // 进入并设置为当前目录
                    sftp.cd(filePath.toString());
                }
            }
            this.sftp.cd(createpath);
        } catch (SftpException e) {
            logger.error("目录创建异常!", e);
            return false;
        }
        return true;
    }

    /**
     * 判断目录是否存在
     * @param directory     路径
     * @return
     */
    public boolean isDirExist(String directory) {
        boolean isDirExistFlag = false;
        try {
            SftpATTRS sftpATTRS = this.sftp.lstat(directory);
            isDirExistFlag = true;
            return sftpATTRS.isDir();
        } catch (Exception e) {
            if (e.getMessage().toLowerCase().equals("no such file")) {
                isDirExistFlag = false;
            }
        }
        return isDirExistFlag;
    }
    
    /**
	 * 方法功能说明:目录不存在时创建目录  
	 * @参数: @param path      
	 * @return void     
	 * @throws
	 */
    public void mkdirs(String path){
		File file = new File(path);
		String fs = file.getParent();
		file = new File(fs);
		if(!file.exists()){
			file.mkdirs();
		}
	}
    /**
    * 判断文件或目录是否存在
    * @param directory     路径
    * @return
    */
    public boolean isExist(String path,ChannelSftp sftp){
        boolean  isExist=false;
        try {
            sftp.lstat(path);
            isExist = true;
        } catch (Exception e) {
            if (e.getMessage().toLowerCase().equals("no such file")) {
                isExist = false;
            }
        }
        return isExist;
    }
}

JschUtil

package com.jmanager.common.utils;

import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author zhou
 * @Date 2021/4/3 - 14:50
 */
public class JschUtilRe {
    Logger logger = LoggerFactory.getLogger(JschUtilRe.class);

    public static final int PORT4SSH = 22;

    private Session session;

    private JschUtilRe(String user, String host, String password) {

        session = getSession(user, host, password);
    }
    /**
     * 定义端口,获取session会话
     * @param user
     * @param host
     * @param password
     * @param port
     */
    private JschUtilRe(String user, String host, String password, int port) {

        session = getSession(user, host, password, port);
    }
    /**
     * 获得实例
     * @param user
     * @param host
     * @param password
     * @return
     */
    public static JschUtilRe getInstance(String user, String host, String password) {
        return new JschUtilRe(user, host, password);
    }
    public static JschUtilRe getInstance(String user, String host, String password, int port) {
        return new JschUtilRe(user, host, password,port);
    }
    public static String getTestHost(String user, String host, String password) {
        JSch jsch = new JSch();
        Session session = null;
        try {
            if("免密".equals(password)) {
                jsch.addIdentity("~/.ssh/id_rsa");
                session = jsch.getSession(user, host, PORT4SSH);
            } else {
                session = jsch.getSession(user, host, PORT4SSH);
                session.setPassword(password);
            }
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
        } catch (JSchException e) {
            return e.getMessage();
        } finally {
            if (session != null) {
                session.disconnect();
            }
        }
        return "yes";
    }

    public Session getSession() {
        return session;
    }
    // 获得回话session,定义端口port,ssh默认端口是22,亦可传入其他值
    private Session getSession(String user, String host, String password,int port) {
        JSch jsch = new JSch();
        Session session = null;
        logger.info(user + host +password + port);
        try {
            if("免密".equals(password)) {
                jsch.addIdentity("~/.ssh/id_rsa");
                session = jsch.getSession(user, host, port);
            } else {
                session = jsch.getSession(user, host, port);
                session.setPassword(password);
            }
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
        } catch (JSchException e) {
            logger.error(e.toString());
        }
        return session;
    }
    // 获得回话session
    private Session getSession(String user, String host, String password) {
        JSch jsch = new JSch();
        Session session = null;
        try {
            if("免密".equals(password)) {
                jsch.addIdentity("~/.ssh/id_rsa");
                session = jsch.getSession(user, host, PORT4SSH);
            } else {
                session = jsch.getSession(user, host, PORT4SSH);
                session.setPassword(password);
            }
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
        } catch (JSchException e) {
            logger.error(e.toString());
        }
        return session;
    }

    // 执行command
    public String execCommand(String cmd) {
        StringBuffer result = new StringBuffer("");
        BufferedReader reader = null;
        Channel channel = null;
        Session session = this.getSession();
        try {
            channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(cmd);
            channel.setInputStream(null);
            channel.connect();
            InputStream in = channel.getInputStream();
            reader = new BufferedReader(new InputStreamReader(in, Charset.forName("utf-8")));
            String buf = null;
            while ((buf = reader.readLine()) != null) {
                result.append(buf + "\n");
            }
        } catch (Exception e) {
            logger.error(e.toString());
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                logger.error(e.toString());
            }
            if (channel != null) {
                channel.disconnect();
            }
        }
        return result.toString();
    }

    public String execCommand(String cmd, String code) {
        StringBuffer result = new StringBuffer("");
        BufferedReader reader = null;
        Channel channel = null;
        Session session = this.getSession();
        try {
            channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(cmd);
            channel.setInputStream(null);
            channel.connect();
            InputStream in = channel.getInputStream();
            reader = new BufferedReader(new InputStreamReader(in, Charset.forName(code)));
            String buf = null;
            while ((buf = reader.readLine()) != null) {
                result.append(buf + "\n");
            }
        } catch (Exception e) {
            logger.error(e.toString());
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                logger.error(e.toString());
            }
            if (channel != null) {
                channel.disconnect();
            }
        }
        return result.toString();
    }

    // 检查目录是否存在,不存在自动创建
    public void checkDir(String dir) {
        StringBuffer buf = new StringBuffer("[[ -d ");
        buf.append(dir);
        buf.append(" ]] || mkdir -p " + dir);
        String result = execCommand(buf.toString());
    }
    public boolean checkFileExist(String fileName) {
        String result = execCommand("ls " + fileName);
        return result.contains(fileName);
    }
    // 读取文件中内容
    public String headFileContent(String pathAndFileName) {
        String content = null;
        String cmd = ". ~/.bash_profile;head -n 10 " + pathAndFileName;
        content = execCommand(cmd);
        return content;
    }
    // 获得某路径下的文件
    public ArrayList getDirFileList(String path) {
        ArrayList fileList = new ArrayList();
        boolean checked = checkPathExist(path);
        if (checked) {

            String cmd = "cd " + path + ";ls -l";
            String result = execCommand(cmd);
            String[] results = result.split("\n");
            for (String res : results) {
                if (res.startsWith("-")) {
                    String[] ress = res.split(" ");
                    fileList.add(ress[ress.length - 1]);
                }
            }
        }
        return fileList;
    }
    // 读取文件中内容
    public String readFileContent(String pathAndFileName) {
        String content = null;
        String cmd = ". ~/.bash_profile;cat " + pathAndFileName;
        content = execCommand(cmd);
        return content;
    }
    // 逐行读取文件中内容
    public List readFileContentByLine(String pathAndFileName) {
        List res = new ArrayList<>();
        if (checkFileExist(pathAndFileName)) {
            Channel channel = null;
            try {
                channel = session.openChannel("sftp");
                channel.connect();
                ChannelSftp sftp = (ChannelSftp) channel;
                InputStream stream = sftp.get(pathAndFileName);
                BufferedReader br = new BufferedReader(new InputStreamReader(stream));
                String line;
                while ((line = br.readLine()) != null) {
                    res.add(line);
                }
            } catch (JSchException | SftpException | IOException e) {
                e.printStackTrace();
            } finally {
                if (channel != null) {
                    channel.disconnect();
                }
            }
        }
        return res;
    }
    // 读取文件中内容
    public String readFileContent(String path, String fileName) {
        String content = null;
        String cmd = "cat " + path + "/" + fileName;
        content = execCommand(cmd);
        return content;
    }

    // 写入文件
    public void writeFileContent(String path, String fileName, String content) {

        byte[] temp;
        String newStr = "";
        try {
            temp = content.getBytes("utf-8");
            newStr = new String(temp, "gbk");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        String cmd = "cat >" + path + "/" + fileName + " << EOF\n" + newStr + "\nEOF";
        content = execCommand(cmd);
    }
    // 写入文件
    public void writeFileContent(String pathAndFileName, String content) {
        String cmd = ". ~/.bash_profile;cat >" + pathAndFileName + " << 'EOF' \n" + content + "\nEOF";
        execCommand(cmd);
    }
    // 文件备份备份到该路径的backup文件下
    public void backupFile(String path, String fileName) {

        checkDir(path + "/backup");
        StringBuffer cmd = new StringBuffer("scp -r ");
        cmd.append(path).append("/").append(fileName).append(" ").append(path).append("/backup/").append(getTimeStr())
                .append(fileName);
        execCommand(cmd.toString());

    }

    private String getTimeStr() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        String str = sdf.format(new Date());
        return str;
    }

    // 指定路径新建文件
    public void createFile(String path, String fileName) {
        checkDir(path);
        String cmd = "touch " + path + "/" + fileName;
        execCommand(cmd);
    }

    // 检测路径存在与否
    public boolean checkPathExist(String path) {
        String[] paths = null;
        String p;
        if (path.indexOf("/") != 0) {
            paths = path.split("/");
            p = "";
        } else {
            paths = path.substring(1, path.length()).split("/");
            p = "/";
        }
        for (int i = 0; i < paths.length; i++) {
            String result = execCommand("cd " + p + ";ls");
            List resultList = Arrays.asList(result.split("\n"));
            if (!resultList.contains(paths[i])) {
                return false;
            }
            p += paths[i] + "/";
        }
        return true;
    }

    // 文件copy
    public void copyFile(String pathFrom, String pathTo) {
        StringBuffer buf = new StringBuffer("scp -r ");
        buf.append(pathFrom);
        buf.append(" ");
        buf.append(pathTo);
        // System.out.println(buf.toString());
        execCommand(buf.toString());
    }

    // 关闭session
    public void closeSession() {
        if (session != null) {
            session.disconnect();
        }
    }

    public static void main(String[] args) {

        JschUtilRe jr = JschUtilRe.getInstance("zhou", "127.0.0.1", "xxxxxx");
        String resutlt = jr.execCommand("$HOME/.bash_profile;sh  /zhou/work/test.sh","gbk");
        jr.closeSession();
        System.out.println("resutlt = " + resutlt);

    }
}

你可能感兴趣的:(Java基础,log4j,apache,ssh)