org.apache.sshd
Class SshClient

java.lang.Object
  extended by org.apache.sshd.common.AbstractFactoryManager
      extended by 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

Field Summary
protected  org.apache.mina.core.service.IoConnector connector
           
protected  SessionFactory sessionFactory
           
 
Fields inherited from class org.apache.sshd.common.AbstractFactoryManager
channelFactories, cipherFactories, compressionFactories, keyExchangeFactories, keyPairProvider, macFactories, properties, randomFactory, signatureFactories, version
 
Fields inherited from interface org.apache.sshd.common.FactoryManager
DEFAULT_NIO_WORKERS, MAX_PACKET_SIZE, NIO_WORKERS, WINDOW_SIZE
 
Constructor Summary
SshClient()
           
 
Method Summary
 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()
           
 
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
 
Methods inherited from interface org.apache.sshd.common.FactoryManager
getChannelFactories, getCipherFactories, getCompressionFactories, getKeyExchangeFactories, getKeyPairProvider, getMacFactories, getProperties, getRandomFactory, getSignatureFactories, getVersion
 

Field Detail

connector

protected org.apache.mina.core.service.IoConnector connector

sessionFactory

protected SessionFactory sessionFactory
Constructor Detail

SshClient

public SshClient()
Method Detail

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.