Class SSHUtils

java.lang.Object
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$ Last update : $Date$ By : $Author$
Author:
chatellier
  • Field Details

  • Constructor Details

    • SSHUtils

      protected SSHUtils()
      Utility class
  • Method Details

    • 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