Auf Thema antworten

Aloha :)


Ich schreibe atm gerade an einem Online-SSH Client mit JSP.

Meine Probleme liegen nicht an JSP, Servlets usw, sondern an der dahinterliegenden Connection.


Ich verwende die Library "Ganymed 2 SSH2" Ganymed SSH-2 for Java


Soweit sogut, Verbindung aufbauen, Welcome-Message auslesen, Kommandos absetzen, Verbindung beenden usw funktionieren super.

Das Problem liegt zur Zeit eher an einer "Komfort-Funktion". Mein Ziel ist es, TAB verwenden zu können, um die Auto-Vervollständigung eines Terminals zu simulieren.


Hier mal mein Code:


[code=Java]

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;


import ch.ethz.ssh2.Connection;

import ch.ethz.ssh2.Session;

import ch.ethz.ssh2.StreamGobbler;


/*

 * @author: Gerald Reisinger

 * @description: Basic SSH-connection functions like connect, exec & disconnect

 * @date: 18.05.2010 14:32

 */


public class SSH_connection {

   

   

    //Construktor

    public SSH_connection(String host, String username, String password) {

        this.setHostname(host);

        this.setUsername(username);

        this.setPassword(password);

       

        this.initConnection();

    }

   

   

    //Variables

    private String hostname;

    private String username;

    private String password;

    private String welcome_message ="";

    private Connection conn = null;

   

    int y= 0;

    int x =0;

    char[][] lines = new char[y][];

    int posy = 0;

    int posx = 0;


   

    private void initConnection() {

        try {

            // Create a connection instance

            this.conn = new Connection(hostname);

            conn.connect();

           

            // Authenticate

            boolean isAuthenticated = conn.authenticateWithPassword(username, password);

            if (isAuthenticated == false)

                throw new IOException("Authentication failed.");

           

            Session sess = conn.openSession();


            //Catch Welcome Message

           

            sess.requestPTY("bla");

            sess.startShell();


            //Testing

            //Sending "cd /e" to the pseudo-Terminal and the tab-keycode(=9)

            //seems not to work since it goes into nowhere/is not read by the input-reader

            OutputStream out = sess.getStdin();

            out.write(99);

            out.write(101);

            out.write(32);

            out.write(47);

            out.write(101);

            out.write(9);

            //enter-key

             //out.write(10);

           

            StreamGobbler stdout = new StreamGobbler(sess.getStdout());

            //InputStream stdout = new StreamGobbler(sess.getStdout());

            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));


            int c = sess.getStdout().available();

           

            System.out.println(c);

           

            for (int i = 1; i <= c; i++) {

                int bla = br.read();

                this.welcome_message += String.valueOf((char)bla);

            }

   

            System.out.println(this.welcome_message);

            //System.out.println(this.username+this.hostname+":~$ ");

           

            sess.close();

           

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

   

    public String exec(String command) {


       

        //Only one command executions per session possible.

        //This is not a restriction of the library, but rather an enforcement by the underlying SSH-2 protocol

       

        String answer ="";

        Session sess = null;

       

        try {

            sess = conn.openSession();

            sess.execCommand(command);


            InputStream stdout = new StreamGobbler(sess.getStdout());

            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));


            while (true)

            {

                String line = br.readLine();

                if (line == null)

                    break;

                //System.out.println(line);

                answer += line;

            }

           

        } catch (IOException e) {

            e.printStackTrace();

        }


       

        sess.close();

       

        return answer;

       

    }

   

    //atm not working, quite difficult to handle the pseudo-terminal in the background

    public void tabbed() {

       

        try {

            Session sess = conn.openSession();

            //String answer ="";

           

            //Testing

           

            OutputStream out;

            out = sess.getStdin();

            out.write(99);

            out.write(101);

            out.write(32);

            out.write(47);

            out.write(101);

            out.write(9);


            InputStream stdout = new StreamGobbler(sess.getStdout());

            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));


            while (true)

            {

                String line = br.readLine();

                if (line == null)

                    break;

                //System.out.println(line);

                //answer += line;

            }

           

        } catch (IOException e) {

            e.printStackTrace();

        }

       

    }

   

    //Close the connection

    public boolean disconnect() {

        this.conn.close();

        return true;

    }

   

    //Getter & Setter

    public String getHostname() {

        return hostname;

    }

    public void setHostname(String hostname) {

        this.hostname = hostname;

    }

    public String getUsername() {

        return username;

    }

    public void setUsername(String username) {

        this.username = username;

    }

    public String getPassword() {

        return password;

    }

    public void setPassword(String password) {

        this.password = password;

    }


    public String getWelcome_message() {

        return welcome_message;

    }


    public void setWelcome_message(String welcomeMessage) {

        welcome_message = welcomeMessage;

    }

    public String getUserHost() {

        String msg = this.username+this.hostname+":~$ ";

        return msg;

    }

   

}


[/code]



Vllt habt ihr ja Erfahrung mit dieser Lib, einer anderen Lib für SSH-Connections oder vllt ne andere Idee, die Auto-Vervollständigung zu realisieren.

LG Gerald



Oben