Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,19 @@ public class MinaUtils {

/** 打开远程会话 */
public static ClientSession openConnection(String sshHost, Integer sshPort, String sshUser) {
SshClient sshClient = SshClient.setUpDefaultClient();
sshClient.start();
ClientSession session = null;
String privateKeyPath = System.getProperty("user.home") + Constants.ID_RSA;
try {
try (SshClient sshClient = SshClient.setUpDefaultClient()) {
sshClient.start();
String privateKeyPath = System.getProperty("user.home") + Constants.ID_RSA;

String privateKeyContent = new String(Files.readAllBytes(Paths.get(privateKeyPath)));
session = sshClient.connect(sshUser, sshHost, sshPort).verify().getClientSession();
session.addPublicKeyIdentity(getKeyPairFromString(privateKeyContent));
if (session.auth().verify().isFailure()) {
LOG.info("验证失败");
return null;
}

} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down