Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

6.3. Programmatically connect to the console

A connection to the kernel Karaf console can also be done programmatically.
The following code is a simplified version of the code from the client library.

Code Block
langjava
import org.apache.geronimo.gshell.remote.crypto.CryptoContextsshd.ClientChannel;
import org.apache.geronimo.gshell.remote.client.RshClientsshd.ClientSession;
import org.apache.geronimo.gshell.remote.client.RemoteExecuteExceptionsshd.SshClient;
import org.apache.geronimosshd.gshell.remote.client.handler.EchoHandler;
import org.apache.geronimo.gshell.remote.client.handler.ClientMessageHandler;
import org.apache.geronimo.gshell.whisper.transport.TransportException;
import org.apache.geronimo.gshell.whisper.transport.TransportFactory;
import org.apache.geronimo.gshell.whisper.transport.TransportFactoryLocator;
import org.apache.geronimo.gshell.whisper.transport.tcp.SpringTcpTransportFactory;
import org.apache.geronimo.gshell.whisper.stream.StreamFeeder;
import org.apache.geronimo.gshell.layout.NotFoundException;
import org.apache.geronimo.gshell.ExitNotificationfuture.ConnectFuture;

public class Main {

    public static void main(String[] args) throws Exception {
        RshClientString clienthost = null"localhost";
        tryint {
port = 8101;
        String  CryptoContext contextuser = new CryptoContext("RSAkaraf", null);
        String password = "karaf";

   List<ClientMessageHandler> handlers = new LinkedList<ClientMessageHandler>();
 SshClient client = null;
        handlers.add(new EchoHandler());try {
            client = new RshClient(context, new Locator(), handlers);
SshClient.setUpDefaultClient();
            client.initializestart();
            ConnectFuture future = client.connect(addresshost, new URI("tcp://0.0.0.0:0"))port);
            clientfuture.loginawait(user, password);
            StreamFeederClientSession outputFeedersession = new StreamFeeder(client.getInputStream(), System.out future.getSession();
            session.authPassword(user, password);
            outputFeeder.createThread().start(ClientChannel channel = session.createChannel("shell");
            clientchannel.openShellsetIn(System.in);
            channel.setOut(System.out.println("Connected");

            clientchannel.execute(args[0]setErr(System.err);
          } catch channel.open(ExitNotification e) {;
            Systemchannel.exitwaitFor(ClientChannel.CLOSED, 0);
        } catch (Throwable t) {
            t.printStackTrace();
            System.exit(1);
        } finally {
            try {
                client.closeShell();
                client.closestop();
            } catch (Throwable t) { }
        }
        System.exit(0);
    }

    private static class Locator implements TransportFactoryLocator {
        SpringTcpTransportFactory factory = new SpringTcpTransportFactory();

        public TransportFactory locate(URI arg0) throws TransportException {
            return factory;
        }

    }
}

You can find a more complete example at the following location.

6.3. Programmatically connect to the console#top

Wiki Markup
{scrollbar}