1、安装依赖
<!-- ssh连接执行Shell-->
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>
2、编写工具类
package com.example.springboot3.utils;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.charset.Charset;
public class RemoteShellTool {
private Connection conn;
private String ipAddr;
private String charset = Charset.defaultCharset().toString();
private String userName;
private String password;
public RemoteShellTool(String ipAddr, String userName, String password,
String charset) {
this.ipAddr = ipAddr;
this.userName = userName;
this.password = password;
if (charset != null) {
this.charset = charset;
}
}
public boolean login() throws IOException {
conn = new Connection(ipAddr); //默认端口22
conn.connect(); // 连接
return conn.authenticateWithPassword(userName, password); // 认证
}
public void close() {
conn.close();
}
public void execShell(String cmds) {
try {
Session session = conn.openSession(); // 打开一个会话
session.requestDumbPTY();
session.startShell();
PrintWriter pw = new PrintWriter(session.getStdin());
pw.println(cmds);
pw.close();
session.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
//获取查询结果
public String exec(String cmds) {
InputStream in = null;
String result = "";
try {
Session session = conn.openSession(); // 打开一个会话
session.execCommand(cmds);
in = session.getStdout();
result = this.processStdout(in, this.charset);
in.close();
session.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return result;
}
public String processStdout(InputStream in, String charset) {
byte[] buf = new byte[1024];
StringBuffer sb = new StringBuffer();
try {
while (in.read(buf) != -1) {
sb.append(new String(buf, charset));
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
}
3、执行工具类
package com.example.springboot3.controller;
import com.example.springboot3.utils.RemoteShellTool;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SshController {
@GetMapping("/ssh")
public String start() {
RemoteShellTool tool = new RemoteShellTool("localhost", "xxxxx",
"xxxxxx", "utf-8");
try {
if(!tool.login()){
return "服务器连接失败";
}
} catch (Exception e) {
e.printStackTrace();
}
// /**
// * 服务是否已经停止
// */
// String name = param.getEdition();
// ReleaserRecord oldRecord = mapper.getByPublisId(param.getPublishConfigurationId());
// String oldname = name;
// if(oldRecord != null){
// oldname = oldRecord.getEdition();
// }
//
// boolean isStart = close(tool, 0,oldname);
// if (isStart) {
// tool.execShell(param.getPreCommand()+" "+host.getTargetPath()+ name + " " +param.getPostCommand());
// boolean start = isStart(tool, 0,name);
// if(!start){
// return new R(-1, "服务启动超时");
// }
// } else {
// return new R(-1, "服务停止超时");
// }
String result=tool.exec("df -h");
tool.close();
System.out.println("计算结果");
System.out.println(result);
return result;
}
}