From 4c01065a751030d078b802934bca3b16c1d7441c Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Wed, 10 Mar 2021 00:22:27 +0100 Subject: [RemoteExec/RemoteReboot] convert ssh key to a format supported by JSch --- .../java/org/openslx/taskmanager/tasks/RemoteExec.java | 13 +++++++++++++ .../org/openslx/taskmanager/tasks/RemoteReboot.java | 17 ++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/java/org/openslx/taskmanager/tasks/RemoteExec.java b/src/main/java/org/openslx/taskmanager/tasks/RemoteExec.java index 3bd0954..8e88599 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/RemoteExec.java +++ b/src/main/java/org/openslx/taskmanager/tasks/RemoteExec.java @@ -9,6 +9,10 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import com.sshtools.common.publickey.InvalidPassphraseException; +import com.sshtools.common.publickey.SshKeyUtils; +import com.sshtools.common.publickey.bc.OpenSSHPrivateKeyFileBC; +import com.sshtools.common.ssh.components.SshKeyPair; import org.openslx.satserver.util.Util; import org.openslx.taskmanager.api.AbstractTask; @@ -77,6 +81,15 @@ public class RemoteExec extends AbstractTask if ( status.error != null ) return false; + // Convert ssh key to old rsa format supported by JSch + try { + SshKeyPair key = SshKeyUtils.getPrivateKey(sshkey, ""); + sshkey = new String(new OpenSSHPrivateKeyFileBC(key, "").getFormattedKey(), + StandardCharsets.UTF_8); + } catch (IOException | InvalidPassphraseException e) { + e.printStackTrace(); + } + JSch.setConfig( "StrictHostKeyChecking", "no" ); sshClient = new JSch(); try { diff --git a/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java b/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java index f5d9a4b..1913891 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java +++ b/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java @@ -1,7 +1,9 @@ package org.openslx.taskmanager.tasks; +import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; @@ -13,6 +15,10 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import com.sshtools.common.publickey.InvalidPassphraseException; +import com.sshtools.common.publickey.SshKeyUtils; +import com.sshtools.common.publickey.bc.OpenSSHPrivateKeyFileBC; +import com.sshtools.common.ssh.components.SshKeyPair; import org.openslx.taskmanager.api.AbstractTask; import com.google.gson.annotations.Expose; @@ -80,7 +86,16 @@ public class RemoteReboot extends AbstractTask if ( status.error != null ) return false; - + + // Convert ssh key to old rsa format supported by JSch + try { + SshKeyPair key = SshKeyUtils.getPrivateKey(sshkey, ""); + sshkey = new String(new OpenSSHPrivateKeyFileBC(key, "").getFormattedKey(), + StandardCharsets.UTF_8); + } catch (IOException | InvalidPassphraseException e) { + e.printStackTrace(); + } + Date shutdownTime = new Date( System.currentTimeMillis() + minutes * 60 * 1000 ); SimpleDateFormat sdf = new SimpleDateFormat( "HH:mm" ); status.time = sdf.format( shutdownTime ); -- cgit v1.2.3-55-g7522