Bonjour,
Actuellement je développe un outil en JAVA, qui va me permettre d’envoyer via SSH des commandes Telnet à partir d’un poste distant. J’utilise l’API J2SSH/SSHTools qui me permet de me connecter en SSH. Sur ce point tout fonctionne, à part la base de mon programme qui est d’envoyer des commandes via le flux de données Output.
Deuxiemement, je n’arrive pas très bien à lire ce que je recois dans mon flux Input, étant donné que la fonction read() renvoie un tableau de int :s
Voici les liens des docs pour les différentes classes Output et Input :
ChannelOutputStream
ChannelInputStream
public static void run(String from, String to, String hostClient, String pool, String helo, String hostTarantel, String user, String pass) throws IOException
{
//C'est dans cette fonction que va se passer tout le mécanisme de connexion en SSH, et d'envoie du mail
ConfigurationLoader.initialize(false);
System.out.println("Host : " + hostTarantel + " Username : " + user + " Password : " + pass);
//On créer une connection SSH
SshClient ssh = new SshClient();
//On connecte le client ssh, avec l'ip de tarantella, et une clé
IgnoreHostKeyVerification verif = new IgnoreHostKeyVerification();
ssh.connect(hostTarantel, verif);
//On récupere les identifiants de connexion de tarantella
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername(user);
pwd.setPassword(pass);
//on regarde si tout c'est bien passer
int result = ssh.authenticate(pwd);
if(result == AuthenticationProtocolState.COMPLETE)// si oui, on peut ouvrir un channel
{
System.out.println("authentifation réussie");
ForwardingIOChannel channel = new ForwardingIOChannel(ForwardingIOChannel.LOCAL_FORWARDING_CHANNEL,"tarantella",hostTarantel,22,"myhost",22);
if(ssh.openChannel(channel))//si le channel c'est bien ouvert, on envoie les requetes au serveur tarantella
{
//toutes les commandes nécessaires sont ici
String [] commande = {"ssh " + pool, pass, "telnet " + hostClient + " 25", "helo " + helo, "mail from: " + from, "rcpt to: " + to, "data", "Ceci est un mail test de Orange Business Service, merci de ne pas en tenir compte.", ".", "quit"};
System.out.println("channel ouvert");
ChannelOutputStream out = channel.getOutputStream();
ChannelInputStream in = channel.getInputStream();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i = 0; i <commande.length; i++)
{
try{
Interface.Jbody.append(commande[i] + "\n");
System.out.println(commande[i]);
byte[] requete = commande[i].getBytes();
out.write(requete);
/*while(in.read() != -1){
System.out.println(in.read());}*/
System.out.println("i = " + i);
}catch (IOException e){System.out.println("erreur");};
}
channel.close();
}
else
System.out.println("Channel pas ouvert");
//ssh.close();
}//if
}//run()