Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1077 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Probleme Java + Telnet + Active Directory

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Probleme Java + Telnet + Active Directory

n°2108504
sebastien4​444
Posté le 28-10-2011 à 09:40:47  profilanswer
 

Bonjour,
 
Je dois réaliser une petite appli qui permet d'activer/desactiver un compte utilisateur sur l'Active Directory d'un serveur distant, avec un systeme d'indicateur qui montre si le compte est activé ou non (plus ou moins en temps réel).
 
En premier lieu, j'ouvrai une connexion Telnet puis la fermait à chaque action (Activation-Desactivation-Consultation de l'état) et bien que tout fonctionnait, je trouvai ça trop lourd (enfin surtout trop lent) pour ce qui est de la consultation de l'état qui doit se faire toutes les 2-3 secondes.
 
J'ai donc décidé d'ouvrir une connexion Telnet au démarrage de l'appli et de ne la fermer qu'à la fermeture de celle-ci.
 
Seulement, quand je lance mes commandes DSMOD qui permettent l'activation et la désactivation : celà fonctionne impécable (compte bien activé/désactivé quand je vérifie sur l'AD) mais en ce qui concerne la commande DSGET (envoyée toutes les 3 secondes) qui me renvoi "yes" si le compte est désactivé et "non" si il est activé : elle me donne le bon statut au lancement de l'appli, mais continu à me renvoyer ce statut même après que celui-ci aie changé .
 
Voilà ce que j'obtiens dans la console Eclipse :
 
 
Welcome to Microsoft Telnet Service  
 
 
login: administrateur
 
password: *===============================================================                Bienvenue ? Microsoft Telnet Server.                                            *===============================================================                C:\Documents and Settings\Administrateur>                                       DSGET user "CN=test test, CN=Users, DC=CDGEPBAI, DC=ADP, DC=FR" -disableddisabledyesdsget r?ussiC:\Documents and Settings\Administrateur>JE SUIS FERME, yes au char :356
CLOSED
DSGET user "CN=test test, CN=Users, DC=CDGEPBAI, DC=ADP, DC=FR" -disableddisabledyesdsget r?ussiC:\Documents and Settings\Administrateur>JE SUIS FERME, yes au char :109
CLOSED
DSGET user "CN=test test, CN=Users, DC=CDGEPBAI, DC=ADP, DC=FR" -disableddisabledyesdsget r?ussiC:\Documents and Settings\Administrateur>JE SUIS FERME, yes au char :109
CLOSED
DSMOD user "CN=test test, CN=Users, DC=CDGEPBAI, DC=ADP, DC=FR" -disabled nodsmod r?ussi:CN=test test,CN=Users,DC=CDGEPBAI,DC=ADP,DC=FR
C:\Documents and Settings\Administrateur>OUVERTURE
C:\Documents and Settings\Administrateur>CLOSED
DSGET user "CN=test test, CN=Users, DC=CDGEPBAI, DC=ADP, DC=FR" -disabled                                disabled                                                                        yes                             dsget r?ussiC:\Documents and Settings\Administrateur>JE SUIS FERME, yes au char :203
CLOSED
DSGET user "CN=test test, CN=Users, DC=CDGEPBAI, DC=ADP, DC=FR" -disabled  disabled                                                                        yes                             dsget r?ussiC:\Documents and Settings\Administrateur>JE SUIS FERME, yes au char :174
CLOSED
DSGET user "CN=test test, CN=Users, DC=CDGEPBAI, DC=ADP, DC=FR" -disabled  disabled                                                                        yes                             dsget r?ussiC:\Documents and Settings\Administrateur>JE SUIS FERME, yes au char :176
CLOSED
 
 
En rouge : le statut qui me dis que le compte est désactivé (disabled yes)
En vert : la commande d'activation qui réussi (vérif sur l'Active Directory, le compte est bien passé actif)
 
On voit bien que le DSGET me renvoi toujours yes après l'activation du compte ALORS QUE si au même moment, j'ouvre un invite de commande, me connecte en telnet et envoi la même commande, celle-ci me renvoi le bon statut,c'est à dire "disabled no".
 
Quelqu'un aurai une idée de ce qui pose problème ?
 
[PS : je vais publier les class de mon programme dans la suite du topic]
 
 

mood
Publicité
Posté le 28-10-2011 à 09:40:47  profilanswer
 

n°2108507
sebastien4​444
Posté le 28-10-2011 à 09:58:56  profilanswer
 

Class TelnetConnexion qui gère la connexion Telnet et l'envoi de commances :
 
import java.io.InputStream;
import java.io.PrintStream;
 
