public class SSHClient extends Object
A SSHClient with operations to upload/download files, execute remote commands and list the contents of a directory.
This client has support to tunneled connections.
Example of use:
SSHClient sshClient = new SSHClient(sshHost, sshPort); sshClient.connect(sshUserName, sshUserPrivKey);With tunnel:
SSHClient sshClient = new SSHClient(sshHost, sshPort); sshClient.createTunnel(...); sshClient.connect(sshUserName, sshUserPrivKey);
To close the connections use the disconnect() method.
| Constructor and Description |
|---|
SSHClient(String host,
int port)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
connect(String userName,
String privateKeyFilePath)
Connects to the remote host using a username and a private key for
authentication
|
void |
connect(String userName,
String privateKeyFilePath,
int timeOut)
Connects to the remote host using a username and a private key for
authentication.
|
void |
createDirectory(String remotDirectoryPath)
Creates a remote directory.
|
void |
createTunnel(String tunnelHost,
int tunnelPort,
String tunnelUserName,
String tunnelPrivateKeyFilePath,
int localPort)
Creates a tunnel.
|
void |
createTunnel(String tunnelHost,
int tunnelPort,
String tunnelUserName,
String tunnelPrivateKeyFilePath,
int localPort,
int localRange)
Creates a tunnel.
|
void |
disconnect()
Disconnect the client and its tunnel.
|
void |
download(String localFilePath,
String remoteFilePath)
Downloads a remote file or directory.
|
CommandResult |
execute(String command)
Executes a command in the remote server.
|
boolean |
isConnected() |
boolean |
isTunneled()
Checks if the client is using a tunnel.
|
Map<String,Long> |
listFiles(String remotePath)
Lists files with theirs modification timestamp from a remote path.
|
Map<String,Long> |
listFilesAndDirectories(String remotePath)
Lists files and directories with theirs modification timestamp from a
remote path.
|
void |
remove(String remotePath)
Removes a remote file or directory.
|
boolean |
stat(String remotePath)
Verifies the existence of a remote file or directory.
|
void |
upload(String localFilePath,
String remoteFilePath)
Uploads a local file or directory.
|
public SSHClient(String host, int port)
host - remote hostport - remote portpublic void connect(String userName, String privateKeyFilePath) throws SSHClientException
userName - the usernameprivateKeyFilePath - private key pathSSHClientException - error while connectingpublic void connect(String userName, String privateKeyFilePath, int timeOut) throws SSHClientException
userName - the usernameprivateKeyFilePath - private key pathtimeOut - time outSSHClientException - error while connectingpublic void createTunnel(String tunnelHost, int tunnelPort, String tunnelUserName, String tunnelPrivateKeyFilePath, int localPort) throws SSHClientException
tunnelHost - tunnelPort - tunnelUserName - tunnelPrivateKeyFilePath - localPort - SSHClientExceptionpublic void createTunnel(String tunnelHost, int tunnelPort, String tunnelUserName, String tunnelPrivateKeyFilePath, int localPort, int localRange) throws SSHClientException
tunnelHost - tunnelPort - tunnelUserName - tunnelPrivateKeyFilePath - localPort - localRange - SSHClientExceptionpublic void disconnect()
public boolean isConnected()
public CommandResult execute(String command) throws IOException
command - the commandIOExceptionpublic void remove(String remotePath) throws IOException
remotePath - the remote absolute pathIOExceptionpublic void createDirectory(String remotDirectoryPath) throws IOException
remotDirectoryPath - the remote directory absolute pathIOExceptionpublic boolean stat(String remotePath) throws IOException
remotePath - the remote absolute pathIOExceptionpublic void download(String localFilePath, String remoteFilePath) throws IOException
localFilePath - the local absolute pathremoteFilePath - the remote absolute pathIOExceptionpublic void upload(String localFilePath, String remoteFilePath) throws IOException
localFilePath - the local absolute pathremoteFilePath - the remote absolute pathIOExceptionpublic Map<String,Long> listFiles(String remotePath) throws IOException
remotePath - the remote pathIOExceptionpublic Map<String,Long> listFilesAndDirectories(String remotePath) throws IOException
remotePath - the remote pathIOExceptionpublic boolean isTunneled()
Copyright © 2016. All rights reserved.