O
OceanDriver
Gast
Hallo,
Ich bin gerade dabei ein kleines Programm zu basteln, was ein paar Kommandos an einen ssh-server sendet.
Hier mal meine Verbindungsklasse:
Sehe ich es richtig, dass pro Kommando eine Session aufgemacht werden muss?
Jetzt habe ich aber das Problem, dass mir die execute-methode keinen String zurückliefert - was mache ich falsch? (es wird an der Stelle auch keine Exception ausgelöst)
Danke schonmal für die hilfe.
Ich bin gerade dabei ein kleines Programm zu basteln, was ein paar Kommandos an einen ssh-server sendet.
Hier mal meine Verbindungsklasse:
Code:
package Connection;
import com.trilead.ssh2.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class SSHConnection {
private String hostname="";
private String user="";
private String pass="";
private Connection sshconn = null;
private Session sshsess = null;
private static SSHConnection instance=null;
private SSHConnection(){}
public void setDbConAttributes(String host, String user, String pass)
{
this.hostname = host;
this.user = user;
this.pass = pass;
}
public static SSHConnection getInstance()
{
if(instance==null)
{
instance = new SSHConnection();
}
return instance;
}
public void laden() throws Exception
{
String pfad = new File("").getAbsolutePath();
pfad = pfad + "\\" + "connection_data.txt";
DataInputStream dis = new DataInputStream(new FileInputStream(pfad));
System.out.println("Lade Konfiguration aus : " + pfad);
String file = "";
while (dis.available() > 0) {
file = file + dis.readLine();
}
String[] parts = file.split(",");
//host, user, pass
this.setDbConAttributes(parts[0], parts[1], parts[2]);
dis.close();
}
public void speichern() throws Exception
{
String pfad = new File("").getAbsolutePath();
pfad = pfad + "\\" + "db_data.txt";
DataOutputStream dos = new DataOutputStream(new FileOutputStream(pfad));
String connectionInfo = "";
connectionInfo = this.hostname + "," + this.user + "," + this.pass;
dos.writeBytes(connectionInfo);
dos.close();
}
public boolean initConnection() throws Exception
{
laden();
sshconn = new Connection(hostname);
boolean isAuthenticated=false;
try
{
sshconn.connect();
isAuthenticated = sshconn.authenticateWithPassword(user, pass);
if(isAuthenticated == false)
throw new IOException("Authentication failed");
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "Verbindung konnte nicht hergestellt werden: " + e.getMessage(), "Verbindungsfehler", JOptionPane.ERROR_MESSAGE);
System.exit(2);
}
return isAuthenticated;
}
public void initSession()
{
if(sshsess != null)
sshsess.close();
try
{
sshsess = sshconn.openSession();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Session konnte nicht geöffnet werden: " +e.getMessage(), "Sessionfehler", JOptionPane.ERROR_MESSAGE);
}
}
public String execute(String command)
{
initSession();
String lines="";
try
{
sshsess.execCommand(command);
InputStream stdout = new StreamGobbler(sshsess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while(true)
{
if(lines==null)
break;
lines = lines + "\n" + br.readLine();
}
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "Kommando konnte nicht ausgeführt werden: " +e.getMessage(), "Command not found", JOptionPane.ERROR_MESSAGE);
}
System.out.println("Zeilen"+lines);
return lines;
}
}
Sehe ich es richtig, dass pro Kommando eine Session aufgemacht werden muss?
Jetzt habe ich aber das Problem, dass mir die execute-methode keinen String zurückliefert - was mache ich falsch? (es wird an der Stelle auch keine Exception ausgelöst)
Danke schonmal für die hilfe.