1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
package com.bnpparibas.geode.tools.ssh;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.bnpparibas.geode.display.generic.DialogBox;
import com.bnpparibas.geode.tools.ApplicationResources;
import com.bnpparibas.geode.tools.config.ConfigLoader;
import com.trilead.ssh2.Connection;
import com.trilead.ssh2.Session;
import com.trilead.ssh2.StreamGobbler;
/**
* Classe de gestion de la connexion ssh
*
* @author Fabrice Calafat
*
*/
public class SshHandler {
private Connection connection;
private Session session;
private boolean isConnected = false;
private String host;
public SshHandler(String host) {
this.host = host;
}
/**
* Permet de lancer la connexion à Unix. Le mot de passe sera
* automatiquement demandé à l'utilisateur
*
*/
public boolean connect() {
try {
boolean keyboardInteractive = false;
connection = new Connection(host);
connection.connect();
String user = ConfigLoader.getInstance().getParameter("user",
"username");
String[] authMethods = connection.getRemainingAuthMethods(user);
for (int i = 0; i < authMethods.length; i++) {
if (authMethods[i].equals("keyboard-interactive"))
keyboardInteractive = true;
}
SshUserInfo callBack = new SshUserInfo();
callBack.setWindowTitle(user + "@" + host);
if (keyboardInteractive) {
isConnected = connection.authenticateWithKeyboardInteractive(
ConfigLoader.getInstance().getParameter("user",
"username"), callBack);
} else {
String[] response = callBack.replyToChallenge("", "", 1,
new String[] { "Enter SecurId passcode" }, null);
isConnected = connection.authenticateWithPassword(user,
response[0]);
}
if (isConnected)
System.out.println("Connected");
return isConnected;
} catch (Exception e) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
Shell shell = Display.getCurrent().getActiveShell();
DialogBox.errorMessage(shell, ApplicationResources
.getInstance().getParameter("SshHandler",
"connectionErrorTitle"),
ApplicationResources.getInstance().getParameter(
"SshHandler", "connectionErrorMessage"));
}
});
return false;
}
}
/**
* Récupère le statut de connexion
*
* @return : Statut de connexion
*/
public synchronized boolean isConnected() {
return isConnected;
}
/**
* Se déconnecte après confirmation de la part de l'utilisateur si confirm =
* true
*
* @param confirm :
* si true, demande confirmation, sinon déconnecte directement
*/
public synchronized boolean disconnect() {
connection.close();
System.out.println("Disconnected");
isConnected = false;
return true;
}
/**
* Envoie une commande Unix et récupère le résultat
*
* @param cmd :
* Commande Unix à effectuer
* @return StringBuffer contenant le résultat de la commande
*/
public synchronized StringBuffer sendCommand(String cmd) {
StringBuffer result = new StringBuffer();
try {
if (isConnected()) {
session = connection.openSession();
session.execCommand(cmd);
System.out.println("Unix Command Sent : " + cmd);
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(
stdout));
while (true) {
String line = br.readLine();
if (line == null)
break;
result.append(line + "\n");
}
}
//meme en utilisant des waitforcondition
//ou bien verifier le status de sortie de l execution avec getExitstatus
session.close();
} catch (IOException io) {
System.out.println("Error IO: " + io.getMessage());
isConnected = false;
}
if (result.length() > 0)
result.deleteCharAt(result.length() - 1);
return result;
}
} |
Partager