import org.apache.commons.net.telnet.TelnetClient;
 
 
public class TelnetConnexion
{
  private TelnetClient telnet = new TelnetClient();
  private InputStream in;
  private PrintStream out;
  private char prompt = '>';
 
  public TelnetConnexion( String server, String user, String password ) {
   try {
  // Connect to the specified server
  telnet.connect( server, 23 );
 
  // Get input and output stream references
  in = telnet.getInputStream();
  out = new PrintStream( telnet.getOutputStream() );
 
  // Log the user on
  readUntil( "login: " );
  write( user );
  readUntil( "password: " );
  write( password );
   
 
  // Advance to a prompt
  readUntil( prompt + "" );
   }
   catch( Exception e ) {
  e.printStackTrace();
   }
  }
 
 /* public void su( String password ) {
    try {
      write( "su" );
      readUntil( "Password: " );
      write( password );
      prompt = '#';
      readUntil( prompt + "" );
    }
    catch( Exception e ) {
      e.printStackTrace();
    }
  }*/
 
  public String readUntil( String pattern ) {
   try {
  char lastChar = pattern.charAt( pattern.length() - 1 );
  StringBuffer sb = new StringBuffer();
 // boolean found = false;
  char ch = ( char )in.read();
  while( true ) {
   System.out.print( ch );
   sb.append( ch );
   if( ch == lastChar ) {
     if( sb.toString().endsWith( pattern ) ) {
      //System.out.println( sb.toString() );
   return sb.toString();
     }
   }
   ch = ( char )in.read();
  }
   }
   catch( Exception e ) {
  e.printStackTrace();
   }
   return null;
  }
 
  public void write( String value ) {
   try {
  out.println( value );
  out.flush();
   }
   catch( Exception e ) {
  e.printStackTrace();
   }
  }
 
  public String sendCommand( String command ) {
   try {
  write( command );
  return readUntil( prompt + "" );
   }
   catch( Exception e ) {
  e.printStackTrace();
   }
   return null;
  }
 
  public void disconnect() {
   try {
  telnet.disconnect();
   }
   catch( Exception e ) {
  e.printStackTrace();
   }
  }
}

Class GestionAcces qui instancie la connexion telnet et défini les fonctions necessaire à activation-desactivation-consultation :

 
public class GestionAcces {
 
 public TelnetConnexion telnet;
 public boolean b= false;
 
 public GestionAcces(TelnetConnexion t){
  telnet = t;
 }
 
 public void activer(){
  telnet.sendCommand( "DSMOD user \"CN=test test, CN=Users, DC=CDGEPBAI, DC=ADP, DC=FR\" -disabled no" );
 }
 
 public void desactiver(){
  telnet.sendCommand( "DSMOD user \"CN=test test, CN=Users, DC=CDGEPBAI, DC=ADP, DC=FR\" -disabled yes" );
 }
 
 public void estActiver(){
  String s="";
  s = telnet.sendCommand( "DSGET user \"CN=test test, CN=Users, DC=CDGEPBAI, DC=ADP, DC=FR\" -disabled" );
  if(s.indexOf("yes" )!= -1 && s.indexOf("ussi" ) != -1){
   b=false;
   System.out.println("JE SUIS FERME, yes au char :"+s.indexOf("yes" ));
  }else if(s.indexOf("no" )!= -1 && s.indexOf("ussi" ) != -1){
   b=true;
   System.out.println("JE SUIS OUVERT, no au char :"+s.indexOf("no" ));
  }
 }  
}
 
Class EtatAcces qui permet de gérer l'affichage (voyant + bon bouton radio sélectionné) en fonction de l'état du compte :
 
import java.awt.Color;
import java.awt.Graphics;
 
import javax.swing.JPanel;
 
 
public class EtatAcces extends JPanel{
 
 boolean etatAcces;
 
 public void paintComponent(Graphics g){
  g.setColor(Color.white);
  g.fillRect(0, 0, this.getWidth(), this.getHeight());
  if(etatAcces){
   g.setColor(Color.GREEN);
  }else{
   g.setColor(Color.RED);
  }
  g.fillRect(0,0,this.getWidth(),this.getHeight());
 }
 
 public void setEtat(boolean b){
  etatAcces = b;
 }
 
}
 
Class GestionInterface pour l'interface graphique et l'action des boutons :
 
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
 
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
 
 
public class GestionInterface extends JFrame implements WindowListener{
 
 TelnetConnexion telnet = new TelnetConnexion( "192.9.200.6", "administrateur", "********" );
 
 EtatAcces etat = new EtatAcces();
 
