(Quick Reference)

2 Quick Start - Reference Documentation

Authors: Michael Rice

Version: 0.2

2 Quick Start

This is a quick and dirty how-to detailing how to quickly begin using the plugin. While there is a lot of configurability offered by this plugin, this is a very basic demonstration of its usage.

Create The Application

Create a project named JSchSsh2Example. You can do this by entering:
grails create-app JSchSsh2Example

Install the jsch-ssh2 Plugin

In @grails-app/conf/BuildConfig.groovy, add:
compile ':jsch-ssh2:0.2'

Configuring

In @grails-app/conf/Config.groovy, You can over ride the following default values:
jschSsh2 {
    user = null
    password = null
    keyFile = null
    keyFilePassword = null
    port = 22
    StrictHostKeyChecking = "yes"
    knownHostsFile = "~/.ssh/known_hosts"
    sshConfigFile = "~/.ssh/config"
    connectionTimeout = 0
    preserveTimeStamps = true
    // Normal File Read + Write for user,
    // Read for group and Everyone
    defaultFilePermission = "0644"
}

Usage

Create a controller:
grails create-controller com.test.Test

In the controller add:

package com.test

import com.jcraft.jsch.JSchException import com.toastcoders.jschssh.RunSshCommand import com.toastcoders.jschssh.ScpFileTo import com.toastcoders.jschssh.ScpFileFrom

class TestController {

def index() {

try { new ScpFileTo().execute() { host = "10.12.254.10" username = "root" password = "password" localFile = "/home/errr/desktop/hello_world.txt" remoteFile = "/tmp/hello_world.txt" strictHostKeyChecking = "yes" } } catch (JSchException e) { log.trace("Oh noes!!", e) render "There was an error placing file on host. ${e.message}<br />" }

render new RunSshCommand().execute() { host = "10.12.254.10" username = "root" password = "password" command = "cat /tmp/hello_world.txt" strictHostKeyChecking = "yes" }

try { new ScpFileFrom().execute() { host = "10.12.254.10" username = "root" password = "password" localFile = "/home/errr/my_cool_new_file.txt" remoteFile = "/tmp/hello_world.txt" } } catch (JSchException je) { log.trace("Failed to copy file from remote host to local host.") } } }

Sometimes in development mode it helps to set strictHostKeyChecking = "no" but it should never be used in production like that.