springboot連接服務器登錄harbor私人倉庫並獲取登錄憑證供k8s拉取私人倉庫鏡像時使用

引入包

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

console

@RequestMapping(value = "/harbor/getSecret", method = RequestMethod.POST)
    public JsonResponse<String> getHarborgerSecret(@RequestBody Map<String,Object> params) throws Exception {
        String userName = MapUtil.getString(params,"userName");
        String result = repositoryService.getHarborgerSecret(userName);
        return new JsonResponse<String>(result);
    }

service

public String  getHarborgerSecret(String userName) throws Exception {
        String commd = "sudo docker login  -u "+userName+" -p Harbor12345 harbor.test.com ; sudo cat /root/.docker/config.json | base64 -w 0";
        //因爲執行兩條命,所以返回值通過\n來 汲取,並且後一部分加密後的密文。
        Map map = SshUtils.execCmd(commd);
        String vaule = (String)map.get(commd);
        String result = vaule.split("\n")[1].replace("\n","");
        return result;
    }

SshUtils

package com.boe.container.util;

import com.jcraft.jsch.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class SshUtils {

   
    private static final String userName="soaadmin";
    private  static final String passwd="soaadmin";
    private static final String host="10.211.211.211";
    private static final int timeout=30000;
    public static final String LINE_SEPARATOR = System.getProperty("line.separator");

    /**
     * 連接到指定的HOST
     *
     * @return isConnect
     * @throws JSchException JSchException
     */
    public static Session connect() throws Exception {
        JSch  jsch = new JSch();
        Session session = jsch.getSession(userName, host, 22);
        if (session == null) {
            throw new Exception("session is null");
        }
        session.setPassword(passwd);
        java.util.Properties config = new java.util.Properties();
        //第一次登陸
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        try {
            session.connect(30000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return session;
    }

    /**
     * @description 執行shell命令
     * @param command shell 命令
     * @throws Exception
     */
    public static Map execCmd(String command) throws Exception {
    	
        Map map = new HashMap();
        StringBuilder stringBuffer;
        JSch  jsch = new JSch();
        Session session = jsch.getSession(userName, host, 22);
        if (session == null) {
            throw new Exception("session is null");
        }
        session.setPassword(passwd);
        java.util.Properties config = new java.util.Properties();
        //第一次登陸
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        try {
            session.connect(30000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        BufferedReader reader = null;
        Channel channel = null;
        try {
            stringBuffer = new StringBuilder();
            channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);

            channel.setInputStream(null);
            ((ChannelExec) channel).setErrStream(System.err);

            channel.connect();
            InputStream in = channel.getInputStream();
            reader = new BufferedReader(new InputStreamReader(in));
            String buf = null;
            //返回數據
            while ((buf = reader.readLine()) != null) {
                stringBuffer.append(buf.trim()).append(LINE_SEPARATOR);
                System.out.println(buf);
            }
            map.put(command, stringBuffer.toString());
            return map;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (JSchException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            channel.disconnect();
            session.disconnect();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章