Java, J2SSh mit der AUTHORIZED_KEY Verbindung – Tutorial
Therapiesitzung vom Juni 14, 2009
•
Aktenschrank: Programmierung
•
0 Diagnosen
Gefällt mir
Ich habe sehr lange dafür gebraucht und in Anbetracht der mießen Dokumentation der J2SSH Klasse will ich allen, die das selbe Problem haben wie ich hier meine Klasse zur Verfügung stellen. Wichtig ist für AUTHORIZED_KEYS, das es sich hier um SSH1 !!! Handelt. Das kann mit dem Putty-Keygen erstellt werden (öffentlichen Schlüssel auf der Unix/Linuxkiste hinterlassen).
Hier die gesamte Verbindung:
ssh = new SshClient();
ssh.setSocketTimeout(1000);
SshConnectionProperties properties = new SshConnectionProperties();
properties.setHost(IP);
properties.setUsername(LoginName);
// Connect to the host
try {
// Mit Passwort:
// ssh.connect(properties);
// PasswordAuthenticationClient pk = new PasswordAuthenticationClient();
// pk.setUsername(s.LoginName);
// pk.setPassword(s.Passwort);
// Per SSH
HostKeyVerification ver = new SinkHostKeyVerification(IP);
ssh.connect(properties,ver);
PublicKeyAuthenticationClient pk = new PublicKeyAuthenticationClient();
pk.setUsername(LoginName);
// Private Key File (SSH 1)
File filename = new File("key_ppk.ppk");
SshPrivateKeyFile file = SshPrivateKeyFile.parse(filename);
SshPrivateKey key = file.toPrivateKey("keypass");
pk.setKey(key);
if (ssh.authenticate(pk) == AuthenticationProtocolState.COMPLETE) {
// The connection is authenticated we can now do some real work!
session = ssh.openSessionChannel();
if(!session.requestPseudoTerminal("vt100", 80, 24, 0, 0, ""))
System.out.println("Failed to allocate a pseudo terminal");
if (session.startShell()) {
IOStreamConnector error = new IOStreamConnector();
error.setCloseOutput(false);
error.connect(session.getStderrInputStream(), System.out);
conn.connect(getInputStream(), os);
this.isConnected = true;
start();
} else {
System.out.println("Failed to start the users shell");
ssh.disconnect();
}
}
} catch (InvalidStateException e) {} catch (IOException e) {}
Die Dick makierte Klasse “HostKeyVerification” sieht so aus:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sshtools.common.hosts.AbstractHostKeyVerification;
import com.sshtools.j2ssh.transport.InvalidHostFileException;
import com.sshtools.j2ssh.transport.TransportProtocolException;
/**
* @author dessard
*
*/
public class SinkHostKeyVerification extends AbstractHostKeyVerification {
/** to use log facility, just put in your code: log.info(\"...\"); */
@SuppressWarnings("unused")
static private Log log = LogFactory.getLog(SinkHostKeyVerification.class);
public SinkHostKeyVerification(String hostFileName)
throws InvalidHostFileException {
super(hostFileName);
}
public void onDeniedHost(String host) throws TransportProtocolException {
System.out.println("ERROR - Connexion host refusée sur : " + host);
}
public void onHostKeyMismatch(String host, String recordedFingerprint,
String actualFingerprint) throws TransportProtocolException {
// si le clef du host est inconnu, je l'autorise
allowHost(host, actualFingerprint, true);
}
public void onUnknownHost(String host, String fingerprint)
throws TransportProtocolException {
// si le host est inconnu, je l'autorise
allowHost(host, fingerprint, true);
}
}
So ich hoffe ich konnte damit jemandem helfen. Wenn ja bitte Hinterlasst doch mal eine Nachricht
Das ganze war übrigends für mein Abschlussprojekt. Wer sich gerne noch ein paar Code-Teile daraus ansehen möchte kann sich auch gerne melden.
