IdentifiantMot de passe
Loading...
Mot de passe oubli� ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les r�ponses en temps r�el, voter pour les messages, poser vos propres questions et recevoir la newsletter

S�curit� Java Discussion :

Utiliser ssh dans une application java


Sujet :

S�curit� Java

  1. #1
    Membre tr�s actif
    Profil pro
    D�veloppeur informatique
    Inscrit en
    Janvier 2005
    Messages
    153
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activit� : D�veloppeur informatique
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Janvier 2005
    Messages : 153
    Par d�faut Utiliser ssh dans une application java
    Bonjour,

    Je programme une application java sous windows et mon application doit ex�cuter un script sous unix: elle doit acc�der � un serveur unix et ex�cuter ce script et je voudrais savoir comment pourrais-je m'y prendre.

    je suis en train de lire de la doc sur ssh car c'est le protocole qu'il me faut. Mais comment utiliser ce protocole dans une appli java???

    Merci

  2. #2
    Membre �prouv� Avatar de BainE
    Inscrit en
    Mai 2004
    Messages
    1 327
    D�tails du profil
    Informations forums :
    Inscription : Mai 2004
    Messages : 1 327
    Par d�faut
    Bonjour, j ai developp� une fois une appli qui executait des commande shell
    type mkdir, find ...

    et pour ca on utilisait la classe Runtime (classe statique) qui possede une methode runProcess ou un truc du genre...

    si ca peu t aid�...

  3. #3
    Membre tr�s actif
    Profil pro
    D�veloppeur informatique
    Inscrit en
    Janvier 2005
    Messages
    153
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activit� : D�veloppeur informatique
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Janvier 2005
    Messages : 153
    Par d�faut
    ah oui �a me dit quelque chose merci!
    je vais essayer.

  4. #4
    R�dacteur
    Avatar de lunatix
    Homme Profil pro
    Architecte technique
    Inscrit en
    Novembre 2002
    Messages
    1 960
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (�le de France)

    Informations professionnelles :
    Activit� : Architecte technique

    Informations forums :
    Inscription : Novembre 2002
    Messages : 1 960

  5. #5
    Membre tr�s actif
    Profil pro
    D�veloppeur informatique
    Inscrit en
    Janvier 2005
    Messages
    153
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activit� : D�veloppeur informatique
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Janvier 2005
    Messages : 153
    Par d�faut
    Merci pour le lien, j'ai pu t�l�charger jsch-0.1.20, et j'ai g�n�r� le .jar
    Dans les exemples il y a la classe Shell qui est comme suit:
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    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
     
    import com.jcraft.jsch.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    import java.io.*;
     
    public class Shell{
      public static void main(String[] arg){
     
        try{
          JSch jsch=new JSch();
     
          //jsch.setKnownHosts("/home/foo/.ssh/known_hosts");
     
          String host=JOptionPane.showInputDialog("Enter username@hostname",				      System.getProperty("user.name")+
    		 "@localhost"); 
     
          String user=host.substring(0, host.indexOf('@'));
          host=host.substring(host.indexOf('@')+1);
     
          Session session=jsch.getSession(user, host, 22);
          //session.setPassword("your password");
     
          // username and password will be given via UserInfo interface.
          UserInfo ui=new MyUserInfo();
          session.setUserInfo(ui);
     
          //java.util.Hashtable config=new java.util.Hashtable();
          //config.put("StrictHostKeyChecking", "no");
          //session.setConfig(config);
     
     
        session.connect();
     
             Channel channel=session.openChannel("shell");
     
             channel.setInputStream(System.in);
             channel.setOutputStream(System.out);
     
             channel.connect();
           }
           catch(Exception e){
             System.out.println(e);
           }
         }
     
      public static class MyUserInfo implements UserInfo{
        public String getPassword(){ return passwd; }
        public boolean promptYesNo(String str){
          Object[] options={ "yes", "no" };
          int foo=JOptionPane.showOptionDialog(null, 
                 str,
                 "Warning", 
                 JOptionPane.DEFAULT_OPTION, 
                 JOptionPane.WARNING_MESSAGE,
                 null, options, options[0]);
           return foo==0;
        }
     
        String passwd;
        JTextField passwordField=(JTextField)new JPasswordField(20);
     
        public String getPassphrase(){ return null; }
        public boolean promptPassphrase(String message){ return true; }
        public boolean promptPassword(String message){
          Object[] ob={passwordField}; 
          int result=
    	  JOptionPane.showConfirmDialog(null, ob, message,
    					JOptionPane.OK_CANCEL_OPTION);
          if(result==JOptionPane.OK_OPTION){
    	passwd=passwordField.getText();
    	return true;
          }
          else{ return false; }
        }
        public void showMessage(String message){
          JOptionPane.showMessageDialog(null, message);
        }
      }
     
    }
    Le probl�me, c'est que je ne comprends pas ce que fait cette classe (je suis d�butante ) et si quelqu'un peut m'aider, ce serait gentil
    Merci!

  6. #6
    Membre tr�s actif
    Profil pro
    D�veloppeur informatique
    Inscrit en
    Janvier 2005
    Messages
    153
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activit� : D�veloppeur informatique
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Janvier 2005
    Messages : 153
    Par d�faut
    De plus, qd je l'ex�cute, il ne se passe rien !!

  7. #7
    Membre tr�s actif
    Profil pro
    D�veloppeur informatique
    Inscrit en
    Janvier 2005
    Messages
    153
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activit� : D�veloppeur informatique
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Janvier 2005
    Messages : 153
    Par d�faut
    Une seule m�thode main peut �tre appel�e par la JVM et c'�tait �a mon erreur, j'ai essay� d'ex�cuter cette classe qui contient main() dans un programme qui contient son main() et donc �a ne marche pas.
    J'ai donc remplac� le main() de la classe par un constructeur et �a a march�. Elle affiche une boite de dialogue permettant � l'utilisateur de saisir le username et le hostname :username@hostname
    et d'�tabir une connexion avec le serveur en question.
    Bon pour la connexion, �a a �chou�, mais je suis contente d'avoir r�solu la moiti� du probl�me!

  8. #8
    Membre tr�s actif
    Profil pro
    D�veloppeur informatique
    Inscrit en
    Janvier 2005
    Messages
    153
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activit� : D�veloppeur informatique
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Janvier 2005
    Messages : 153
    Par d�faut
    Le programme refuse de connecter au serveur et le message d'erreur est le suivant: The authenticity of host 'hostname' can't be established.
    DSA key fingerprint is ...

    Je ne sais vraiment pas quoi faire pour r�soudre le probl�me!

  9. #9
    Membre tr�s actif
    Profil pro
    D�veloppeur informatique
    Inscrit en
    Janvier 2005
    Messages
    153
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activit� : D�veloppeur informatique
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Janvier 2005
    Messages : 153
    Par d�faut
    Salut,

    J'ai pos� la question sur la mailing list de Jsch et voil� ce qu'il m'ont r�pondu:
    How about 'UserAuthKI.java' under 'examples'?
    I guess that your remote sshd does not support password authentication,
    but support the Keyboard-Interactive authentication to use
    password based on PAM.
    (Ne me demandez pas de traduire )

    La classe UserAuthKI.java est comme suit:
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    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
    import com.jcraft.jsch.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class UserAuthKI{
      public static void main(String[] arg){
     
        try{
          JSch jsch=new JSch();
     
          String host=JOptionPane.showInputDialog("Enter username@hostname",					      System.getProperty("user.name")+ "@localhost");
     
          String user=host.substring(0, host.indexOf('@'));
          host=host.substring(host.indexOf('@')+1);
     
          Session session=jsch.getSession(user, host, 22);
     
          // username and passphrase will be given via UserInfo interface.
          UserInfo ui=new MyUserInfo();
          session.setUserInfo(ui);
          session.connect();
     
          Channel channel=session.openChannel("shell");
     
          channel.setInputStream(System.in);
          channel.setOutputStream(System.out);
     
          channel.connect();
     
          //channel.
        }
        catch(Exception e){
          System.out.println(e);
        }
      }
     
     
      public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
        public String getPassword(){ return passwd; }
        public boolean promptYesNo(String str){
          Object[] options={ "yes", "no" };
          int foo=JOptionPane.showOptionDialog(null, 
                 str,
                 "Warning", 
                 JOptionPane.DEFAULT_OPTION, 
                 JOptionPane.WARNING_MESSAGE,
                 null, options, options[0]);
           return foo==0;
        }
     
        String passwd;
        JTextField passwordField=(JTextField)new JPasswordField(20);
     
        public String getPassphrase(){ return null; }
        public boolean promptPassphrase(String message){ return false; }
        public boolean promptPassword(String message){
          Object[] ob={passwordField}; 
          int result=
    	  JOptionPane.showConfirmDialog(null, ob, message,
    					JOptionPane.OK_CANCEL_OPTION);
          if(result==JOptionPane.OK_OPTION){
    	passwd=passwordField.getText();
    	return true;
          }
          else{ return false; }
        }
        public void showMessage(String message){
          JOptionPane.showMessageDialog(null, message);
        }
     
        final GridBagConstraints gbc = 
    	new GridBagConstraints(0,0,1,1,1,1,
    			       GridBagConstraints.NORTHWEST,
    			       GridBagConstraints.NONE,
    			       new Insets(0,0,0,0),0,0);
        private Container panel;
        public String[] promptKeyboardInteractive(String destination,
    					      String name,
    					      String instruction,
    					      String[] prompt,
    					      boolean[] echo){
          panel = new JPanel();
          panel.setLayout(new GridBagLayout());
     
          gbc.weightx = 1.0;
          gbc.gridwidth = GridBagConstraints.REMAINDER;
          gbc.gridx = 0;
          panel.add(new JLabel(instruction), gbc);
          gbc.gridy++;
     
          gbc.gridwidth = GridBagConstraints.RELATIVE;
     
          JTextField[] texts=new JTextField[prompt.length];
          for(int i=0; i<prompt.length; i++){
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.gridx = 0;
    	gbc.weightx = 1;
    	panel.add(new JLabel(prompt[i]),gbc);
     
    	gbc.gridx = 1;
    	gbc.fill = GridBagConstraints.HORIZONTAL;
    	gbc.weighty = 1;
    	if(echo[i]){
    	  texts[i]=new JTextField(20);
    	}
    	else{
    	  texts[i]=new JPasswordField(20);
    	}
    	panel.add(texts[i], gbc);
    	gbc.gridy++;
          }
     
          if(JOptionPane.showConfirmDialog(null, panel, 
    				       destination+": "+name,
    				       JOptionPane.OK_CANCEL_OPTION,
    				       JOptionPane.QUESTION_MESSAGE)
    	 ==JOptionPane.OK_OPTION){
    	String[] response=new String[prompt.length];
    	for(int i=0; i<prompt.length; i++){
    	  response[i]=texts[i].getText();
    	}
    	return response;
          }
          else{
    	return null;  // cancel
          }
        }
      }
    }
    et �a marche!

  10. #10
    Membre tr�s actif
    Profil pro
    D�veloppeur informatique
    Inscrit en
    Janvier 2005
    Messages
    153
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activit� : D�veloppeur informatique
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Janvier 2005
    Messages : 153

  11. #11
    Invit� de passage
    Inscrit en
    Ao�t 2006
    Messages
    1
    D�tails du profil
    Informations forums :
    Inscription : Ao�t 2006
    Messages : 1
    Par d�faut
    salut voila j'ai a peu pret le meme probleme que toi
    je veux acceder a un poste par une connexion ssh en java et je ne sais vraiment pas comment m'y prendre
    peux tu m'aider ?
    merci d'avance

  12. #12
    Membre �clair�
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    496
    D�tails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 496
    Par d�faut
    Citation Envoy� par heyhey
    salut voila j'ai a peu pret le meme probleme que toi
    je veux acceder a un poste par une connexion ssh en java et je ne sais vraiment pas comment m'y prendre
    peux tu m'aider ?
    merci d'avance
    as tu trouv� ta r�ponse ?

  13. #13
    Membre �clair�
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    496
    D�tails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 496
    Par d�faut
    Citation Envoy� par Samanta
    Salut,

    J'ai pos� la question sur la mailing list de Jsch et voil� ce qu'il m'ont r�pondu:
    (Ne me demandez pas de traduire )

    La classe UserAuthKI.java est comme suit:
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    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
    import com.jcraft.jsch.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class UserAuthKI{
      public static void main(String[] arg){
     
        try{
          JSch jsch=new JSch();
     
          String host=JOptionPane.showInputDialog("Enter username@hostname",                          System.getProperty("user.name")+ "@localhost");
     
          String user=host.substring(0, host.indexOf('@'));
          host=host.substring(host.indexOf('@')+1);
     
          Session session=jsch.getSession(user, host, 22);
     
          // username and passphrase will be given via UserInfo interface.
          UserInfo ui=new MyUserInfo();
          session.setUserInfo(ui);
          session.connect();
     
          Channel channel=session.openChannel("shell");
     
          channel.setInputStream(System.in);
          channel.setOutputStream(System.out);
     
          channel.connect();
     
          //channel.
        }
        catch(Exception e){
          System.out.println(e);
        }
      }
     
     
      public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
        public String getPassword(){ return passwd; }
        public boolean promptYesNo(String str){
          Object[] options={ "yes", "no" };
          int foo=JOptionPane.showOptionDialog(null, 
                 str,
                 "Warning", 
                 JOptionPane.DEFAULT_OPTION, 
                 JOptionPane.WARNING_MESSAGE,
                 null, options, options[0]);
           return foo==0;
        }
     
        String passwd;
        JTextField passwordField=(JTextField)new JPasswordField(20);
     
        public String getPassphrase(){ return null; }
        public boolean promptPassphrase(String message){ return false; }
        public boolean promptPassword(String message){
          Object[] ob={passwordField}; 
          int result=
          JOptionPane.showConfirmDialog(null, ob, message,
                        JOptionPane.OK_CANCEL_OPTION);
          if(result==JOptionPane.OK_OPTION){
        passwd=passwordField.getText();
        return true;
          }
          else{ return false; }
        }
        public void showMessage(String message){
          JOptionPane.showMessageDialog(null, message);
        }
     
        final GridBagConstraints gbc = 
        new GridBagConstraints(0,0,1,1,1,1,
                       GridBagConstraints.NORTHWEST,
                       GridBagConstraints.NONE,
                       new Insets(0,0,0,0),0,0);
        private Container panel;
        public String[] promptKeyboardInteractive(String destination,
                              String name,
                              String instruction,
                              String[] prompt,
                              boolean[] echo){
          panel = new JPanel();
          panel.setLayout(new GridBagLayout());
     
          gbc.weightx = 1.0;
          gbc.gridwidth = GridBagConstraints.REMAINDER;
          gbc.gridx = 0;
          panel.add(new JLabel(instruction), gbc);
          gbc.gridy++;
     
          gbc.gridwidth = GridBagConstraints.RELATIVE;
     
          JTextField[] texts=new JTextField[prompt.length];
          for(int i=0; i<prompt.length; i++){
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 0;
        gbc.weightx = 1;
        panel.add(new JLabel(prompt[i]),gbc);
     
        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weighty = 1;
        if(echo[i]){
          texts[i]=new JTextField(20);
        }
        else{
          texts[i]=new JPasswordField(20);
        }
        panel.add(texts[i], gbc);
        gbc.gridy++;
          }
     
          if(JOptionPane.showConfirmDialog(null, panel, 
                           destination+": "+name,
                           JOptionPane.OK_CANCEL_OPTION,
                           JOptionPane.QUESTION_MESSAGE)
         ==JOptionPane.OK_OPTION){
        String[] response=new String[prompt.length];
        for(int i=0; i<prompt.length; i++){
          response[i]=texts[i].getText();
        }
        return response;
          }
          else{
        return null;  // cancel
          }
        }
      }
    }
    et �a marche!
    As tu downloder quelques librairie ? car j'ai une erreur comme quoi la classe Jsch n'est pas reconnue.

+ R�pondre � la discussion
Cette discussion est r�solue.

Discussions similaires

  1. [SIFT] Utiliser SIFT dans une application Java
    Par mohamed11000 dans le forum G�n�ral Java
    R�ponses: 0
    Dernier message: 19/06/2013, 16h24
  2. R�ponses: 1
    Dernier message: 10/12/2012, 12h58
  3. [GATE] Utilisation de la libraire gate.jar dans une application Java
    Par Zarkk dans le forum API standards et tierces
    R�ponses: 1
    Dernier message: 15/04/2011, 14h07
  4. R�ponses: 0
    Dernier message: 17/06/2010, 14h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo