带有PuTTYUserKeyFile和密码短语的java Apache SSHD客户端会话
我尝试使用ApacheSSHD和Putty用户密钥和密码短语创建SSHEXEC通道。 但我不知道如何将它们添加为会话标识
我们公司用密码阻止了ssh连接(一些安全问题),我需要用这种方式建立连接
我们的ppk文件是这样的:
PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: imported-openssh-key
Public-Lines: 12
.
.
.
Private-Lines: 28
.
.
.
致以最良好的祝愿
SshClient sshClient = SshClient.setUpDefaultClient();
sshClient.start();
int defaultTimeout = 5000;
File ppk = new File("path/to/file.ppk");
String passphrase = "passphrase";
try (ClientSession session = sshClient.connect("root", "192.168.72.128", 22).verify(defaultTimeout)
.getSession()) {
// here, how i add ppk file and passphrase to session ?
session.auth().verify(defaultTimeout);
ChannelExec channelExec;
int exitStatus;
ByteArrayOutputStream responseStream;
ByteArrayOutputStream errorStream;
String command = "ls -al";
String error;
String respond;
// to execute remote commands
try {
responseStream = new ByteArrayOutputStream();
errorStream = new ByteArrayOutputStream();
channelExec = session.createExecChannel(command);
channelExec.setOut(responseStream);
channelExec.setErr(errorStream);
try {
channelExec.open().verify(defaultTimeout);
channelExec.waitFor(EnumSet.of(ClientChannelEvent.CLOSED),
TimeUnit.MILLISECONDS.toMillis(defaultTimeout));
exitStatus = channelExec.getExitStatus();
System.out.println("Exit Status: " + exitStatus);
error = new String(errorStream.toByteArray());
if (!error.isEmpty()) {
throw new Exception(error);
}
respond = responseStream.toString();
System.out.println(respond);
} finally {
channelExec.close(false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
sshClient.stop();
对于JSch,我使用addIdentity()函数完成了以下操作:
JSch jsch = new JSch();
File ppk = new File("path/to/file.ppk");
String passphrase = "passphrase";
Session session = jsch.getSession("user", "ip", 22);
jsch.addIdentity(ppk.getAbsolutePath(), passphrase);
共 (0) 个答案