Package org.sentrysoftware.ssh
Class SshClient
java.lang.Object
org.sentrysoftware.ssh.SshClient
- All Implemented Interfaces:
AutoCloseable
SSH Client that lets you perform basic SSH operations
- Instantiate the SSH Client
- Connect
- Authenticate
- Enjoy!
- DISCONNECT!
- Author:
- Bertrand
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Represents the result of a command execution -
Constructor Summary
ConstructorsConstructorDescriptionCreates an SSHClient to connect to the specified hostnameCreates an SSHClient to connect to the specified hostnameCreates an SSHClient to connect to the specified hostname -
Method Summary
Modifier and TypeMethodDescriptionboolean
authenticate
(String username) Authenticate the SSH Client against the SSH server using NO passwordboolean
authenticate
(String username, char[] password) Authenticate the SSH Client against the SSH server using a passwordboolean
authenticate
(String username, File privateKeyFile, char[] password) Authenticate the SSH Client against the SSH server using a private keyboolean
authenticate
(String username, String password) Deprecated.(since = "3.14.00", forRemoval = true)boolean
authenticate
(String username, String privateKeyFile, String password) Deprecated.(since = "3.14.00", forRemoval = true)void
Check if the SSH session exists.void
close()
Disconnects the SSH Client from the SSH servervoid
connect()
Connects the SSH Client to the SSH servervoid
connect
(int timeout) Connects the SSH client to the SSH server using the default SSH port (22).void
connect
(int timeout, int port) Connects the SSH client to the SSH server using a specified SSH port.void
Deprecated.(since = "3.14.00", forRemoval = true)executeCommand
(String command) Executes a command through the SSH connectionexecuteCommand
(String command, int timeout) Executes a command through the SSH connectionvoid
interactiveSession
(InputStream in, OutputStream out) Starts an interactive session.List the content of the specified directory through the SSH connection (using SCP)void
Open a SSH Session.void
Open a Terminal request for a wiiiiide terminal, with no ECHO (see https://tools.ietf.org/html/rfc4254#section-8)read
(int size, int timeout) Read the stdout and stderr from the SSH session.Read the specified file over the SSH session that was established.readFileAttributes
(String filePath) Return information about the specified file on the connected system, in the same format as the PSL functionfile()
void
removeFile
(String remoteFilePath) Removes the specified file on the remote system.void
removeFile
(String[] remoteFilePathArray) Removes a list of files on the remote system.void
Copy a file to the remote host through SCPvoid
Write into the SSH session.
-
Constructor Details
-
SshClient
Creates an SSHClient to connect to the specified hostname- Parameters:
pHostname
- Hostname of the SSH server to connect to
-
SshClient
Creates an SSHClient to connect to the specified hostname- Parameters:
pHostname
- Hostname of the SSH server to connect topLocale
- Locale used on the remote server (e.g. zh_CN.utf8)
-
SshClient
Creates an SSHClient to connect to the specified hostname- Parameters:
pHostname
- Hostname of the SSH server to connect topCharset
- Charset used on the remote server
-
-
Method Details
-
connect
Connects the SSH Client to the SSH server- Throws:
IOException
-
connect
Connects the SSH client to the SSH server using the default SSH port (22).- Parameters:
timeout
- Timeout in milliseconds- Throws:
IOException
- when connection fails or when the server does not respond (SocketTimeoutException)
-
connect
Connects the SSH client to the SSH server using a specified SSH port.- Parameters:
timeout
- Timeout in millisecondsport
- SSH server port.- Throws:
IOException
- when connection fails or when the server does not respond (SocketTimeoutException)
-
disconnect
Deprecated.(since = "3.14.00", forRemoval = true)Disconnects the SSH Client from the SSH serverNote: This is important! Otherwise, the listener thread will remain running forever! Use a try with resource instead or the
close()
instead. -
close
public void close()Disconnects the SSH Client from the SSH serverNote: This is important! Otherwise, the listener thread will remain running forever!
- Specified by:
close
in interfaceAutoCloseable
-
authenticate
@Deprecated public boolean authenticate(String username, String privateKeyFile, String password) throws IOException Deprecated.(since = "3.14.00", forRemoval = true)Authenticate the SSH Client against the SSH server using a private key- Parameters:
username
-privateKeyFile
-password
-- Returns:
- a boolean stating whether the authentication worked or not
- Throws:
IOException
-
authenticate
public boolean authenticate(String username, File privateKeyFile, char[] password) throws IOException Authenticate the SSH Client against the SSH server using a private key- Parameters:
username
-privateKeyFile
-password
-- Returns:
- a boolean stating whether the authentication worked or not
- Throws:
IOException
-
authenticate
Deprecated.(since = "3.14.00", forRemoval = true)Authenticate the SSH Client against the SSH server using a password- Parameters:
username
-password
-- Returns:
- a boolean stating whether the authentication worked or not
- Throws:
IOException
-
authenticate
Authenticate the SSH Client against the SSH server using a password- Parameters:
username
-password
-- Returns:
- a boolean stating whether the authentication worked or not
- Throws:
IOException
-
authenticate
Authenticate the SSH Client against the SSH server using NO password- Parameters:
username
-- Returns:
- a boolean stating whether the authentication worked or not
- Throws:
IOException
-
readFileAttributes
Return information about the specified file on the connected system, in the same format as the PSL functionfile()
- Parameters:
filePath
- Path to the file on the remote system- Throws:
IOException
-
listFiles
public String listFiles(String remoteDirectoryPath, String regExpFileMask, boolean includeSubfolders) throws IOException List the content of the specified directory through the SSH connection (using SCP)- Parameters:
remoteDirectoryPath
- The path to the directory to list on the remote hostregExpFileMask
- A regular expression that listed files must match with to be listedincludeSubfolders
- Whether to parse subdirectories as well- Returns:
- The list of files in the specified directory, separated by end-of-lines
- Throws:
IOException
- When something bad happens while communicating with the remote hostIllegalStateException
- If called while not yet connected
-
readFile
Read the specified file over the SSH session that was established.- Parameters:
remoteFilePath
- Path to the file to be read on the remote hostreadOffset
- Offset to read fromreadSize
- Amount of bytes to be read- Returns:
- The content of the file read
- Throws:
IOException
- when something gets wrong while reading the file (we get disconnected, for example, or we couldn't read the file)IllegalStateException
- when the session hasn't been properly authenticated first
-
removeFile
Removes a list of files on the remote system.- Parameters:
remoteFilePathArray
- Array of paths to the files to be deleted on the remote host- Throws:
IOException
- when something bad happens while deleting the fileIllegalStateException
- when not connected and authenticated yet
-
removeFile
Removes the specified file on the remote system.- Parameters:
remoteFilePath
-- Throws:
IOException
IllegalStateException
-
executeCommand
Executes a command through the SSH connection- Parameters:
command
- The command to be executed- Returns:
- a CommandResult object with the result of the execution
- Throws:
IllegalStateException
- when the connection is not established firstIOException
- when there is a problem while communicating with the remote system
-
executeCommand
Executes a command through the SSH connection- Parameters:
command
- The command to be executedtimeout
- Milliseconds after which the command is considered failed- Returns:
- a CommandResult object with the result of the execution
- Throws:
IllegalStateException
- when the connection is not established firstIOException
- when there is a problem while communicating with the remote system
-
interactiveSession
public void interactiveSession(InputStream in, OutputStream out) throws IOException, InterruptedException Starts an interactive session.- Parameters:
in
- Where the input is coming from (typically System.in)out
- Where the output has to go (e.g. System.out)- Throws:
IllegalStateException
- when not connected and authenticatedIOException
- in case of communication problems with the hostInterruptedException
- when a thread is interrupted
-
scp
public void scp(String localFilePath, String remoteFilename, String remoteDirectory, String fileMode) throws IOException Copy a file to the remote host through SCP- Parameters:
localFilePath
-remoteFilename
-remoteDirectory
-fileMode
-- Throws:
IOException
-
openSession
Open a SSH Session.- Throws:
IOException
- When an I/O error occurred.
-
openTerminal
Open a Terminal request for a wiiiiide terminal, with no ECHO (see https://tools.ietf.org/html/rfc4254#section-8)- Throws:
IOException
- When an I/O error occurred.
-
write
Write into the SSH session.- Parameters:
text
- The text to be written.- Throws:
IOException
- When an I/O error occurred.
-
read
Read the stdout and stderr from the SSH session.- Parameters:
size
- The buffer size of stdout and/or stderr to be read. (If less than 0 all data)timeout
- Timeout in seconds- Returns:
- An optional with all the data read (stdout and stderr). Empty if nothing was read.
- Throws:
IOException
- When an I/O error occurred.
-
checkIfSessionOpened
public void checkIfSessionOpened()Check if the SSH session exists.
-