public class SshClient extends AbstractFactoryManager implements ClientFactoryManager
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); 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(); } finally { client.stop(); }
Modifier and Type | Field and Description |
---|---|
protected org.apache.mina.core.service.IoConnector |
connector |
protected SessionFactory |
sessionFactory |
agentFactory, channelFactories, cipherFactories, compressionFactories, keyExchangeFactories, keyPairProvider, macFactories, properties, randomFactory, signatureFactories, version
DEFAULT_NIO_WORKERS, MAX_PACKET_SIZE, NIO_WORKERS, WINDOW_SIZE
Constructor and Description |
---|
SshClient() |
Modifier and Type | Method and Description |
---|---|
ConnectFuture |
connect(SocketAddress address) |
ConnectFuture |
connect(String host,
int port) |
protected org.apache.mina.transport.socket.nio.NioSocketConnector |
createAcceptor() |
ServerKeyVerifier |
getServerKeyVerifier()
Retrieve the server key verifier to be used to check the key when connecting
to an ssh server.
|
SessionFactory |
getSessionFactory() |
static void |
main(String[] args) |
void |
setServerKeyVerifier(ServerKeyVerifier serverKeyVerifier) |
void |
setSessionFactory(SessionFactory sessionFactory) |
static SshClient |
setUpDefaultClient()
Setup a default client.
|
void |
start() |
void |
stop() |
getAgentFactory, getChannelFactories, getCipherFactories, getCompressionFactories, getKeyExchangeFactories, getKeyPairProvider, getMacFactories, getNioWorkers, getProperties, getRandomFactory, getSignatureFactories, getVersion, loadVersion, setAgentFactory, setChannelFactories, setCipherFactories, setCompressionFactories, setKeyExchangeFactories, setKeyPairProvider, setMacFactories, setNioWorkers, setProperties, setRandomFactory, setSignatureFactories
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getAgentFactory, getChannelFactories, getCipherFactories, getCompressionFactories, getKeyExchangeFactories, getKeyPairProvider, getMacFactories, getProperties, getRandomFactory, getSignatureFactories, getVersion
protected org.apache.mina.core.service.IoConnector connector
protected SessionFactory sessionFactory
public SessionFactory getSessionFactory()
public void setSessionFactory(SessionFactory sessionFactory)
public ServerKeyVerifier getServerKeyVerifier()
ClientFactoryManager
getServerKeyVerifier
in interface ClientFactoryManager
public void setServerKeyVerifier(ServerKeyVerifier serverKeyVerifier)
public void start()
protected org.apache.mina.transport.socket.nio.NioSocketConnector createAcceptor()
public void stop()
public ConnectFuture connect(String host, int port) throws Exception
Exception
public ConnectFuture connect(SocketAddress address) throws Exception
Exception
public static SshClient setUpDefaultClient()
Copyright © 2008-2013 The Apache Software Foundation. All Rights Reserved.