Guest User

Untitled

a guest
Oct 21st, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. package ssh_client;
  2.  
  3. import java.io.*;
  4. import ch.ethz.ssh2.*;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7.  
  8. /**
  9.  * Hier sind alle SSH Funktionen enthalten. Gesteuert werden sie von der Web Klasse
  10.  * @author cedric
  11.  */
  12. public class SSH {
  13.    
  14.         Connection con;
  15.         Session se;
  16.         public SSH() {
  17.             con = new Connection("");
  18.         try {
  19.             se = con.openSession();
  20.         } catch (IOException ex) {
  21.             System.err.println("Error \n" + ex);
  22.         }
  23.         }
  24.        
  25.        
  26.     public String login(String host, String username, String password) {
  27.        
  28.         try {
  29.             //connection erstellen und verbinden
  30.             con = new Connection(host);
  31.             con.connect();
  32.             boolean isAuthenticated = con.authenticateWithPassword(username, password);
  33.             if (isAuthenticated == false) {
  34.                 throw new IOException("Authentication failed.");
  35.             }      
  36.             //session erstellen
  37.             se = con.openSession();
  38.  
  39.         } catch (Exception e) {
  40.             System.out.println("Fehler beim einloggen. \n " + e);
  41.         }
  42.         return null;
  43.     }
  44.        
  45.     public String sendToServer (String command) {
  46.         String line;
  47.        
  48.         try {            
  49.             //befehl senden
  50.             se.execCommand(command);
  51.            
  52.             //rückmeldung einlesen
  53.             InputStream stdout = new StreamGobbler(se.getStdout());
  54.             BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
  55.             while(true) {
  56.                 line = br.readLine();
  57.                 if(line == null) {
  58.                     break;
  59.                 }
  60.                 System.out.println(line);
  61.             }
  62.            
  63.         } catch(Exception e) {
  64.             System.err.println("Error: " + e);
  65.         }
  66.         return line;
  67.     }
  68.    
  69.     public void logout() {
  70.         try {
  71.             se.close();//schließt die session
  72.             con.close();//schließt die verbindung
  73.         } catch(Exception e) {
  74.             System.out.println("Fehler beim ausloggen \n" + e);
  75.         }
  76.        
  77.     }
  78.    
  79. }
Add Comment
Please, Sign In to add comment