SSH-Connection - Auto-Vervollständigung mittels TAB

sorny

Mitglied
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:

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;
	}
	
}


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
 
Die Auto-Vervollständigung ist ja Bestandteil der Shell und nicht der SSH-Verbindung. Insofern sollte es doch reichen, wenn Du einen Tab (oder je nach Shell auch zwei) hinschickst und die Ausgabe liest.
 
Hab ich scho versucht, funktioniert leider nicht. Wobei ich zur Zeit nur mehr ein kleines Problem hab.

Keine Ahnung welchen KeyChar die Shell als Tab erwartet!?
 
Ich hab mal etwas gegoogelt und bin auf einen Commit gestoßen, der die Folge "tab key" enthält. Such mal danach. Dort wird als Demo offensichtlich ein Swing-Client vorgestellt. Dabei wird einfach nur der KeyChar aus dem [c]KeyEvent[/c] geholt und so wie er ist abgeschickt. Demnach ist das wohl einfach nur der Code 9; wie in der ASCII-Tabelle.
 
Jup, hab ich schon getestet, funktioniert leider nicht. Muss wohl irgendwo in meinem Code liegen. Aus dem Swing-Beispiel hab ich scho einiges an Information rausgezogen. Mal nochmal alles überdenken.
 

Zurück
Oben