栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 私钥登录ssh

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 私钥登录ssh

package org.utils.ssh;

import com.jcraft.jsch.*;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import java.util.concurrent.TimeUnit;


public class PublicKeyLogin {

    private static Session session;

    private static Channel channel;

    public static Session getSession() {
        return session;
    }

    public static Channel getChannel() {
        return channel;
    }

    
    public  static Session  getSession(String host,String port,String userName,String pkeyPath,String securityLevel) throws JSchException {
        if(session != null ){

                return  session;

        }
        //从配置文件中读取 目标服务器ip、端口、用户名、私钥路径
//        String ftpHost = prop.getProperty("downloadHost");
//        String port = prop.getProperty("downloadftpPort");
//        String ftpUserName = prop.getProperty("downloadUserName");
//        String priKeyBasePath = prop.getProperty("priBaseKeyPath");
        //建立JSch对象
        JSch jsch = new JSch();
        try{
            //添加私钥
            jsch.addIdentity(pkeyPath);
            try{
            session=jsch.getSession(userName, host);
            }catch (Exception e){
                System.out.println("ss"+e.getMessage());
            }
            Properties sessionConfig = new Properties();
            //SSH 公钥检查机制 no、ask、yes
            
            sessionConfig.put("StrictHostKeyChecking", securityLevel);
            session.setConfig(sessionConfig);
           // session.connect();
            return  session;
//            channelSftp = (ChannelSftp) session.openChannel("sftp"); // 打开SFTP通道
//            channelSftp.connect();
//            //进行操作 如进入指定文件夹
//            channelSftp.cd(config.getFileDir());
        }catch (JSchException e) {
           throw  new JSchException();
        }
    }

    
    public static void setPassPhrase(String passPhrase){
        UserInfo ui = new GoldwindUser(passPhrase);
        session.setUserInfo(ui);
    }



    
    public static Channel getChannel(int timeout,String type) throws JSchException {
        //获取jsch的会话
        //Properties config = new Properties();
        //连接  超时时间30s,30000
        //session.connect(timeout);
        //开启shell通道,shell
        channel = session.openChannel(type);
        //通道连接 超时时间3s
        //channel.connect(timeout);
        return channel;
    }

    public  static String runExec(String commands,String charset) throws JSchException, IOException {
        try {
            ChannelExec channelExec = (ChannelExec) getChannel(50000, "exec");
            System.out.println(PublicKeyLogin.getSession().isConnected());
            channelExec.setPty(false);
            channelExec.setCommand(commands);
            channelExec.connect();
            InputStream std = channelExec.getInputStream();
            InputStream error = channelExec.getErrStream();
            BufferedReader brStd = new BufferedReader(new InputStreamReader(std, StandardCharsets.UTF_8));
            BufferedReader brError = new BufferedReader(new InputStreamReader(error, StandardCharsets.UTF_8));
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = brStd.readLine()) != null) {
                if (!line.equals("")) {
                    sb.append(line + "n");
                }
            }

            while ((line = brError.readLine()) != null) {
                if (!line.equals("")) {
                    sb.append(line + "n");
                }
            }
            channelExec.disconnect();
            return  sb.substring(0);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return  "";
        }

    }


    
    public  static String runExec(String[] commands,String charset) throws JSchException, IOException {

        //String[] commands = new String[]{"ls -l ","echo 123123"};
        String command = commands[0];
        for (int i = 1; i  0) {
                byte[] data = new byte[instream.available()];
                int nLen = instream.read(data);

                if (nLen < 0) {
                    throw new Exception("network error...桌面有错误");
                }

                temp = new String(data, 0, nLen, "UTF-8");
            }
        }  finally {
            outstream.close();
            instream.close();
        }
        return temp;
    }

    public static  void closeSession() {

        session.disconnect();
    }

    public static void closeChannel(){
        channel.disconnect();
    }

    public  static void  close(){
        channel.disconnect();
        session.disconnect();
    }
    public static class GoldwindUser implements  UserInfo{
        private String passphrase = null;

        public GoldwindUser(String passphrase) {
            this.passphrase = passphrase;
        }
        @Override
        public String getPassphrase() {
            return passphrase;
        }

        @Override
        public String getPassword() {
            return null;
        }

        @Override
        public boolean promptPassword(String s) {
            return true;
        }

        @Override
        public boolean promptPassphrase(String s) {
            return true;
        }

        @Override
        public boolean promptYesNo(String s) {
            return true;
        }

        @Override
        public void showMessage(String s) {
            System.out.println("passPhrase:"+s);
        }
    }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1024453.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号