由于需要调用shell脚本实现一些自动化控制操作,特记录一下实现代码。
主要使用到Ganymed SSH-2 for Java包,是java实现SSH-2协议的一个包,利用它在Java程序中连接SSH服务器。
包地址:点我下载
maven仓库地址:点我打开
pom.xml配置
......
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- SSH-2协议的包 -->
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>
</dependencies>
......
yml配置文件
ssh1:
host: 192.168.227.3
port: 22
user: root
password: 123
ssh2:
host: 192.168.227.4
port: 22
user: root
password: 123
实体类
package com.bq8023.task.domain;
import lombok.Data;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* @Author bq8023
* @Date 21/5/9 19:36
* @Version 1.0
*/
@Data
public class SSHEntity {
/**
* 编码
*/
private Charset charset = StandardCharsets.UTF_8;
/**
* 主机(IP)
*/
private String host;
/**
* 连接端口
*/
private int port;
/**
* 账号
*/
private String user;
/**
* 密码
*/
private String password;
}
配置服务器SSH
package com.bq8023.task.conf;
import com.bq8023.task.domain.SSHEntity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 配置各服务器ssh连接信息
*
* @Author bq8023
* @Date 21/5/9 20:44
* @Version 1.0
*/
@Configuration
public class SSHConfig {
@Bean(name = "ssh1")
@ConfigurationProperties(prefix = "ssh1")
public SSHEntity sshEntity3() {
return new SSHEntity();
}
@Bean(name = "ssh2")
@ConfigurationProperties(prefix = "ssh2")
public SSHEntity sshEntity4() {
return new SSHEntity();
}
}
可以在此处配置多台服务器
具体业务
package com.bq8023.task.service;
import com.bq8023.task.domain.SSHEntity;
import org.springframework.stereotype.Service;
import java.io.IOException;
/**
* @Author bq8023
* @Date 21/5/9 19:56
* @Version 1.0
*/
@Service
public interface SSHService {
/**
* 连接服务器并 执行shell脚本
*
* @param sshEntity ssh连接信息
* @param shell shell语句
* @return shell执行结果
* @throws IOException 读写异常
*/
StringBuilder exec(SSHEntity sshEntity, String shell) throws IOException;
}
package com.bq8023.task.service.serviceImpl;
import com.bq8023.task.domain.SSHEntity;
import com.bq8023.task.service.SSHService;
import java.io.IOException;
import java.io.InputStream;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import org.springframework.stereotype.Service;
/**
* @Author bq8023
* @Date 21/5/9 19:57
* @Version 1.0
*/
@Service
public class SSHServiceImpl implements SSHService {
private Connection connect;
/**
* 连接服务器并 执行shell脚本
*
* @param sshEntity ssh连接信息
* @param shell shell语句
* @return shell执行结果
* @throws IOException 读写异常
*/
@Override
public StringBuilder exec(SSHEntity sshEntity, String shell) throws IOException {
InputStream inputStream = null;
StringBuilder result = new StringBuilder();
try {
// 认证登录信息
if (this.login(sshEntity)) {
// 登陆成功则打开一个会话
Session session = connect.openSession();
session.execCommand(shell);
inputStream = session.getStdout();
result = this.processStdout(inputStream);
connect.close();
}
} finally {
if (null != inputStream) {
inputStream.close();
}
}
return result;
}
private boolean login(SSHEntity sshEntity) {
connect = new Connection(sshEntity.getHost(), sshEntity.getPort());
try {
connect.connect();
return connect.authenticateWithPassword(sshEntity.getUser(), sshEntity.getPassword());
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private StringBuilder processStdout(InputStream in) {
byte[] buf = new byte[1024];
StringBuilder builder = new StringBuilder();
try {
int length;
while ((length = in.read(buf)) != -1) {
builder.append(new String(buf, 0, length));
}
} catch (IOException e) {
e.printStackTrace();
}
return builder;
}
}
测试
package com.bq8023.task;
import com.bq8023.task.domain.SSHEntity;
import com.bq8023.task.service.SSHService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.IOException;
@SpringBootTest
class JdtaskApplicationTests {
@Qualifier("SSHServiceImpl")
@Autowired
private SSHService sshService;
@Resource(name = "ssh1")
private SSHEntity sshEntity;
@Test
void contextLoads() throws IOException {
StringBuilder stringBuilder = sshService.exec(sshEntity, "pwd");
System.out.println(stringBuilder);
}
}
结果输出