From 1bbd47459c3ea8c1b3235b386a2630c7c11125dc Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Tue, 27 Mar 2018 17:34:25 +0200 Subject: [RemoteReboot] Switched from the jcabi-ssh library to the jsch library. --- pom.xml | 6 +-- .../openslx/taskmanager/tasks/RemoteReboot.java | 54 ++++++++++++++++++---- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/pom.xml b/pom.xml index 7fe7fc7..0eab9c2 100644 --- a/pom.xml +++ b/pom.xml @@ -111,9 +111,9 @@ compile - com.jcabi - jcabi-ssh - 1.5.2 + com.jcraft + jsch + 0.1.54 diff --git a/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java b/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java index 9f450b6..38a9c12 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java +++ b/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java @@ -1,6 +1,5 @@ package org.openslx.taskmanager.tasks; -import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.text.SimpleDateFormat; @@ -17,8 +16,10 @@ import java.util.concurrent.TimeUnit; import org.openslx.taskmanager.api.AbstractTask; import com.google.gson.annotations.Expose; -import com.jcabi.ssh.SSH; -import com.jcabi.ssh.Shell; +import com.jcraft.jsch.ChannelExec; +import com.jcraft.jsch.JSch; +import com.jcraft.jsch.JSchException; +import com.jcraft.jsch.Session; public class RemoteReboot extends AbstractTask { @@ -42,6 +43,8 @@ public class RemoteReboot extends AbstractTask @Expose private int port; + + private JSch sshClient; private Output status = new Output(); @@ -73,6 +76,16 @@ public class RemoteReboot extends AbstractTask status.time = sdf.format( shutdownTime ); status.locationId = locationId; status.locationName = locationName; + + JSch.setConfig("StrictHostKeyChecking", "no"); + sshClient = new JSch(); + try { + sshClient.addIdentity("", sshkey.getBytes(), null, null); + } catch ( JSchException e ) { + status.addError( e.toString() ); + return false; + } + return true; } @@ -89,25 +102,36 @@ public class RemoteReboot extends AbstractTask status.addError( "null Client or missing ip/uuid in list, ignoring." ); continue; } - status.clientStatus.put( client.machineuuid, ClientStatus.CONNECTING ); tp.submit( new Runnable() { public void run() { int ret = -1; try { - Shell shell = new SSH( client.clientip, port, "root", sshkey ); - String args = " " + minutes + " " + SSH.escape( client.machineuuid ); + status.clientStatus.put( client.machineuuid, ClientStatus.CONNECTING ); + Session session = sshClient.getSession("root", client.clientip, port); + session.connect(5000); + ChannelExec channel = (ChannelExec) session.openChannel("exec"); + + String args = " " + minutes + " " + String.format("'%s'", client.machineuuid.replace("'", "'\\''")); if ( shutdown ) { - ret = new Shell.Empty( shell ).exec( SHUTDOWN_CMD + args ); + channel.setCommand( SHUTDOWN_CMD + args ); + channel.connect(2000); + waitForCommand(channel, 2000); + ret = channel.getExitStatus(); status.clientStatus.put( client.machineuuid, minutes == 0 ? ClientStatus.SHUTDOWN : ClientStatus.SHUTDOWN_AT ); } else { - ret = new Shell.Empty( shell ).exec( REBOOT_CMD + args ); + channel.setCommand( REBOOT_CMD + args ); + channel.connect(2000); + waitForCommand(channel, 2000); + ret = channel.getExitStatus(); if ( ret == 0 ) { status.clientStatus.put( client.machineuuid, minutes == 0 ? ClientStatus.REBOOTING : ClientStatus.REBOOT_AT ); rebootingClients.add( client ); } } - } catch ( IOException e ) { + channel.disconnect(); + session.disconnect(); + } catch ( JSchException e ) { if ( e.toString().contains( "Auth fail" ) ) { status.clientStatus.put( client.machineuuid, ClientStatus.AUTH_FAIL ); ret = 0; @@ -116,6 +140,7 @@ public class RemoteReboot extends AbstractTask ret = -1; } } + if ( ret != 0 ) { if ( ret != -1 ) { status.addError( client.clientip + ": Exit Code " + ret ); @@ -184,6 +209,15 @@ public class RemoteReboot extends AbstractTask return true; } + + private void waitForCommand(ChannelExec channel, int timeout) { + for ( int i = 0; i < timeout / 100; i++ ) { + if ( channel.isClosed() ) { break; } + try { + Thread.sleep(100); + } catch (InterruptedException e) {} + } + } private boolean isOnline( String address, int... ports ) { @@ -210,7 +244,7 @@ public class RemoteReboot extends AbstractTask private String locationName; private String error; - private void addError( String e ) + private synchronized void addError( String e ) { if ( error == null ) { error = e + "\n"; -- cgit v1.2.3-55-g7522