We have reset all users FileMaker related profile fields. Please take the opportunity to update your information,  this will provide background to members whom read your posts. Click here.

Jump to content

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

Share this post


Link to post
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;

Share this post


Link to post
Share on other sites
john renfrew    29

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
john renfrew    29

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.

Share this post


Link to post
Share on other sites
theKoog    0

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

Share this post


Link to post
Share on other sites

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


  • Similar Content

    • By Raúl Basurco M.
      ScriptMaster 5.01 is not working as usual.
      Just downloaded and updated and my solutions started to report errors on many dialog boxes.

    • By jaylkjr
      I have several customers that are still running Wondows Version 9, which is a version out of date. On attempting to load a 64 bit, ScriptMaster generated Plug-In, I am seeing a dialog that ScriptMaster can't run without Java 64 bit. On installing Java, there is no change and the same issue persists. This goes as far as literally crashing FileMaker. 
      Has anyone experienced this? Although it's really a Java issue, and the OS is out of date, I have two important customers running Windows 9.
    • By laurentades
      I am working on a project which requires me to make SOAP calls from FileMaker.
      I have successfully done that in the past thanks to ScriptMaster ability to handle that. It works beautifully.
      The new project now requires me to include a client certificate - issues by the SOAP API provider - in each requests. The documentation on their side is fairly scarce ("Install the certificate on your host" !).
      I have no idea how to handle that but something tells me that there is no reason why it would not be possible to "include" the certificate in the Groovy bit of the plugin...
      Any idea anyone ?
      Thanks
    • By john renfrew
      Is it possible to get access to the fmpro object inside an inner class in code???
      Example
       
      //at head of groovy code class myClass extends HelperClass { def xx = fmpro.evaluate('Get(PersistentID)') def ... def ... }  
      Currently fails with groovy.lang.MissingPropertyException: No such property: fmpro for class: myClass
    • By truelifeajf
      I'm at a loss as to why I'm getting "ERROR" when:
      I have an image (JPG) 3648 x 5472 pixels at this location:
      $crop_location = /Volumes/Macintosh HD/Applications/Wolf Spring/Wolf Spring Lightroom/Originals/Anderson_Austin_RB6649233682XA-O9330018750AV-crop.JPG
      The file location is definitely correct.
      This function:
      CropImage( $crop_location ; 0 ; 300 ; 3648 ; 5172 )
      Should simply crop 300 pixels off the top, right?
      I get "ERROR"
      Any ideas?
×

Important Information

By using this site, you agree to our Guidelines.