Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ssh_client;
- import java.io.*;
- import ch.ethz.ssh2.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- * Hier sind alle SSH Funktionen enthalten. Gesteuert werden sie von der Web Klasse
- * @author cedric
- */
- public class SSH {
- Connection con;
- Session se;
- public SSH() {
- con = new Connection("");
- try {
- se = con.openSession();
- } catch (IOException ex) {
- System.err.println("Error \n" + ex);
- }
- }
- public String login(String host, String username, String password) {
- try {
- //connection erstellen und verbinden
- con = new Connection(host);
- con.connect();
- boolean isAuthenticated = con.authenticateWithPassword(username, password);
- if (isAuthenticated == false) {
- throw new IOException("Authentication failed.");
- }
- //session erstellen
- se = con.openSession();
- } catch (Exception e) {
- System.out.println("Fehler beim einloggen. \n " + e);
- }
- return null;
- }
- public String sendToServer (String command) {
- String line;
- try {
- //befehl senden
- se.execCommand(command);
- //rückmeldung einlesen
- InputStream stdout = new StreamGobbler(se.getStdout());
- BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
- while(true) {
- line = br.readLine();
- if(line == null) {
- break;
- }
- System.out.println(line);
- }
- } catch(Exception e) {
- System.err.println("Error: " + e);
- }
- return line;
- }
- public void logout() {
- try {
- se.close();//schließt die session
- con.close();//schließt die verbindung
- } catch(Exception e) {
- System.out.println("Fehler beim ausloggen \n" + e);
- }
- }
- }
Add Comment
Please, Sign In to add comment