org.apache.sshd
Class SshClient
java.lang.Object
org.apache.sshd.common.AbstractFactoryManager
org.apache.sshd.SshClient
- All Implemented Interfaces:
- ClientFactoryManager, FactoryManager
public class SshClient
- extends AbstractFactoryManager
- implements ClientFactoryManager
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, 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();
}
- Author:
- Apache MINA SSHD Project
Methods inherited from class org.apache.sshd.common.AbstractFactoryManager |
getChannelFactories, getCipherFactories, getCompressionFactories, getKeyExchangeFactories, getKeyPairProvider, getMacFactories, getNioWorkers, getProperties, getRandomFactory, getSignatureFactories, getVersion, loadVersion, setChannelFactories, setCipherFactories, setCompressionFactories, setKeyExchangeFactories, setKeyPairProvider, setMacFactories, setNioWorkers, setProperties, setRandomFactory, setSignatureFactories |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
connector
protected org.apache.mina.core.service.IoConnector connector
sessionFactory
protected SessionFactory sessionFactory
SshClient
public SshClient()
getSessionFactory
public SessionFactory getSessionFactory()
setSessionFactory
public void setSessionFactory(SessionFactory sessionFactory)
getServerKeyVerifier
public ServerKeyVerifier getServerKeyVerifier()
- Description copied from interface:
ClientFactoryManager
- Retrieve the server key verifier to be used to check the key when connecting
to an ssh server.
- Specified by:
getServerKeyVerifier
in interface ClientFactoryManager
- Returns:
- the server key verifier to use
setServerKeyVerifier
public void setServerKeyVerifier(ServerKeyVerifier serverKeyVerifier)
start
public void start()
createAcceptor
protected org.apache.mina.transport.socket.nio.NioSocketConnector createAcceptor()
stop
public void stop()
connect
public ConnectFuture connect(String host,
int port)
throws Exception
- Throws:
Exception
connect
public ConnectFuture connect(SocketAddress address)
throws Exception
- Throws:
Exception
setUpDefaultClient
public static SshClient setUpDefaultClient()
- Setup a default client. The client does not require any additional setup.
- Returns:
- a newly create SSH client
main
public static void main(String[] args)
throws Exception
- Throws:
Exception
Copyright © 2008-2012 Apache Software Foundation. All Rights Reserved.