fr.ifremer.isisfish.util.ssh
Class SSHUtils

java.lang.Object
  extended by fr.ifremer.isisfish.util.ssh.SSHUtils

public class SSHUtils
extends Object

SSH utils class. All this code has be taken from ant optionnal ssh task. Use full for: - exec command - scpTo command - scpFrom command

Version:
$Revision: 3124 $ Last update : $Date: 2010-11-29 19:14:09 +0100 (lun., 29 nov. 2010) $ By : $Author: chatellier $
Author:
chatellier

Nested Class Summary
protected static class SSHUtils.UtilsProgressMonitor
          Progress monitor (sftp).
 
Field Summary
protected static int BUFFER_SIZE
           
protected static int HUNDRED_KILOBYTES
           
protected static byte LINE_FEED
           
 
Constructor Summary
protected SSHUtils()
          Utility class
 
Method Summary
protected static void doSingleSftpransfer(com.jcraft.jsch.Session session, File localFile, String remoteFilePath)
           
protected static void doSingleTransfer(com.jcraft.jsch.Session session, File localFile, String remoteFilePath)
           
static int exec(com.jcraft.jsch.Session session, String command)
          Exec command on remote server.
static int exec(com.jcraft.jsch.Session session, String command, Writer out)
          Exec command on remote server.
protected static void fetchFile(File localFile, long filesize, OutputStream out, InputStream in, ProgressMonitor monitor)
           
protected static void getDir(com.jcraft.jsch.ChannelSftp channel, String remoteFile, File localFile)
           
protected static void getFile(com.jcraft.jsch.ChannelSftp channel, com.jcraft.jsch.ChannelSftp.LsEntry le, File localFile)
           
protected static com.jcraft.jsch.ChannelSftp openSftpChannel(com.jcraft.jsch.Session session)
          Open sftp channel.
protected static File parseAndCreateDirectory(String serverResponse, File localFile)
           
protected static void parseAndFetchFile(String serverResponse, File localFile, OutputStream out, InputStream in, ProgressMonitor monitor)
           
static void scpFrom(com.jcraft.jsch.Session session, String remoteFileName, File localFile)
          Download a local file from remote server and check md5sum.
static void scpFrom(com.jcraft.jsch.Session session, String remoteFileName, File localFile, ProgressMonitor monitor)
          Download a local file from remote server and check md5sum.
static void scpTo(com.jcraft.jsch.Session session, File localFile, String remoteFilePath)
          Upload file on remote server.
protected static void sendAck(OutputStream out)
          Send an ack.
protected static void sendFileToRemote(com.jcraft.jsch.ChannelSftp channel, File localFile, String remotePath)
           
protected static void sendFileToRemote(File localFile, InputStream in, OutputStream out)
           
static void sftpFrom(com.jcraft.jsch.Session session, File localFile, String remotePath)
          Perform sftp file transfert.
static void sftpTo(com.jcraft.jsch.Session session, File localFile, String remoteFilePath)
          Perform sftp file transfert.
protected static void startRemoteCpProtocol(InputStream in, OutputStream out, File localFile, ProgressMonitor monitor)
           
protected static int trackProgress(long filesize, long totalLength, int percentTransmitted)
          Track progress every 10% if 100kb < filesize < 1mb.
protected static void waitForAck(InputStream in)
          Reads the response, throws a BuildException if the response indicates an error.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_FEED

protected static final byte LINE_FEED
See Also:
Constant Field Values

BUFFER_SIZE

protected static final int BUFFER_SIZE
See Also:
Constant Field Values

HUNDRED_KILOBYTES

protected static final int HUNDRED_KILOBYTES
See Also:
Constant Field Values
Constructor Detail

SSHUtils

protected SSHUtils()
Utility class

Method Detail

sendAck

protected static void sendAck(OutputStream out)
                       throws IOException
Send an ack.

Parameters:
out - the output stream to use
Throws:
IOException - on error

waitForAck

protected static void waitForAck(InputStream in)
                          throws IOException,
                                 SSHException
Reads the response, throws a BuildException if the response indicates an error.

Parameters:
in - the input stream to use
Throws:
IOException - on I/O error
SSHException

trackProgress

protected static int trackProgress(long filesize,
                                   long totalLength,
                                   int percentTransmitted)
Track progress every 10% if 100kb < filesize < 1mb. For larger files track progress for every percent transmitted.

Parameters:
filesize - the size of the file been transmitted
totalLength - the total transmission size
percentTransmitted - the current percent transmitted
Returns:
the percent that the file is of the total

exec

public static int exec(com.jcraft.jsch.Session session,
                       String command)
                throws SSHException
Exec command on remote server.

Parameters:
session - opened valid session
command - command to exec
Returns:
channel exit status (0 = no problem)
Throws:
SSHException

exec

