public class SshClient extends AbstractFactoryManager implements ClientFactoryManager, Closeable
Entry point for the client side of the SSH protocol.
The default configured client can be created using
the setUpDefaultClient(). The next step is to
start the client using the start() method.
Sessions can then be created using on of the
connect(String, String, int) or connect(String, java.net.SocketAddress)
methods.
The client can be stopped any time using the stop() method.
Following is an example of using the SshClient:
try(SshClient client = SshClient.setUpDefaultClient()) {
client.start();
try(ClientSession session = client.connect(login, host, port).await().getSession()) {
session.addPasswordIdentity(password);
session.auth().verify();
try(ClientChannel channel = session.createChannel("shell")) {
channel.setIn(new NoCloseInputStream(System.in));
channel.setOut(new NoCloseOutputStream(System.out));
channel.setErr(new NoCloseOutputStream(System.err));
channel.open();
channel.waitFor(ClientChannel.CLOSED, 0);
} finally {
session.close(false);
}
} finally {
client.stop();
}
}
CloseableUtils.AbstractCloseable.State| Modifier and Type | Field and Description |
|---|---|
protected IoConnector |
connector |
static List<ServiceFactory> |
DEFAULT_SERVICE_FACTORIES |
static Factory<SshClient> |
DEFAULT_SSH_CLIENT_FACTORY |
static List<NamedFactory<UserAuth>> |
DEFAULT_USER_AUTH_FACTORIES |
protected SessionFactory |
sessionFactory |
protected List<NamedFactory<UserAuth>> |
userAuthFactories |
protected UserInteraction |
userInteraction |
agentFactory, channelFactories, cipherFactories, compressionFactories, executor, fileSystemFactory, globalRequestHandlers, ioServiceFactory, ioServiceFactoryFactory, keyExchangeFactories, keyPairProvider, macFactories, properties, randomFactory, serviceFactories, sessionTimeoutListener, shutdownExecutor, signatureFactories, tcpipForwarderFactory, tcpipForwardingFilter, timeoutListenerFuturecloseFuture, lock, statelogDEFAULT_HEARTBEAT_INTERVAL, DEFAULT_KEEP_ALIVE_HEARTBEAT_STRING, DEFAULT_PASSWORD_PROMPTS, HEARTBEAT_INTERVAL, HEARTBEAT_REQUEST, PASSWORD_PROMPTS, PREFERRED_AUTHSAUTH_TIMEOUT, CHANNEL_CLOSE_TIMEOUT, DEFAULT_NIO_WORKERS, DEFAULT_VERSION, DISCONNECT_TIMEOUT, IDLE_TIMEOUT, MAX_PACKET_SIZE, NIO_WORKERS, NIO2_READ_BUFFER_SIZE, SOCKET_BACKLOG, SOCKET_KEEPALIVE, SOCKET_LINGER, SOCKET_RCVBUF, SOCKET_REUSEADDR, SOCKET_SNDBUF, TCP_NODELAY, WINDOW_SIZE, WINDOW_TIMEOUT| Constructor and Description |
|---|
SshClient() |
createSessionTimeoutListener, getAgentFactory, getChannelFactories, getCipherFactories, getCompressionFactories, getFileSystemFactory, getGlobalRequestHandlers, getIoServiceFactory, getIoServiceFactoryFactory, getKeyExchangeFactories, getKeyPairProvider, getMacFactories, getNioWorkers, getProperties, getRandomFactory, getScheduledExecutorService, getServiceFactories, getSignatureFactories, getTcpipForwarderFactory, getTcpipForwardingFilter, getVersion, removeSessionTimeout, setAgentFactory, setChannelFactories, setCipherFactories, setCompressionFactories, setFileSystemFactory, setGlobalRequestHandlers, setIoServiceFactoryFactory, setKeyExchangeFactories, setKeyPairProvider, setMacFactories, setNioWorkers, setProperties, setRandomFactory, setScheduledExecutorService, setScheduledExecutorService, setServiceFactories, setSignatureFactories, setTcpipForwarderFactory, setTcpipForwardingFilter, setupSessionTimeout, stopSessionTimeoutListenerdoCloseGracefully, doCloseImmediatelybuilder, close, isClosed, isClosing, preCloseclose, isOpenclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitgetAgentFactory, getChannelFactories, getCipherFactories, getCompressionFactories, getFileSystemFactory, getGlobalRequestHandlers, getIoServiceFactory, getKeyExchangeFactories, getKeyPairProvider, getMacFactories, getProperties, getRandomFactory, getScheduledExecutorService, getServiceFactories, getSignatureFactories, getTcpipForwarderFactory, getTcpipForwardingFilter, getVersionpublic static final List<NamedFactory<UserAuth>> DEFAULT_USER_AUTH_FACTORIES
public static final List<ServiceFactory> DEFAULT_SERVICE_FACTORIES
protected IoConnector connector
protected SessionFactory sessionFactory
protected UserInteraction userInteraction
protected List<NamedFactory<UserAuth>> userAuthFactories
public SessionFactory getSessionFactory()
public void setSessionFactory(SessionFactory sessionFactory)
public ServerKeyVerifier getServerKeyVerifier()
ClientFactoryManagergetServerKeyVerifier in interface ClientFactoryManagerpublic void setServerKeyVerifier(ServerKeyVerifier serverKeyVerifier)
public UserInteraction getUserInteraction()
getUserInteraction in interface ClientFactoryManagerUserInteraction object to communicate with the user
(may be null to indicate that no such communication is allowed)public void setUserInteraction(UserInteraction userInteraction)
public List<NamedFactory<UserAuth>> getUserAuthFactories()
getUserAuthFactories in interface ClientFactoryManagerList of UserAuth NamedFactory-ies - never
null/emptypublic void setUserAuthFactories(List<NamedFactory<UserAuth>> userAuthFactories)
protected void checkConfig()
checkConfig in class AbstractFactoryManagerpublic void start()
public void stop()
public void open()
throws IOException
IOExceptionprotected Closeable getInnerCloseable()
getInnerCloseable in class CloseableUtils.AbstractInnerCloseablepublic ConnectFuture connect(String username, String host, int port) throws IOException
IOExceptionpublic ConnectFuture connect(String username, SocketAddress address)
protected IoConnector createConnector()
protected SessionFactory createSessionFactory()
public static SshClient setUpDefaultClient()
public static ClientSession setupClientSession(String portOption, BufferedReader stdin, PrintStream stdout, PrintStream stderr, String... args) throws Exception
ExceptionCopyright © 2008–2015 The Apache Software Foundation. All rights reserved.