I forgot to include the Groovy script itself that does all the heavy-lifting
in my previous mail - see Groovy.SshHelperClass below. Also, there's a much
better way of calling this groovy "sshHelper" class out of a single groovy
invocation in a 2.0 document. Groovy directly outputs wikitext, the renderer
takes care of the rest.
==== Use Groovy Script run server processes and display result ====
See [[SshHelperClass?viewer=code]], [[
http://blog.asyd.net/2008/12/xwiki-cest-decidemment-magique/]]
{{groovy}}
println "==== Call parseGroovyFromPage('Groovy.SshHelperClass') ===="
def sshHelper = xwiki.parseGroovyFromPage("Groovy.SshHelperClass")
sshHelper.openSession("127.0.0.1", "22",
"tomcat-ssh-slave",
"/usr/share/tomcat6/.ssh/id_dsa", "")
println "==== Output from 'uname -a' ===="
println "##" + sshHelper.runCommand("uname -a") + "##"
println "==== Output from 'free' ===="
println "##" + sshHelper.runCommand("free") + "##"
println "==== Output from 'ps -l U tomcat-ssh-slave U tomcat U apache'
===="
println "##" + sshHelper.runCommand("ps -l h U tomcat-ssh-slave U tomcat U
apache") + "##"
println "==== Output from 'df -H' ===="
println "##" + sshHelper.runCommand("df -H") + "##"
println "==== Output from 'top -b -n 1' ===="
println "##" + sshHelper.runCommand("top -b -n 1") + "##"
println "==== Close the connection and exit tomcat-ssh-slave shell ===="
sshHelper.close()
{{/groovy}}
However, the Groovy.SshHelperClass script must be saved as a 1.0 document in
order to call it at the top level via:
"xwiki.parseGroovyFromPage("Groovy.SshHelperClass")". Is there a 2.0
syntax
that works with parseGroovyFromPage()?
Groovy.SshHelperClass in xwiki/1.0 format (with special comment hacks per
http://platform.xwiki.org/xwiki/bin/view/DevGuide/GroovyClassHelloWorldTuto…):
...................................
/* Groovy Class for [SshHelperTest], orig [
http://blog.asyd.net/wp-content/uploads/2008/12/sshhelper.groovy] #* */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.trilead.ssh2.Connection;
import com.trilead.ssh2.Session;
import com.trilead.ssh2.StreamGobbler;
/**
*
* @author asyd, modified by NielsMayer
*/
class SshHelper {
def connection
def session
void openSession(hostname, port, username, identityfile, password) {
def identityIOFile = new File(identityfile)
this.connection = new Connection(hostname, Integer.parseInt(port))
this.connection.connect()
if (!connection.authenticateWithPublicKey(username, identityIOFile,
password)) {
println "Authentication failed!"
}
}
String runCommand(command) {
this.session = connection.openSession()
this.session.execCommand(command)
def stdoutStream = new StreamGobbler(this.session.getStdout())
def stdoutReader = new BufferedReader(new
InputStreamReader(stdoutStream))
def lineout = ""
def stdoutOutput = ""
while ((lineout = stdoutReader.readLine()) != null) {
stdoutOutput += (lineout + "\n")
}
this.session.close()
this.session = null
return stdoutOutput
}
void close() {
if (this.session != null) {
this.session.close()
}
this.connection.close()
}
/* *# */
...................................
-- Niels
http://nielsmayer.com