#java #sshd #apache-sshd
Вопрос:
я пытаюсь создать канал ssh exec с помощью Apache SSHD с ключом пользователя Putty и парольной фразой. Но я не знаю, как я добавляю их в качестве идентификаторов сеанса.
Наша компания заблокировала 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);