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 régulier
    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
    Points : 100
    Points
    100
    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 expérimenté Avatar de BainE
    Inscrit en
    Mai 2004
    Messages
    1 327
    Détails du profil
    Informations forums :
    Inscription : Mai 2004
    Messages : 1 327
    Points : 1 544
    Points
    1 544
    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 régulier
    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
    Points : 100
    Points
    100
    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
    Points : 3 736
    Points
    3 736
    Par défaut
    sinon, http://www.jcraft.com/jsch/ est ton ami

  5. #5
    Membre régulier
    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
    Points : 100
    Points
    100
    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 régulier
    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
    Points : 100
    Points
    100
    Par défaut
    De plus, qd je l'exécute, il ne se passe rien !!

  7. #7
    Membre régulier
    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
    Points : 100
    Points
    100
    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 régulier
    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
    Points : 100
    Points
    100
    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 régulier
    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
    Points : 100
    Points
    100
    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 régulier
    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
    Points : 100
    Points
    100
    Par défaut
    Juste pour info, l'adresse de la mainling list: http://lists.sourceforge.net/lists/listinfo/jsch-users

  11. #11
    Nouveau Candidat au Club
    Inscrit en
    Août 2006
    Messages
    1
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 1
    Points : 1
    Points
    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
    Débutant
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    496
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 496
    Points : 149
    Points
    149
    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
    Débutant
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    496
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 496
    Points : 149
    Points
    149
    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