public static int exec(com.jcraft.jsch.Session session,
                       String command,
                       Writer out)
                throws SSHException
Exec command on remote server.

Parameters:
session - opened valid session
command - command to exec
out - exec output (can be null)
Returns:
channel exit status (0 = no problem)
Throws:
SSHException

scpFrom

public static void scpFrom(com.jcraft.jsch.Session session,
                           String remoteFileName,
                           File localFile)
                    throws SSHException
Download a local file from remote server and check md5sum.

Parameters:
session - opened valid jsch session
remoteFileName - remote file name to download
localFile - local file name to download into
Throws:
SSHException - if transfer fail

scpFrom

public static void scpFrom(com.jcraft.jsch.Session session,
                           String remoteFileName,
                           File localFile,
                           ProgressMonitor monitor)
                    throws SSHException
Download a local file from remote server and check md5sum.

Parameters:
session - opened valid jsch session
remoteFileName - remote file name to download
localFile - local file name to download into
monitor - progress monitor (can be null)
Throws:
SSHException - if transfer fail

scpTo

public static void scpTo(com.jcraft.jsch.Session session,
                         File localFile,
                         String remoteFilePath)
                  throws SSHException
Upload file on remote server.

Parameters:
session - opened valid session
localFile - file to upload
remoteFilePath - remote file path
Throws:
SSHException

startRemoteCpProtocol

protected static void startRemoteCpProtocol(InputStream in,
                                            OutputStream out,
                                            File localFile,
                                            ProgressMonitor monitor)
                                     throws IOException,
                                            SSHException
Throws:
IOException
SSHException

parseAndCreateDirectory

protected static File parseAndCreateDirectory(String serverResponse,
                                              File localFile)

parseAndFetchFile

protected static void parseAndFetchFile(String serverResponse,
                                        File localFile,
                                        OutputStream out,
                                        InputStream in,
                                        ProgressMonitor monitor)
                                 throws IOException,
                                        SSHException
Throws:
IOException
SSHException

fetchFile

protected static void fetchFile(File localFile,
                                long filesize,
                                OutputStream out,
                                InputStream in,
                                ProgressMonitor monitor)
                         throws IOException
Throws:
IOException

doSingleTransfer

protected static void doSingleTransfer(com.jcraft.jsch.Session session,
                                       File localFile,
                                       String remoteFilePath)
                                throws IOException,
                                       com.jcraft.jsch.JSchException,
                                       SSHException
Throws:
IOException
com.jcraft.jsch.JSchException
SSHException

sendFileToRemote

protected static void sendFileToRemote(File localFile,
                                       InputStream in,
                                       OutputStream out)
                                throws IOException,
                                       SSHException
Throws:
IOException
SSHException

openSftpChannel

protected static com.jcraft.jsch.ChannelSftp openSftpChannel(com.jcraft.jsch.Session session)
                                                      throws com.jcraft.jsch.JSchException
Open sftp channel.

Parameters:
session -
Returns:
sftp channel
Throws:
com.jcraft.jsch.JSchException
See Also:
ChannelSftp

sftpFrom

public static void sftpFrom(com.jcraft.jsch.Session session,
                            File localFile,
                            String remotePath)
                     throws SSHException
Perform sftp file transfert.

Parameters:
session -
localFile -
remotePath -
Throws:
SSHException - on i/o errors

getDir

protected static void getDir(com.jcraft.jsch.ChannelSftp channel,
                             String remoteFile,
                             File localFile)
                      throws IOException,
                             com.jcraft.jsch.SftpException
Throws:
IOException
com.jcraft.jsch.SftpException

getFile

protected static void getFile(com.jcraft.jsch.ChannelSftp channel,
                              com.jcraft.jsch.ChannelSftp.LsEntry le,
                              File localFile)
                       throws IOException,
                              com.jcraft.jsch.SftpException
Throws:
IOException
com.jcraft.jsch.SftpException

sftpTo

public static void sftpTo(com.jcraft.jsch.Session session,
                          File localFile,
                          String remoteFilePath)
                   throws SSHException
Perform sftp file transfert.

Parameters:
session -
localFile -
remoteFilePath -
Throws:
SSHException - on i/o errors

doSingleSftpransfer

protected static void doSingleSftpransfer(com.jcraft.jsch.Session session,
                                          File localFile,
                                          String remoteFilePath)
                                   throws IOException,
                                          com.jcraft.jsch.JSchException
Throws:
IOException
com.jcraft.jsch.JSchException

sendFileToRemote

protected static void sendFileToRemote(com.jcraft.jsch.ChannelSftp channel,
                                       File localFile,
                                       String remotePath)
                                throws IOException,
                                       com.jcraft.jsch.SftpException
Throws:
IOException
com.jcraft.jsch.SftpException


Copyright © 1999-2011 CodeLutin. All Rights Reserved.