public class SshClient extends AbstractFactoryManager implements ClientFactoryManager, Closeable
setUpDefaultClient(). The next step is to
start the client using the start() method.
Sessions can then be created using on of the
connect(String, int) or connect(java.net.SocketAddress)
methods.
The client can be stopped at anytime using the stop() method.
Following is an example of using the SshClient:
SshClient client = SshClient.setUpDefaultClient();
client.start();
try {
ClientSession session = client.connect(host, port).await().getSession();
int ret = ClientSession.WAIT_AUTH;
while ((ret & ClientSession.WAIT_AUTH) != 0) {
System.out.print("Password:");
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String password = r.readLine();
session.authPassword(login, password);
ret = session.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0);
}
if ((ret & ClientSession.CLOSED) != 0) {
System.err.println("error");
System.exit(-1);
}
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);
session.close(false);
} finally {
client.stop();
}
| Modifier and Type | Field and Description |
|---|---|
protected IoConnector |
connector |
protected Factory<IoConnector> |
connectorFactory |
protected SessionFactory |
sessionFactory |
protected java.util.List<NamedFactory<UserAuth>> |
userAuthFactories |
protected UserInteraction |
userInteraction |
agentFactory, channelFactories, cipherFactories, compressionFactories, executor, fileSystemFactory, globalRequestHandlers, ioServiceFactory, ioServiceFactoryFactory, keyExchangeFactories, keyPairProvider, log, macFactories, properties, randomFactory, serviceFactories, shutdownExecutor, signatureFactories, tcpipForwarderFactory, tcpipForwardingFilter, versionHEARTBEAT_INTERVAL, HEARTBEAT_REQUEST, PASSWORD_PROMPTS, PREFERRED_AUTHSDEFAULT_NIO_WORKERS, MAX_PACKET_SIZE, NIO_WORKERS, WINDOW_SIZE| Constructor and Description |
|---|
SshClient() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
checkConfig() |
CloseFuture |
close(boolean immediately)
Close this resource asynchronously and return a future.
|
ConnectFuture |
connect(java.net.SocketAddress address)
Deprecated.
|
ConnectFuture |
connect(java.lang.String host,
int port)
Deprecated.
|
ConnectFuture |
connect(java.lang.String username,
java.net.SocketAddress address) |
ConnectFuture |
connect(java.lang.String username,
java.lang.String host,
int port) |
protected IoConnector |
createConnector() |
protected SessionFactory |
createSessionFactory() |
ServerKeyVerifier |
getServerKeyVerifier()
Retrieve the server key verifier to be used to check the key when connecting
to an ssh server.
|
SessionFactory |
getSessionFactory() |
java.util.List<NamedFactory<UserAuth>> |
getUserAuthFactories()
Retrieve a list of UserAuth factories.
|
UserInteraction |
getUserInteraction()
Retrieve the UserInteraction object to communicate with the user.
|
static void |
main(java.lang.String[] args) |
void |
open() |
void |
setServerKeyVerifier(ServerKeyVerifier serverKeyVerifier) |
void |
setSessionFactory(SessionFactory sessionFactory) |
static SshClient |
setUpDefaultClient()
Setup a default client.
|
void |
setUserAuthFactories(java.util.List<NamedFactory<UserAuth>> userAuthFactories) |
void |
setUserInteraction(UserInteraction userInteraction) |
void |
start() |
void |
stop() |
getAgentFactory, getChannelFactories, getCipherFactories, getCompressionFactories, getFileSystemFactory, getGlobalRequestHandlers, getIoServiceFactory, getIoServiceFactoryFactory, getKeyExchangeFactories, getKeyPairProvider, getMacFactories, getNioWorkers, getProperties, getRandomFactory, getScheduledExecutorService, getServiceFactories, getSignatureFactories, getTcpipForwarderFactory, getTcpipForwardingFilter, getVersion, loadVersion, setAgentFactory, setChannelFactories, setCipherFactories, setCompressionFactories, setFileSystemFactory, setGlobalRequestHandlers, setIoServiceFactoryFactory, setKeyExchangeFactories, setKeyPairProvider, setMacFactories, setNioWorkers, setProperties, setRandomFactory, setScheduledExecutorService, setScheduledExecutorService, setServiceFactories, setSignatureFactories, setTcpipForwarderFactory, setTcpipForwardingFilterclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetTcpipForwarderFactorygetAgentFactory, getChannelFactories, getCipherFactories, getCompressionFactories, getFileSystemFactory, getGlobalRequestHandlers, getIoServiceFactory, getKeyExchangeFactories, getKeyPairProvider, getMacFactories, getProperties, getRandomFactory, getScheduledExecutorService, getServiceFactories, getSignatureFactories, getTcpipForwardingFilter, getVersionprotected IoConnector connector
protected SessionFactory sessionFactory
protected UserInteraction userInteraction
protected Factory<IoConnector> connectorFactory
protected java.util.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()
ClientFactoryManagergetUserInteraction in interface ClientFactoryManagerUserInteraction or nullpublic void setUserInteraction(UserInteraction userInteraction)
public java.util.List<NamedFactory<UserAuth>> getUserAuthFactories()
ClientFactoryManagergetUserAuthFactories in interface ClientFactoryManagerUserAuth factories, never nullpublic void setUserAuthFactories(java.util.List<NamedFactory<UserAuth>> userAuthFactories)
protected void checkConfig()
public void start()
public void stop()
public void open()
throws java.io.IOException
java.io.IOExceptionpublic CloseFuture close(boolean immediately)
Closeable@Deprecated public ConnectFuture connect(java.lang.String host, int port) throws java.io.IOException
java.io.IOExceptionpublic ConnectFuture connect(java.lang.String username, java.lang.String host, int port) throws java.io.IOException
java.io.IOException@Deprecated public ConnectFuture connect(java.net.SocketAddress address)
public ConnectFuture connect(java.lang.String username, java.net.SocketAddress address)
protected IoConnector createConnector()
protected SessionFactory createSessionFactory()
public static SshClient setUpDefaultClient()
public static void main(java.lang.String[] args)
throws java.lang.Exception
java.lang.ExceptionCopyright © 2008-2014 The Apache Software Foundation. All Rights Reserved.