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