vendredi 22 mai 2015

SSH sessions

Hallo zusammen,

ich hoffe ihr könnt mir weiterhelfen.
ich habe nur wenig Erfahrung mit Java.

ich will per Java über SSH aufs Raspberry zugreifen. (Commands schicken)
Das funktioniert auch soweit. Verbindung kann ich aufbauen und einzelne Befehle schicken.

Mein Ziel: Swingoberfläche bauen (kein Problem, hab ich schon oft gemacht)
Auf dieser GUI befinden sich Keylistener, die bei Tastenschlag bestimmte Methoden aufrufen sollen (hab ich auch schon)

Nur hab ich keine Ahnung, wie ich per diesen aufgerufenen Methoden einen Befehl per SSH schicken soll.

Mein SSH Zugriff funktioniert folgendermaßen:


  • connection aufbauen. (mit zugangsdaten)
  • Session starten.
  • Befehl schicken.
  • session beenden.
  • connection schließen.


Natürlich könnte ich einfach die gesammte Prozedur in die Methoden schreiben, aber ich will nicht bei jeden tastenschlag einen neue connection aufbauen usw...

code von Ganymed SSH-2

Java Code:

  1.  
  2. package control;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8.  
  9. import ch.ethz.ssh2.Connection;
  10. import ch.ethz.ssh2.Session;
  11. import ch.ethz.ssh2.StreamGobbler;
  12.  
  13. public class Basic
  14. {
  15. public static void main(String[] args)
  16. {
  17. String hostname = "192.168.1.105";
  18. String username = "pi";
  19. String password = "ultrageheim";
  20.  
  21. try
  22. {
  23. /* Create a connection instance */
  24.  
  25. Connection conn = new Connection(hostname);
  26.  
  27. /* Now connect */
  28.  
  29. conn.connect();
  30.  
  31. /* Authenticate.
  32.   * If you get an IOException saying something like
  33.   * "Authentication method password not supported by the server at this stage."
  34.   * then please check the FAQ.
  35.   */
  36.  
  37. boolean isAuthenticated = conn.authenticateWithPassword(username, password);
  38.  
  39. if (isAuthenticated == false)
  40. throw new IOException("Authentication failed.");
  41.  
  42. /* Create a session */
  43.  
  44. Session sess = conn.openSession();
  45.  
  46. sess.execCommand("uname -a && date && uptime && who");
  47.  
  48. System.out.println("Here is some information about the remote host:");
  49.  
  50. /*
  51.   * This basic example does not handle stderr, which is sometimes dangerous
  52.   * (please read the FAQ).
  53.   */
  54.  
  55. InputStream stdout = new StreamGobbler(sess.getStdout());
  56.  
  57.  
  58. while (true)
  59. {
  60. String line = br.readLine();
  61. if (line == null)
  62. break;
  63. System.out.println(line);
  64. }
  65.  
  66. /* Show exit status, if available (otherwise "null") */
  67.  
  68. System.out.println("ExitCode: " + sess.getExitStatus());
  69.  
  70. /* Close this session */
  71.  
  72. sess.close();
  73.  
  74. /* Close the connection */
  75.  
  76. conn.close();
  77.  
  78. }
  79. catch (IOException e)
  80. {
  81. e.printStackTrace(System.err);
  82. System.exit(2);
  83. }
  84. }
  85.  
  86.  
  87. public void tastendruck(){ // wird bei tasten druck aufgerufen
  88.  
  89. sess.execCommand("sudo etwas... ");
  90.  
  91. }
  92.  
  93.  
  94. public void tastendruck_2(){ // wird bei tasten2 druck aufgerufen
  95.  
  96. sess.execCommand("sudo etwas anderes.. ");
  97.  
  98. }
  99.  
  100.  
  101. }



Ich hoffe ihr könnt mir helfen!

Vielen Dank! :)


SSH sessions

0 commentaires:

Enregistrer un commentaire