public class SSHClient extends Object implements AutoCloseable
| Modifier and Type | Class and Description |
|---|---|
static class |
SSHClient.ExitStatusException |
| Constructor and Description |
|---|
SSHClient(String host,
int port,
com.cloudbees.plugins.credentials.common.StandardUsernameCredentials credentials) |
SSHClient(String host,
int port,
String username,
hudson.util.Secret passPhrase,
String... privateKeys) |
SSHClient(String host,
int port,
String username,
String password) |
SSHClient(String host,
int port,
com.microsoft.jenkins.azurecommons.remote.UsernameAuth auth)
SSH client to the remote host with given credentials.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
SSHClient |
connect()
Establish a connection with the SSH server.
|
void |
copyFrom(String remotePath,
File destFile)
Copy remote file to the local destination.
|
void |
copyFrom(String remotePath,
OutputStream out)
Copy remote file contents to the
OutputStream. |
void |
copyTo(File sourceFile,
String remotePath)
Copy local file to the remote path.
|
void |
copyTo(InputStream in,
String remotePath)
Copy the contents from the
InputStream to the remote path. |
String |
execRemote(String command)
Execute a command on the remote server and return the command standard output.
|
String |
execRemote(String command,
boolean showCommand,
boolean capture) |
SSHClient |
forwardSSH(String remoteHost,
int remotePort)
Forward another remote SSH port to local through the current client, and create a new client based on the local
port.
|
SSHClient |
forwardSSH(String remoteHost,
int remotePort,
com.microsoft.jenkins.azurecommons.remote.UsernameAuth sshCredentials)
Forward another remote SSH port to local through the current client, and create a new client based on the local
port.
|
com.microsoft.jenkins.azurecommons.remote.UsernameAuth |
getCredentials() |
String |
getHost() |
int |
getPort() |
String |
getUsername() |
protected void |
withChannelSftp(com.microsoft.jenkins.azurecommons.remote.SSHClient.ChannelSftpConsumer consumer) |
SSHClient |
withLogger(PrintStream log)
Set the optional logger stream to print the status messages.
|
public SSHClient(String host, int port, String username, String password) throws com.jcraft.jsch.JSchException
com.jcraft.jsch.JSchExceptionpublic SSHClient(String host, int port, String username, hudson.util.Secret passPhrase, String... privateKeys) throws com.jcraft.jsch.JSchException
com.jcraft.jsch.JSchExceptionpublic SSHClient(String host, int port, com.cloudbees.plugins.credentials.common.StandardUsernameCredentials credentials) throws com.jcraft.jsch.JSchException
com.jcraft.jsch.JSchExceptionpublic SSHClient(String host, int port, com.microsoft.jenkins.azurecommons.remote.UsernameAuth auth) throws com.jcraft.jsch.JSchException
The credentials can be one of the two:
UsernamePrivateKeyAuth with username and SSH private key.UsernamePasswordAuth with username and password.host - the SSH server name or IP address.port - the SSH service port.auth - the SSH authentication credentials.com.jcraft.jsch.JSchException - if the passed in parameters are not valid, e.g., null usernamepublic SSHClient withLogger(PrintStream log)
log - the logger streampublic SSHClient connect() throws com.jcraft.jsch.JSchException
Remember to close() the client after a session is established and it's no longer used. You may use
the try with resource statement block.
try (SSHClient connected = notConnected.connect()) {
// do things with the connected instance
}
This method can be called again if the the current session is closed. Otherwise if called on a connected instance, a JSchException will be thrown.
com.jcraft.jsch.JSchException - if the client is already connected or error occurs during the connection.public void copyTo(File sourceFile, String remotePath) throws com.jcraft.jsch.JSchException
sourceFile - the local file.remotePath - the target remote path, can be either absolute or relative to the user home.com.jcraft.jsch.JSchException - if the underlying SSH session fails.public void copyTo(InputStream in, String remotePath) throws com.jcraft.jsch.JSchException
InputStream to the remote path.in - the InputStream containing source contents.remotePath - the target remote path, can be either absolute or relative to the user home.com.jcraft.jsch.JSchException - if the underlying SSH session fails.public void copyFrom(String remotePath, File destFile) throws com.jcraft.jsch.JSchException
remotePath - the remote file path, can be either absolute or relative to the user home.destFile - the local destination file path.com.jcraft.jsch.JSchException - if the underlying SSH session fails.public void copyFrom(String remotePath, OutputStream out) throws com.jcraft.jsch.JSchException
OutputStream.remotePath - the remote file path, can be either absolute or relative to the user home.out - the OutputStream where the file contents should be written to.com.jcraft.jsch.JSchException - if the underlying SSH session fails.protected void withChannelSftp(com.microsoft.jenkins.azurecommons.remote.SSHClient.ChannelSftpConsumer consumer)
throws com.jcraft.jsch.JSchException
com.jcraft.jsch.JSchExceptionpublic String execRemote(String command) throws com.jcraft.jsch.JSchException, IOException, SSHClient.ExitStatusException
command - the command to be executed.com.jcraft.jsch.JSchException - if the underlying SSH session fails.IOException - if it fails to read the output from the remote channel.SSHClient.ExitStatusExceptionpublic String execRemote(String command, boolean showCommand, boolean capture) throws com.jcraft.jsch.JSchException, IOException, SSHClient.ExitStatusException
com.jcraft.jsch.JSchExceptionIOExceptionSSHClient.ExitStatusExceptionpublic SSHClient forwardSSH(String remoteHost, int remotePort) throws com.jcraft.jsch.JSchException
This method assumes that the SSH server on A and B accepts the same authentication credentials.
remoteHost - the target host name or IP address, which is accessible from the SSH target of the current
SSHClient.remotePort - the SSH service port on the target host.com.jcraft.jsch.JSchException - if error occurs during the SSH operations.public SSHClient forwardSSH(String remoteHost, int remotePort, com.microsoft.jenkins.azurecommons.remote.UsernameAuth sshCredentials) throws com.jcraft.jsch.JSchException
Consider in the case with 2 or more remote severs, where:
We can first establish an SSH connection to host A, and then use the port forwarding to forward the connection to the local port through the SSH connection of host A to reach the SSH server on host B.
SSHClient connectionToA = new SSHClient(host_A, port_A, credentials_A);
SSHClient tunnelConnectionToB = connectionToA.forwardSSH(host_B, port_B, credentials_B);
tunnelConnectionToB.execRemote("ls"); // ls executed on host B
remoteHost - the target host name or IP address, which is accessible from the SSH target of the current
SSHClient.remotePort - the SSH service port on the target host.sshCredentials - SSH authentication credentialscom.jcraft.jsch.JSchException - if error occurs during the SSH operations.public String getHost()
public int getPort()
public String getUsername()
public com.microsoft.jenkins.azurecommons.remote.UsernameAuth getCredentials()
public void close()
close in interface AutoCloseableCopyright © 2016–2018. All rights reserved.