Jonathan Perel Posted January 27, 2014 Posted January 27, 2014 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 ); } 1
Jonathan Perel Posted January 31, 2014 Author Posted January 31, 2014 *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;
Jonathan Perel Posted January 30, 2015 Author Posted January 30, 2015 BTW: Requires commons.io and commons.lang JARs as well...
john renfrew Posted February 1, 2015 Posted February 1, 2015 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 ) } 1
Newbies Garrett Debski Posted February 2, 2016 Newbies Posted February 2, 2016 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
john renfrew Posted February 2, 2016 Posted February 2, 2016 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.
Newbies theKoog Posted February 9, 2017 Newbies Posted February 9, 2017 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...
MonkeybreadSoftware Posted February 9, 2017 Posted February 9, 2017 Well, if someone doesn't like to use Java, the MBS Plugin can offer SSH directly: http://www.mbsplugins.eu/component_SSH.shtml also SFTP via CURL functions. If you like to try, get the plugin with examples and try them.
Recommended Posts
This topic is 3113 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 accountSign in
Already have an account? Sign in here.
Sign In Now