Package com.jcabi.ssh
Class Ssh
- java.lang.Object
-
- com.jcabi.ssh.Ssh
-
- All Implemented Interfaces:
Shell
public final class Ssh extends Object
Single SSH Channel.This class implements
Shell
interface. In order to use it, just make an instance and callShell.exec(String, java.io.InputStream, java.io.OutputStream, java.io.OutputStream)
exec()}:String hello = new Shell.Plain( new SSH( "ssh.example.com", 22, "yegor", "-----BEGIN RSA PRIVATE KEY-----..." ) ).exec("echo 'Hello, world!'");
It is highly recommended to use classes from
Shell
interface, they will simplify operations.- Since:
- 1.0
- See Also:
- article by Yegor Bugayenko
- To do:
- #30:30min Refactor this class into smaller ones to avoid null checking of passphrase. There should probably be separate classes for encrypted/unencrypted private key.
- Suppressed Checkstyle violations:
- ClassDataAbstractionCouplingCheck (500 lines)
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.jcabi.ssh.Shell
Shell.Empty, Shell.Fake, Shell.Plain, Shell.Safe, Shell.Verbose
-
-
Field Summary
Fields Modifier and Type Field Description static int
PORT
Default SSH port.
-
Constructor Summary
Constructors Constructor Description Ssh(String adr, int prt, String user, String priv)
Constructor.Ssh(String adr, int prt, String user, String priv, String passphrs)
Constructor.Ssh(String adr, int prt, String user, URL priv)
Constructor.Ssh(String adr, String user, String priv)
Constructor.Ssh(String adr, String user, URL priv)
Constructor.Ssh(InetAddress adr, int prt, String user, URL priv)
Constructor.Ssh(InetAddress adr, String user, String priv)
Constructor.Ssh(InetAddress adr, String user, URL priv)
Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static String
escape(String arg)
Escape SSH argument.int
exec(String command, InputStream stdin, OutputStream stdout, OutputStream stderr)
Execute and return exit code.protected com.jcraft.jsch.Session
session()
Create and return a session, connected.
-
-
-
Field Detail
-
PORT
public static final int PORT
Default SSH port.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Ssh
public Ssh(String adr, String user, URL priv) throws IOException
Constructor.- Parameters:
adr
- IP addressuser
- Loginpriv
- Private SSH key- Throws:
IOException
- If fails- Since:
- 1.4
-
Ssh
public Ssh(InetAddress adr, String user, URL priv) throws IOException
Constructor.- Parameters:
adr
- IP addressuser
- Loginpriv
- Private SSH key- Throws:
IOException
- If fails- Since:
- 1.4
-
Ssh
public Ssh(String adr, String user, String priv) throws UnknownHostException
Constructor.- Parameters:
adr
- IP addressuser
- Loginpriv
- Private SSH key- Throws:
UnknownHostException
- If fails- Since:
- 1.4
-
Ssh
public Ssh(InetAddress adr, String user, String priv) throws UnknownHostException
Constructor.- Parameters:
adr
- IP addressuser
- Loginpriv
- Private SSH key- Throws:
UnknownHostException
- If fails- Since:
- 1.4
-
Ssh
public Ssh(String adr, int prt, String user, URL priv) throws IOException
Constructor.- Parameters:
adr
- IP addressprt
- Port of serveruser
- Loginpriv
- Private SSH key- Throws:
IOException
- If fails- Since:
- 1.4
- Suppressed Checkstyle violations:
- ParameterNumberCheck (6 lines)
-
Ssh
public Ssh(InetAddress adr, int prt, String user, URL priv) throws IOException
Constructor.- Parameters:
adr
- IP addressprt
- Port of serveruser
- Loginpriv
- Private SSH key- Throws:
IOException
- If fails- Since:
- 1.4
- Suppressed Checkstyle violations:
- ParameterNumberCheck (6 lines)
-
Ssh
public Ssh(String adr, int prt, String user, String priv) throws UnknownHostException
Constructor.- Parameters:
adr
- IP addressprt
- Port of serveruser
- Loginpriv
- Private SSH key- Throws:
UnknownHostException
- If fails- Suppressed Checkstyle violations:
- ParameterNumberCheck (6 lines)
-
Ssh
public Ssh(String adr, int prt, String user, String priv, String passphrs) throws UnknownHostException
Constructor.- Parameters:
adr
- IP addressprt
- Port of serveruser
- Loginpriv
- Private SSH keypassphrs
- Pass phrase for encrypted priv. key- Throws:
UnknownHostException
- when host is unknown.- Suppressed Checkstyle violations:
- ParameterNumberCheck (6 lines)
-
-
Method Detail
-
escape
public static String escape(String arg)
Escape SSH argument.- Parameters:
arg
- Argument to escape- Returns:
- Escaped
-
session
@RetryOnFailure(attempts=7, delay=1L, unit=MINUTES, verbose=false, types=java.io.IOException.class) protected com.jcraft.jsch.Session session() throws IOException
Create and return a session, connected.- Returns:
- JSch session
- Throws:
IOException
- If some IO problem inside
-
exec
public int exec(String command, InputStream stdin, OutputStream stdout, OutputStream stderr) throws IOException
Description copied from interface:Shell
Execute and return exit code.- Specified by:
exec
in interfaceShell
- Parameters:
command
- Commandstdin
- Stdin (will be closed)stdout
- Stdout (will be closed)stderr
- Stderr (will be closed)- Returns:
- Exit code
- Throws:
IOException
- If fails
-
-