Package fr.ifremer.isisfish.util.ssh
Class SSHUtils
java.lang.Object
fr.ifremer.isisfish.util.ssh.SSHUtils
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
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
Progress monitor (sftp). -
Field Summary
Modifier and TypeFieldDescriptionprotected static final int
protected static final int
protected static final byte
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected 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 command on remote server.static int
Exec command on remote server.protected static void
fetchFile
(File localFile, long filesize, OutputStream out, InputStream in, ProgressMonitor monitor) protected static void
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
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
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
Perform sftp file transfert.static void
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
Reads the response, throws a BuildException if the response indicates an error.
-
Field Details
-
LINE_FEED
protected static final byte LINE_FEED- See Also:
-
BUFFER_SIZE
protected static final int BUFFER_SIZE- See Also:
-
HUNDRED_KILOBYTES
protected static final int HUNDRED_KILOBYTES- See Also:
-
-
Constructor Details
-
SSHUtils
protected SSHUtils()Utility class
-
-
Method Details
-
sendAck
Send an ack.- Parameters:
out
- the output stream to use- Throws:
IOException
- on error
-
waitForAck
Reads the response, throws a BuildException if the response indicates an error.- Parameters:
in
- the input stream to use- Throws:
IOException
- on I/O errorSSHException
-
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 transmittedtotalLength
- the total transmission sizepercentTransmitted
- the current percent transmitted- Returns:
- the percent that the file is of the total
-
exec
Exec command on remote server.- Parameters:
session
- opened valid sessioncommand
- 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 sessioncommand
- command to execout
- 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 sessionremoteFileName
- remote file name to downloadlocalFile
- 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 sessionremoteFileName
- remote file name to downloadlocalFile
- local file name to download intomonitor
- 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 sessionlocalFile
- file to uploadremoteFilePath
- 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
-
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
-