 private JRadioButton jr1 = new JRadioButton("Ouvert" );
 private JRadioButton jr2 = new JRadioButton("Fermé" );
 
 private ButtonGroup bg = new ButtonGroup();
 
 JPanel choix = new JPanel();
 JLabel label1 = new JLabel("Texte_iciiiii" );
 
 GestionAcces acces = new GestionAcces(telnet);
 
 public GestionInterface(){
   
  bg.add(jr1);
  bg.add(jr2);
   
  choix.add(label1);
  choix.add(jr1);
  choix.add(jr2);
  choix.add(etat);
   
  jr1.addActionListener(new OuvrirListener());
  jr2.addActionListener(new FermerListener());
   
  this.setTitle("Activation VPN" );
  this.setSize(280,200);
  this.setResizable(false);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setLocationRelativeTo(null);
  this.setContentPane(choix);
  this.setVisible(true);
 
  etatRefresh();
 }
 
 class OuvrirListener implements ActionListener{
  public void actionPerformed(ActionEvent e){
   envoiCommande(1);
   System.out.println("OUVERTURE" );
   try{
    Thread.sleep(1000);
   }catch(InterruptedException e1){
    e1.printStackTrace();
   }
  }
 }
 
 class FermerListener implements ActionListener{
  public void actionPerformed(ActionEvent e){
   envoiCommande(2);
   System.out.println("FERMETURE" );
   try{
    Thread.sleep(1000);
   }catch(InterruptedException e2){
    e2.printStackTrace();
   }
  }
 }
 
 private void etatRefresh(){
  for(int i=1;i>0;i++){
   //if(acces.estActiver()){
   envoiCommande(3);
   if(acces.b){
    etat.setEtat(true);
    jr1.setSelected(true);
    jr2.setSelected(false);
    System.out.println("OPEN" );
   }else{
    etat.setEtat(false);
    jr2.setSelected(true);
    jr1.setSelected(false);
    System.out.println("CLOSED" );
   }
   etat.repaint();
   try{
    Thread.sleep(5000);
   }catch(InterruptedException e){
    e.printStackTrace();
   }
  }
 }
 
 public synchronized void envoiCommande(int n){
  if(n==1){
   acces.activer();
  }
  if(n==2){
   acces.desactiver();
  }
  if(n==3){
   acces.estActiver();
  }
 }
 
 public void windowActivated(WindowEvent arg0) {
  // TODO Auto-generated method stub
   
 }
 
 public void windowClosed(WindowEvent e) {
  telnet.disconnect();
   
 }
 
 public void windowClosing(WindowEvent e) {
  // TODO Auto-generated method stub
   
 }
 
 public void windowDeactivated(WindowEvent e) {
  // TODO Auto-generated method stub
   
 }
 
 public void windowDeiconified(WindowEvent e) {
  // TODO Auto-generated method stub
   
 }
 
 public void windowIconified(WindowEvent e) {
  // TODO Auto-generated method stub
   
 }
 
 public void windowOpened(WindowEvent e) {
  // TODO Auto-generated method stub
   
 }
 
 public static void main(String[] args){
  GestionInterface gi = new GestionInterface();
 }
 
}


Message édité par sebastien4444 le 28-10-2011 à 13:19:48
n°2108664
phnatomass
Je m'empare de ton esprit !!
Posté le 29-10-2011 à 10:56:21  profilanswer
 

Utilise la balise cpp
Class TelnetConnexion qui gère la connexion Telnet et l'envoi de commances :

