Salve ragazzi ho due classi che mi permettono di connettermi a un server su cui gira il demone ssh. Per connnettermi passo una stringa del tipo nomeutente@indirizzohost:numero porta. Quando effettuo la connessione e premo yes per poter inserire la password successivamente ho questo errore :
codice:
Are you sure to want to continue connecting?
yes
com.jcraft.jsch.JSchException : Auth fail
at com.jcraft.jsch.Session.connect<Session.java:461>
at com.jcraft.jsch.Session.connect<Session.java:154>
at SshClient.main<Sshclient.java:61>
La classe SshClient è la seguente :
codice:
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
public class SshClient {
public static void main(String[] args) {
try {
/**
* Verifico che la stringa di connessione sia corretta
*/
if (args.length == 0) {
System.out.println("Usage: user@host:port");
return;
}
if (!args[0].contains("@")) {
System.out.println("Usage: user@host:port");
}
/**
* Recupero utente, host e porta
*/
String user = args[0].substring(0, args[0].indexOf('@'));
String host = null;
/**
* La porta di default dell'ssh è 22
*/
int port = 22;
if (args[0].contains(":")) {
host = args[0].substring(args[0].indexOf('@') + 1, args[0].indexOf(":"));
port = Integer.parseInt(args[0].substring(args[0].indexOf(':') + 1));
} else {
host = args[0].substring(args[0].indexOf('@') + 1);
}
/**
* Inizializzo il framework e creo la session
*/
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
/**
* Setto la classe UserInfo per recupare la password dell'utente
*/
UserInfo ui = new MrWebmasterUserInfo();
session.setUserInfo(ui);
/**
* Eseguo la connessione della sessione
*/
session.connect();
/**
* Creo il channel shell
*/
ChannelShell channel = (ChannelShell) session.openChannel("shell");
/**
* Associo stdin e stdout
*/
channel.setInputStream(System.in);
/**
* per Windows
*
* channel.setInputStream(new FilterInputStream(System.in) { public
* int read(byte[] b, int off, int len) throws IOException { return
* in.read(b, off, (len > 1024 ? 1024 : len)); } });
*/
channel.setOutputStream(System.out);
/**
* Eseguo la connessione con un timeout di 3 secondi
*/
channel.connect(3000);
/**
* Controllo ogni secondo se il canale è stato chiuso
*/
while (true) {
if (channel.isClosed()) {
System.exit(channel.getExitStatus());
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} catch (JSchException e) {
e.printStackTrace();
}
}
}