Jump to content
Server Maintenance This Week. ×

Executing SSH commands


This topic is 2633 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Here's some code I put together using the Ganymed SSH java library and examples to execute SSH commands using ScriptMaster. Its easier than doing SSH calls from RunShellScript. I'm sure there's improvements to be made. 

 

Cheers,

JP

// SSHCommand ( hostname ; privateKey ; username ; command )
// Uses Commons-Io-2.4.jar
// Uses Commons-Lang-2.6.jar
// Uses Ganymed-Ssh2-261.jar

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

final String stdout = new String();
final String stderr = new String();


try {

// Convert private key to character array
char[] privateKeyArray = privateKey.toCharArray();

// Create and open connection
Connection theConnection = new Connection(hostname);
theConnection.connect();

// Authenticate with public (private really) key
boolean isSuccess = theConnection.authenticateWithPublicKey(username, privateKeyArray, null);
if (isSuccess == false)
throw new RuntimeException( "ERROR: Authentication error" );

// Open seession and execute command
final Session theSession = theConnection.openSession();
theSession.execCommand(command);

// Get stdout stream and convert to string
final InputStream stdoutStream = new StreamGobbler(theSession.getStdout());
final InputStream stderrStream = new StreamGobbler(theSession.getStderr());

stdout = IOUtils.toString(stdoutStream, "UTF-8");
stderr = IOUtils.toString(stderrStream, "UTF-8");

// Close streams
stdoutStream.close();
stderrStream.close();

// Close the session and connection
theSession.close();
theConnection.close();   

if (StringUtils.isNotEmpty(stderr))
throw new RuntimeException( stderr );

// Return the string
return stdout;
}
catch (IOException e) {
throw new RuntimeException( e );
}
  • Like 1
Link to comment
Share on other sites

*Changed to using the more updated Orion/Trilead SSH2 libraries. 

http://sourceforge.net/apps/mediawiki/orion-ssh2/index.php

 

Change:

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

To:

import com.trilead.ssh2.Connection;
import com.trilead.ssh2.Session;
import com.trilead.ssh2.StreamGobbler;
Link to comment
Share on other sites

  • 11 months later...

Some small improvements making it more Groovy, less Java. Groovy overloads the methods so can be less verbose..

 

StringUtils not needed as if (stderr) is the same as if (StringUtils.isNotEmpty(stderr)) and saves loading another jar just for that one line

No need for import java.io.IOException as that comes for free with Groovy - just makes the code cleaner..

import org.apache.commons.io.IOUtils
//import org.apache.commons.lang.StringUtils

import com.trilead.ssh2.Connection
import com.trilead.ssh2.Session
import com.trilead.ssh2.StreamGobbler

stdout = ''
stderr = ''

try {

// Convert private key to character array
char[] privateKeyArray = privateKey.toCharArray()

// Create and open connection
theConnection = new Connection(hostname)
theConnection.connect()

// Authenticate with public (private really) key
isSuccess = theConnection.authenticateWithPublicKey(username, privateKeyArray, null)
if (!isSuccess )
throw new RuntimeException( 'ERROR: Authentication error' )

// Open seession and execute command
theSession = theConnection.openSession()
theSession.execCommand(command)

// Get stdout stream and convert to string
stdoutStream = new StreamGobbler(theSession.getStdout())
stderrStream = new StreamGobbler(theSession.getStderr())

stdout = IOUtils.toString(stdoutStream, 'UTF-8')
stderr = IOUtils.toString(stderrStream, 'UTF-8')

// Close streams
stdoutStream.close()
stderrStream.close()

// Close the session and connection
theSession.close()
theConnection.close()   

if (stderr){
throw new RuntimeException( stderr )
}
// Return the string
return stdout
}
// catch any exceptions
catch (e) {
throw new RuntimeException( e )
}
  • Like 1
Link to comment
Share on other sites

  • 1 year later...
  • Newbies

Trying to register this on ScriptMaster and getting the following errors - any ideas?

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 5: unable to resolve class com.trilead.ssh2.Session
 @ line 5, column 1.
   import com.trilead.ssh2.Session
   ^

Script1.groovy: 1: unable to resolve class org.apache.commons.io.IOUtils
 @ line 1, column 1.
   import org.apache.commons.io.IOUtils
   ^

Script1.groovy: 6: unable to resolve class com.trilead.ssh2.StreamGobbler
 @ line 6, column 1.
   import com.trilead.ssh2.StreamGobbler
   ^

Script1.groovy: 4: unable to resolve class com.trilead.ssh2.Connection
 @ line 4, column 1.
   import com.trilead.ssh2.Connection
   ^

4 errors

Link to comment
Share on other sites

This is because the jar files needed are not loaded.

If you are using the 360works sample file you need to download the gannymead jar file and apache commons, add them to the file, and then make sure they are ticked on the jars tab..

If in you own solution then you need to add the files to your file and load them before you can run this function.

Link to comment
Share on other sites

  • 1 year later...
  • Newbies

Hi folks,

I've been searching this for a while - is it generally not possible to execute ssh/scp commands from "runShellScript"?

Working on MacOS X in this case.

In my tests, the module basically works, delivering correct return values on "pwd", "whoami", "date -u", but on certain commands it simply fails. Of course, these are the desperately needed ones...

Also, I cannot execute a shell-script which is marked executable by using it's path as input to the module. To me, it is not transparent what kind of calls will work and which ones will not.

Please fill my gaps...

Link to comment
Share on other sites

This topic is 2633 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.