public class SCPClient
extends java.lang.Object
SCPClient
implements SCP (SSH secure copy).
Connections are established when the connect()
is invoked.
Before it is called, the remote host must be defined, and
host verification and client authentication should be
configured.
Host Validation is the process of verifying that a host that is
being connected to is the host that was requested. Host validation is
done through an instance of SSHFTPValidator
or a subclass
thereof. By default, SCPClient
, has an instance of
this class that is available by means of the getValidator()
method. Unless specialized validation functionality is required,
initialization of validation functionality may be done simply by invoking
methods on this object.
There are four approaches to setting up host validation:
ftpClient.getValidator().setHostValidationEnabled(false);
jackspc ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIE...If there are more than one host using a given key the host-name may be replaced by a comma-separated list of host-names. Wildcards may not be used. A host-file is loaded as follows:
ftpClient.getValidator().loadKnownHosts("~/.ssh/known_hosts");
ftpClient.getValidator().addKnownHost("host1pk.pub"); ftpClient.getValidator().addKnownHost("host2pk.pub");Public keys should be in OpenSSH or SECSH format.
Client Authentication is the method used by the server to authenticate
clients. Different servers will enforce different policies for authenticating
clients. SCPClient
currently offers two authentication methods:
setAuthentication
as follows:
ftpClient.setAuthentication("jack", "my_ftp_password");
ftpClient.setAuthentication("myprivatekey", "jack", "my_keyfile_password");The user-name is the name of the user on the SFTP server, but the passphrase is the password for the key-file (if there is a password).
Modifier and Type | Field and Description |
---|---|
protected com.enterprisedt.net.j2ssh.authentication.SshAuthenticationClient |
authenticator
J2SSH authenticator.
|
protected com.enterprisedt.net.j2ssh.configuration.SshConnectionProperties |
connProps
J2SSH SshConnectionProperties instance.
|
static int |
DEFAULT_PORT
Default SSH port.
|
protected int |
deleteCount
Count of deleted files
|
protected int |
downloadCount
Count of downloaded files
|
protected java.lang.String |
id
Id of instance
|
protected com.enterprisedt.util.license.LicensePropertiesBase |
license
The license details
|
protected ProxySettings |
proxySettings
The proxy settings
|
protected com.enterprisedt.net.j2ssh.authentication.SshAuthenticationClient |
retryAuthenticator
J2SSH authenticator.
|
protected com.enterprisedt.net.j2ssh.SshClient |
ssh
J2SSH SshClient instance.
|
protected int |
uploadCount
Count of uploaded files
|
protected SSHFTPValidator |
validator
Encapsulation and extension of J2SSH
AbstractKnownHostsKeyVerification . |
Constructor and Description |
---|
SCPClient()
Constructs an SCP client.
|
Modifier and Type | Method and Description |
---|---|
protected void |
checkConnection(boolean shouldBeConnected)
Checks if the client has connected to the server and throws an exception
if it hasn't.
|
void |
connect()
Connects to the server at the address and port number defined in the
constructor.
|
boolean |
connected()
Is the client currently connected?
|
protected void |
connectSSH()
Connects to the server at the address and port number defined in the
constructor.
|
void |
disableAllAlgorithms()
Disables all algorithms.
|
void |
disableAllAlgorithms(int algorithmType)
Disables all algorithms of a particular type.
|
java.lang.String |
executeCommand(java.lang.String command)
Execute a command on the remote server.
|
void |
get(java.lang.String localPath,
java.lang.String remoteFile)
Download a file
|
com.enterprisedt.net.j2ssh.configuration.SshConnectionProperties |
getConnectionProperties()
This method is not for public use.
|
int |
getDeleteCount()
Get the number of files deleted since the count was
reset
|
int |
getDownloadCount()
Get the number of files downloaded since the count was
reset
|
SSHFTPAlgorithm[] |
getEnabledAlgorithms()
Returns an array containing all enabled algorithms (of all types).
|
SSHFTPAlgorithm[] |
getEnabledAlgorithms(int algorithmType)
Returns an array containing all enabled algorithms of a particular type.
|
SSHFTPPublicKey |
getHostPublicKey()
Provides access to the current server's public key.
|
static SSHFTPPublicKey |
getHostPublicKey(java.lang.String remoteHost)
Helper method that may be used to obtain a server's public key without
instantiating an
SCPClient . |
static SSHFTPPublicKey |
getHostPublicKey(java.lang.String remoteHost,
int remotePort)
Helper method that may be used to obtain a server's public key without
instantiating an
SCPClient . |
java.lang.String |
getId()
Get the identifying string for this instance
|
int |
getNetworkBufferSize()
Get the size of the network buffers (SO_SNDBUF
and SO_RCVBUF).
|
ProxySettings |
getProxySettings()
Get the proxy settings instance.
|
java.lang.String |
getRemoteHost() |
int |
getRemotePort()
Returns the SSH port of the remote host.
|
int |
getTimeout() |
int |
getTransportProvider()
Deprecated.
|
int |
getUploadCount()
Get the number of files uploaded since the count was
reset
|
SSHFTPValidator |
getValidator()
Returns a reference to the current host validator.
|
boolean |
isAlgorithmEnabled(SSHFTPAlgorithm algorithm)
Returns
true if the given algorithm is enabled. |
boolean |
isRekeyEnabled()
Is re-keying after 1 GB enabled? If enabled (the default),
after 1 GB of transfer a new set of cryptographic keys are
established.
|
java.lang.String |
put(java.lang.String localPath,
java.lang.String remoteFile)
Upload a file.
|
void |
quit() |
void |
quitImmediately() |
void |
resetDeleteCount()
Reset the count of deleted files to zero.
|
void |
resetDownloadCount()
Reset the count of downloaded files to zero.
|
void |
resetUploadCount()
Reset the count of uploaded files to zero.
|
void |
setAlgorithmEnabled(SSHFTPAlgorithm algorithm,
boolean enable)
Enable/disable the given algorithm.
|
void |
setAuthentication(java.io.InputStream inStr,
java.lang.String userName,
java.lang.String keyFilePassphrase)
Set up the SFTP client to authenticate using a private/public key-pair.
|
void |
setAuthentication(java.lang.String user,
SSHAuthPrompt[] prompts)
Set up keyboard-interactive (KBI) authentication by supplying an array of responses
to possible prompts by the SSH server
|
void |
setAuthentication(java.lang.String userName,
java.lang.String password)
Set up user-name/password authentication.
|
void |
setAuthentication(java.lang.String userName,
java.lang.String password,
PasswordChanger pwdChanger)
Set up user-name/password authentication.
|
void |
setAuthentication(java.lang.String keyFileName,
java.lang.String userName,
java.lang.String keyFilePassphrase)
Set up the SFTP client to authenticate using a private/public key-pair.
|
void |
setAuthentication(java.lang.String keyFileName,
java.lang.String userName,
java.lang.String keyFilePassphrase,
java.lang.String userPassword)
Set up the SFTP client to authenticate using a private/public key-pair, followed
by password authentication.
|
void |
setAuthentication(java.lang.String keyFileName,
java.lang.String userName,
java.lang.String keyFilePassphrase,
java.lang.String userPassword,
boolean publicKeyFirst)
Set up the SFTP client to authenticate using a private/public key-pair, followed
by password authentication, or in the reverse order.
|
void |
setAuthenticationChangePassword(java.lang.String userName,
java.lang.String oldPassword,
java.lang.String newPassword)
Set up user-name/password authentication, and change the user's password.
|
void |
setConnectionProperties(com.enterprisedt.net.j2ssh.configuration.SshConnectionProperties props)
This method is not for public use.
|
void |
setId(java.lang.String id)
Set the identifying string for this instance
|
void |
setNetworkBufferSize(int networkBufferSize)
Set the size of the network buffers (SO_SNDBUF
and SO_RCVBUF).
|
void |
setProxyParams(ProxyParameters params)
Deprecated.
|
void |
setRekeyEnabled(boolean enableRekey)
Set flag to disable or enable re-keying after 1 GB
|
void |
setRekeyTransferLimit(long kilobytes)
Set a different limit to the 1 GB default
|
void |
setRemoteHost(java.lang.String remoteHost) |
void |
setRemotePort(int port)
Set the SSH port of the remote host.
|
void |
setTimeout(int millis) |
void |
setTransportProvider(int provider)
Deprecated.
|
void |
setValidator(SSHFTPValidator validator)
Sets the host validator.
|
java.lang.String |
system()
Get a string representing the remote system.
|
java.lang.String |
toString()
String representation
|
protected void |
validateAlgorithms()
Check to see if at least one of each type of algorithm is enabled.
|
public static final int DEFAULT_PORT
protected com.enterprisedt.net.j2ssh.SshClient ssh
protected com.enterprisedt.net.j2ssh.configuration.SshConnectionProperties connProps
protected SSHFTPValidator validator
AbstractKnownHostsKeyVerification
.protected com.enterprisedt.net.j2ssh.authentication.SshAuthenticationClient authenticator
protected com.enterprisedt.net.j2ssh.authentication.SshAuthenticationClient retryAuthenticator
protected java.lang.String id
protected int downloadCount
protected int uploadCount
protected int deleteCount
protected com.enterprisedt.util.license.LicensePropertiesBase license
protected ProxySettings proxySettings
public SCPClient()
public void setConnectionProperties(com.enterprisedt.net.j2ssh.configuration.SshConnectionProperties props)
public com.enterprisedt.net.j2ssh.configuration.SshConnectionProperties getConnectionProperties()
public java.lang.String getId()
public void setId(java.lang.String id)
id
- identifying stringpublic java.lang.String system() throws FTPException, java.io.IOException
FTPException
java.io.IOException
public int getDownloadCount()
public void resetDownloadCount()
public int getUploadCount()
public void resetUploadCount()
public int getDeleteCount()
public void resetDeleteCount()
public SSHFTPValidator getValidator()
SSHFTPValidator
. If
customized host validation is required
then setValidator()
should be used to set up
the client to use a subclass
of SSHFTPValidator
.public void setValidator(SSHFTPValidator validator) throws FTPException
SSHFTPValidator
. If customized host validation is required
then this method should be used to set up the client to use a subclass
of SSHFTPValidator
.validator
- The host validator to set.FTPException
public static SSHFTPPublicKey getHostPublicKey(java.lang.String remoteHost) throws FTPException
SCPClient
. This method returns an
SSHFTPPublicKey
object whose SSHFTPPublicKey.write(OutputStream, int)
method may be used to write the public key to a file. The method will
return null
if a public key could not be retrieved. Logging may
be used to discover any connection problems. If more advanced settings are
required then an instance of SCPClient
should be used.remoteHost
- Address of the server whose public key is to be fetched.SSHFTPPublicKey
representingFTPException
public static SSHFTPPublicKey getHostPublicKey(java.lang.String remoteHost, int remotePort) throws FTPException
SCPClient
. This method returns an
SSHFTPPublicKey
object SSHFTPPublicKey.write(OutputStream,int)
method may be used to write the public key to a file. The method will
return null
if a public key could not be retrieved. Logging may
be used to discover any connection problems. If more advanced settings are
required then an instance of SCPClient
should be used.remoteHost
- Address of the server whose public key is to be fetched.remotePort
- Port of the server whose public key is to be fetched.SSHFTPPublicKey
representingFTPException
public SSHFTPPublicKey getHostPublicKey()
null
before the first connection attempt is made.
This method is particularly useful if the server's public key is not
available by other means. In such cases a connection attempt should be made
without adding any known hosts. This attempt will fail, but getHostPublicKey()
may be used afterwards to obtain the public key. The
SSHFTPPublicKey.write(OutputStream,int)
method may be used to write it
to a file for use in subsequent connections.
Note that null may be returned after a connection attempt is made if the attempt failed before public keys were exchanged between the client and the server.
public void setAuthentication(java.lang.String userName, java.lang.String password) throws FTPException
connect
method is invoked). This method first attempts to
use password authentication, and if that fails, falls back to keyboard-interactive
authentication via passworduserName
- User-name as set up on the SFTP server.password
- Password as set up on the SFTP server.FTPException
- Thrown if an SFTP-related error occurs.public void setAuthentication(java.lang.String userName, java.lang.String password, PasswordChanger pwdChanger) throws FTPException
connect
method is invoked). This method first attempts to
use password authentication, and if that fails, falls back to keyboard-interactive
authentication via passworduserName
- User-name as set up on the SFTP server.password
- Password as set up on the SFTP server.pwdChanger
- instance that gets new passwordFTPException
- Thrown if an SFTP-related error occurs.public void setAuthenticationChangePassword(java.lang.String userName, java.lang.String oldPassword, java.lang.String newPassword) throws FTPException
connect
method is invoked).userName
- User-name as set up on the SFTP server.oldPassword
- Password as set up on the SFTP server.newPassword
- new password to setFTPException
- Thrown if an SFTP-related error occurs.public void setAuthentication(java.lang.String user, SSHAuthPrompt[] prompts) throws FTPException
user
- user to login asprompts
- prompt responses supplied to pass the server authentication promptsFTPException
public void setAuthentication(java.lang.String keyFileName, java.lang.String userName, java.lang.String keyFilePassphrase) throws java.io.IOException, FTPException
connect
method is invoked).
The key-file can be in either OpenSSH format (generated by default by ssh-keygen) or SECSH format.
The file may or may not be password protected depending on how it was generated.
keyFileName
- File containing the private key.userName
- User-name as set up on the server.keyFilePassphrase
- (Optional) Key-file passphrase.java.io.IOException
- Thrown if the key-file could not be read.SSHFTPException
- Thrown if an SFTP-related error occurred.FTPException
public void setAuthentication(java.io.InputStream inStr, java.lang.String userName, java.lang.String keyFilePassphrase) throws java.io.IOException, FTPException
connect
method is invoked).
The key-file can be in either OpenSSH format (generated by default by ssh-keygen) or SECSH format.
The file may or may not be password protected depending on how it was generated.
inStr
- InputStream
containing the private key.userName
- User-name as set up on the server.keyFilePassphrase
- (Optional) Key-file passphrase.java.io.IOException
- Thrown if the key-file could not be read.SSHFTPException
- Thrown if an SFTP-related error occurred.FTPException
public void setAuthentication(java.lang.String keyFileName, java.lang.String userName, java.lang.String keyFilePassphrase, java.lang.String userPassword) throws java.io.IOException, FTPException
connect
method is invoked).
The key-file can be in either OpenSSH format (generated by default by ssh-keygen) or SECSH format.
The file may or may not be password protected depending on how it was generated.
keyFileName
- File containing the private key.userName
- User-name as set up on the server.keyFilePassphrase
- (Optional) Key-file passphrase.userPassword
- User's passwordjava.io.IOException
- Thrown if the key-file could not be read.SSHFTPException
- Thrown if an SFTP-related error occurred.FTPException
public void setAuthentication(java.lang.String keyFileName, java.lang.String userName, java.lang.String keyFilePassphrase, java.lang.String userPassword, boolean publicKeyFirst) throws java.io.IOException, FTPException
connect
method is invoked).
The key-file can be in either OpenSSH format (generated by default by ssh-keygen) or SECSH format.
The file may or may not be password protected depending on how it was generated.
keyFileName
- File containing the private key.userName
- User-name as set up on the server.keyFilePassphrase
- (Optional) Key-file passphrase.userPassword
- User's passwordpublicKeyFirst
- if true, public key then private key, if false the reversejava.io.IOException
- Thrown if the key-file could not be read.SSHFTPException
- Thrown if an SFTP-related error occurred.FTPException
protected void validateAlgorithms() throws SSHFTPException
SSHFTPException
- Thrown if one algorithm-type has no enabled algorithms.public SSHFTPAlgorithm[] getEnabledAlgorithms()
public SSHFTPAlgorithm[] getEnabledAlgorithms(int algorithmType) throws SSHFTPException
algorithmType
- Type of algorithms to return (See SSHFTPAlgorithm).SSHFTPException
- Thrown if the given algorithm-type is invalid.public void disableAllAlgorithms(int algorithmType) throws SSHFTPException
algorithmType
- Type of algorithms to return (See SSHFTPAlgorithm).SSHFTPException
public void disableAllAlgorithms()
public void setAlgorithmEnabled(SSHFTPAlgorithm algorithm, boolean enable)
algorithm
- Algorithm to enable/disable.enable
- Flag indicating whether the algorithm should be enabled or disabled.public boolean isAlgorithmEnabled(SSHFTPAlgorithm algorithm)
true
if the given algorithm is enabled.algorithm
- Algorithm to return status of.public java.lang.String getRemoteHost()
public void setRemoteHost(java.lang.String remoteHost) throws FTPException
FTPException
public int getRemotePort()
public void setRemotePort(int port) throws FTPException
port
- The port to set.FTPException
- Thrown if the client is already connected to the server.public ProxySettings getProxySettings()
public void setProxyParams(ProxyParameters params)
getProxySettings()
params
- parameter objectpublic void setTransportProvider(int provider)
getProxySettings()
provider
- provider integerpublic int getTransportProvider()
getProxySettings()
public int getTimeout()
public void setTimeout(int millis) throws FTPException
FTPException
public int getNetworkBufferSize()
public void setNetworkBufferSize(int networkBufferSize)
networkBufferSize
- new buffer size to setpublic boolean isRekeyEnabled()
public void setRekeyEnabled(boolean enableRekey)
enableRekey
- true to enable, false to disablepublic void setRekeyTransferLimit(long kilobytes) throws java.io.IOException
kilobytes
- new limit in kBjava.io.IOException
public void connect() throws java.io.IOException, FTPException
java.io.IOException
- Thrown if there is a TCP/IP-related error.FTPException
- Thrown if client is already connected or if there is
a configuration error, or if a connection could not be established.protected void connectSSH() throws java.io.IOException, FTPException
java.io.IOException
- Thrown if there is a TCP/IP-related error.FTPException
- Thrown if client is already connected or if there is
a configuration error, or if a connection could not be established.public java.lang.String executeCommand(java.lang.String command) throws FTPException, java.io.IOException
command
- command to executeFTPException
java.io.IOException
protected void checkConnection(boolean shouldBeConnected) throws FTPException
FTPException
- Thrown if the client has not connected to the server.public boolean connected()
public java.lang.String put(java.lang.String localPath, java.lang.String remoteFile) throws java.io.IOException, FTPException
localPath
- path of local file to be uploadedremoteFile
- path of remote file to upload tojava.io.IOException
FTPException
public void get(java.lang.String localPath, java.lang.String remoteFile) throws java.io.IOException, FTPException
localPath
- path of local file to download toremoteFile
- path of remote filejava.io.IOException
FTPException
public void quit() throws java.io.IOException, FTPException
java.io.IOException
FTPException
public void quitImmediately() throws java.io.IOException, FTPException
java.io.IOException
FTPException
public java.lang.String toString()
toString
in class java.lang.Object
Copyright © 2001-2014 Enterprise Distributed Technologies Ltd. All Rights Reserved.