Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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 );
}
  • Author

*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;
  • 11 months later...
  • Author

BTW: Requires commons.io and commons.lang JARs as well...

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 )
}
  • Author

Nice one! I'll try it out.

  • 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

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.

  • 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...

Create an account or sign in to comment

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.