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

java ssh连服务器执行命令

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

java ssh连服务器执行命令


import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.credentials.AccessTokenAuthentication;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;


@Slf4j
public class SshInitTest {

    private  String IP = "";// 远程服务器地址
    private  String USR = "";// 远程服务器用户名
    private  String PSWORD = ""; // 远程服务器密码


    private  String DEFAULTCHART = "UTF-8";

    private  Connection connection = new Connection(IP);// 创建对象

    
    public  boolean login() {
        //创建远程连接,默认连接端口为22,如果不使用默认,可以使用方法
        try {
            connection.connect();
            //使用用户名和密码登录 有秘钥可以使用authenticateWithPublicKey验证
            return connection.authenticateWithPassword(USR, PSWORD);
        } catch (IOException e) {
            log.error("用户%s密码%s登录服务器%s失败!", USR, PSWORD, IP, e);
        }
        return false;
    }

    
    public  String execute(String cmd) {
        String result = "";
        try {
            boolean isAuthed = login();
            if (isAuthed && connection != null) {
                Session session = connection.openSession();//打开一个会话
                session.execCommand(cmd);//执行命令
                result = processStdout(session.getStdout(), DEFAULTCHART);
                //如果为得到标准输出为空,说明脚本执行出错了
                if (StringUtils.isBlank(result)) {
                    log.info("得到标准输出为空,链接connection:" + connection + ",执行的命令:" + cmd);
                    result = processStdout(session.getStderr(), DEFAULTCHART);
                } else {
                    log.info("执行命令成功,链接connection:" + connection + ",执行的命令:" + cmd);
                }
                connection.close();
                session.close();
            }
        } catch (IOException e) {
            log.error("执行命令失败,链接connection:" + connection + ",执行的命令:" + cmd + "  " + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    
    private static String processStdout(InputStream in, String charset) {
        InputStream stdout = new StreamGobbler(in);
        StringBuffer buffer = new StringBuffer();
        ;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line + " ");
            }
        } catch (UnsupportedEncodingException e) {
            log.error("解析脚本出错:" + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            log.error("解析脚本出错:" + e.getMessage());
            e.printStackTrace();
        }
        return buffer.toString();
    }

    public static void main(String[] args) {
        SshInitTest sshInit = new SshInitTest();
        System.out.println(sshInit.getToken());
    }

    public  String getToken(){
        String token = execute("./dashborad/get-token.sh");
        return token.trim().replace("=", "");
    }

}

        
            io.kubernetes
            client-java
            5.0.0
        

        
			ch.ethz.ganymed
			ganymed-ssh2
			build210
		

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

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

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