Code :
  1. import java.io.InputStream;
  2. import java.io.PrintStream;
  3. import org.apache.commons.net.telnet.TelnetClient;
  4. public class TelnetConnexion
  5. {
  6.   private TelnetClient telnet = new TelnetClient();
  7.   private InputStream in;
  8.   private PrintStream out;
  9.   private char prompt = '>';
  10.   public TelnetConnexion( String server, String user, String password ) {
  11.    try {
  12.  // Connect to the specified server
  13.  telnet.connect( server, 23 );
  14.  // Get input and output stream references
  15.  in = telnet.getInputStream();
  16.  out = new PrintStream( telnet.getOutputStream() );
  17.  // Log the user on
  18.  readUntil( "login: " );
  19.  write( user );
  20.  readUntil( "password: " );
  21.  write( password );
  22.  // Advance to a prompt
  23.  readUntil( prompt + "" );
  24.    }
  25.    catch( Exception e ) {
  26.  e.printStackTrace();
  27.    }
  28.   }
  29. /* public void su( String password ) {
  30.     try {
  31.       write( "su" );
  32.       readUntil( "Password: " );
  33.       write( password );
  34.       prompt = '#';
  35.       readUntil( prompt + "" );
  36.     }
  37.     catch( Exception e ) {
  38.       e.printStackTrace();
  39.     }
  40.   }*/
  41.   public String readUntil( String pattern ) {
  42.    try {
  43.  char lastChar = pattern.charAt( pattern.length() - 1 );
  44.  StringBuffer sb = new StringBuffer();
  45. // boolean found = false;
  46.  char ch = ( char )in.read();
  47.  while( true ) {
  48.   System.out.print( ch );
  49.   sb.append( ch );
  50.   if( ch == lastChar ) {
  51.     if( sb.toString().endsWith( pattern ) ) {
  52.      //System.out.println( sb.toString() );
  53.   return sb.toString();
  54.     }
  55.   }
  56.   ch = ( char )in.read();
  57.  }
  58.    }
  59.    catch( Exception e ) {
  60.  e.printStackTrace();
  61.    }
  62.    return null;
  63.   }
  64.   public void write( String value ) {
  65.    try {
  66.  out.println( value );
  67.  out.flush();
  68.    }
  69.    catch( Exception e ) {
  70.  e.printStackTrace();
  71.    }
  72.   }
  73.   public String sendCommand( String command ) {
  74.    try {
  75.  write( command );
  76.  return readUntil( prompt + "" );
  77.    }
  78.    catch( Exception e ) {
  79.  e.printStackTrace();
  80.    }
  81.    return null;
  82.   }
  83.   public void disconnect() {
  84.    try {
  85.  telnet.disconnect();
  86.    }
  87.    catch( Exception e ) {
  88.  e.printStackTrace();
  89.    }
  90.   }
  91. }
  92. Class GestionAcces qui instancie la connexion telnet et défini les fonctions necessaire à activation-desactivation-consultation :
  93. public class GestionAcces {
  94. public TelnetConnexion telnet;
  95. public boolean b= false;
  96. public GestionAcces(TelnetConnexion t){
  97.  telnet = t;
  98. }
  99. public void activer(){
  100.  telnet.sendCommand( "DSMOD user \"CN=test test, CN=Users, DC=CDGEPBAI, DC=ADP, DC=FR\" -disabled no" );
  101. }
  102. public void desactiver(){
  103.  telnet.sendCommand( "DSMOD user \"CN=test test, CN=Users, DC=CDGEPBAI, DC=ADP, DC=FR\" -disabled yes" );
  104. }
  105. public void estActiver(){
  106.  String s="";
  107.  s = telnet.sendCommand( "DSGET user \"CN=test test, CN=Users, DC=CDGEPBAI, DC=ADP, DC=FR\" -disabled" );
  108.  if(s.indexOf("yes" )!= -1 && s.indexOf("ussi" ) != -1){
  109.   b=false;
  110.   System.out.println("JE SUIS FERME, yes au char :"+s.indexOf("yes" ));
  111.  }else if(s.indexOf("no" )!= -1 && s.indexOf("ussi" ) != -1){
  112.   b=true;
  113.   System.out.println("JE SUIS OUVERT, no au char :"+s.indexOf("no" ));
  114.  }
  115. }
  116. }
  117. Class EtatAcces qui permet de gérer l'affichage (voyant + bon bouton radio sélectionné) en fonction de l'état du compte :
  118. import java.awt.Color;
  119. import java.awt.Graphics;
  120. import javax.swing.JPanel;
  121. public class EtatAcces extends JPanel{
  122. boolean etatAcces;
  123. public void paintComponent(Graphics g){
  124.  g.setColor(Color.white);
  125.  g.fillRect(0, 0, this.getWidth(), this.getHeight());
  126.  if(etatAcces){
  127.   g.setColor(Color.GREEN);
  128.  }else{
  129.   g.setColor(Color.RED);
  130.  }
  131.  g.fillRect(0,0,this.getWidth(),this.getHeight());
  132. }
  133. public void setEtat(boolean b){
  134.  etatAcces = b;
  135. }
  136. }
  137. Class GestionInterface pour l'interface graphique et l'action des boutons :
  138. import java.awt.event.ActionEvent;
  139. import java.awt.event.ActionListener;
  140. import java.awt.event.WindowEvent;
  141. import java.awt.event.WindowListener;
  142. import javax.swing.ButtonGroup;
  143. import javax.swing.JFrame;
  144. import javax.swing.JLabel;
  145. import javax.swing.JPanel;
  146. import javax.swing.JRadioButton;
  147. public class GestionInterface extends JFrame implements WindowListener{
  148. TelnetConnexion telnet = new TelnetConnexion( "192.9.200.6", "administrateur", "********" );
  149. EtatAcces etat = new EtatAcces();
  150. private JRadioButton jr1 = new JRadioButton("Ouvert" );
  151. private JRadioButton jr2 = new JRadioButton("Fermé" );
  152. private ButtonGroup bg = new ButtonGroup();
  153. JPanel choix = new JPanel();
  154. JLabel label1 = new JLabel("Texte_iciiiii" );
  155. GestionAcces acces = new GestionAcces(telnet);
  156. public GestionInterface(){
  157.  bg.add(jr1);
  158.  bg.add(jr2);
  159.  choix.add(label1);
  160.  choix.add(jr1);
  161.  choix.add(jr2);
  162.  choix.add(etat);
  163.  jr1.addActionListener(new OuvrirListener());
  164.  jr2.addActionListener(new FermerListener());
  165.  this.setTitle("Activation VPN" );
  166.  this.setSize(280,200);
  167.  this.setResizable(false);
  168.  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  169.  this.setLocationRelativeTo(null);
  170.  this.setContentPane(choix);
  171.  this.setVisible(true);
  172.  etatRefresh();
  173. }
  174. class OuvrirListener implements ActionListener{
  175.  public void actionPerformed(ActionEvent e){
  176.   envoiCommande(1);
  177.   System.out.println("OUVERTURE" );
  178.   try{
  179.    Thread.sleep(1000);
  180.   }catch(InterruptedException e1){
  181.    e1.printStackTrace();
  182.   }
  183.  }
  184. }
  185. class FermerListener implements ActionListener{
  186.  public void actionPerformed(ActionEvent e){
  187.   envoiCommande(2);
  188.   System.out.println("FERMETURE" );
  189.   try{
  190.    Thread.sleep(1000);
  191.   }catch(InterruptedException e2){
  192.    e2.printStackTrace();
  193.   }
  194.  }
  195. }
  196. private void etatRefresh(){
  197.  for(int i=1;i>0;i++){
  198.   //if(acces.estActiver()){
  199.   envoiCommande(3);
  200.   if(acces.b){
  201.    etat.setEtat(true);
  202.    jr1.setSelected(true);
  203.    jr2.setSelected(false);
  204.    System.out.println("OPEN" );
  205.   }else{
  206.    etat.setEtat(false);
  207.    jr2.setSelected(true);
  208.    jr1.setSelected(false);
  209.    System.out.println("CLOSED" );
  210.   }
  211.   etat.repaint();
  212.   try{
  213.    Thread.sleep(5000);
  214.   }catch(InterruptedException e){
  215.    e.printStackTrace();
  216.   }
  217.  }
  218. }
  219. public synchronized void envoiCommande(int n){
  220.  if(n==1){
  221.   acces.activer();
  222.  }
  223.  if(n==2){
  224.   acces.desactiver();
  225.  }
  226.  if(n==3){
  227.   acces.estActiver();
  228.  }
  229. }
  230. public void windowActivated(WindowEvent arg0) {
  231.  // TODO Auto-generated method stub
  232. }
  233. public void windowClosed(WindowEvent e) {
  234.  telnet.disconnect();
  235. }
  236. public void windowClosing(WindowEvent e) {
  237.  // TODO Auto-generated method stub
  238. }
  239. public void windowDeactivated(WindowEvent e) {
  240.  // TODO Auto-generated method stub
  241. }
  242. public void windowDeiconified(WindowEvent e) {
  243.  // TODO Auto-generated method stub
  244. }
  245. public void windowIconified(WindowEvent e) {
  246.  // TODO Auto-generated method stub
  247. }
  248. public void windowOpened(WindowEvent e) {
  249.  // TODO Auto-generated method stub
  250. }
  251. public static void main(String[] args){
  252.  GestionInterface gi = new GestionInterface();
  253. }
  254. }


n°2108840
sebastien4​444
Posté le 31-10-2011 à 08:31:15  profilanswer
 

C'est noté.
A part ça, une idée pour mon problème ?

n°2109202
sebastien4​444
Posté le 02-11-2011 à 08:57:35  profilanswer
 

Mais encore ? :D


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Probleme Java + Telnet + Active Directory

 

Sujets relatifs
Python + Qt + OpenCV = problème :/Création d'un livre d'or en PHP : problème de débutant...
Problème avec Dewplayer et Blogspotprobléme de synthaxe sur vlookup
Problème importation XamppProblème avec confirm() et AJAX
utilisation de class Telnet sous EclipseProblème formulaire ["undefined"]
problème pour ajouter un nombre à une variable (heure UTC et CEST) 
Plus de sujets relatifs à : Probleme Java + Telnet + Active Directory


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR