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 |
connectByPassword(String userName,
String password)
Connects to the remote host using a username and a password for
authentication
|
void |
connectByPassword(String userName,
String password,
int timeOut)
Connects to the remote host using a username and a password for
authentication
|
void |
createDirectory(String remoteDirectoryPath)
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()
Check if the client has a active connection.
|
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 connectByPassword(String userName, String password) throws SSHClientException
userName - the usernamepassword - the passwordSSHClientException - error while connectingpublic void connectByPassword(String userName, String password, int timeOut) throws SSHClientException
userName - the usernamepassword - the passwordtimeOut - time outSSHClientException - error while connectingpublic void createTunnel(String tunnelHost, int tunnelPort, String tunnelUserName, String tunnelPrivateKeyFilePath, int localPort) throws SSHClientException
tunnelHost - tunnel hosttunnelPort - tunnel porttunnelUserName - tunnel user nametunnelPrivateKeyFilePath - tunnel private key pathlocalPort - tunnel local portSSHClientException - In case of error creating the tunnelpublic void createTunnel(String tunnelHost, int tunnelPort, String tunnelUserName, String tunnelPrivateKeyFilePath, int localPort, int localRange) throws SSHClientException
tunnelHost - tunnel hosttunnelPort - tunnel porttunnelUserName - tunnel user nametunnelPrivateKeyFilePath - tunnel private key pathlocalPort - tunnel local portlocalRange - tunnel local port rangeSSHClientException - In case of error creating the tunnelpublic void disconnect()
public boolean isConnected()
public CommandResult execute(String command) throws IOException
command - the commandIOException - Error while reading the command outputpublic void remove(String remotePath) throws IOException
remotePath - the remote absolute pathIOException - Error while removing file or directorypublic void createDirectory(String remoteDirectoryPath) throws IOException
remoteDirectoryPath - the remote directory absolute pathIOException - Error while creating the directorypublic boolean stat(String remotePath) throws IOException
remotePath - the remote absolute pathIOException - Error while checking the existence of the file or
directorypublic void download(String localFilePath, String remoteFilePath) throws IOException
localFilePath - the local absolute pathremoteFilePath - the remote absolute pathIOException - Error downloading the file or directorypublic void upload(String localFilePath, String remoteFilePath) throws IOException
localFilePath - the local absolute pathremoteFilePath - the remote absolute pathIOException - Error uploading the file or directorypublic Map<String,Long> listFiles(String remotePath) throws IOException
remotePath - the remote pathIOException - Error while reading the remote filepublic Map<String,Long> listFilesAndDirectories(String remotePath) throws IOException
remotePath - the remote pathIOException - Error while reading the remote filepublic boolean isTunneled()
Copyright © 2022. All rights reserved.