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

API standards et tierces Java Discussion :

Exécution commande Telnet sous Java


Sujet :

API standards et tierces Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2011
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2011
    Messages : 16
    Par défaut Exécution commande Telnet sous Java
    Bonjour,
    Je dois écrire un programme qui doit ouvrir une session TELNET ( ID+PASSWORD) suite a cela je dois exécuter une commande qui va m'aider à créer un fichier que je récupére après via FTP.
    Le problème qui se pose devant est que j'arrive a ouvrir une session TELNET mais je n'arrive pas a envoyer la commande.
    Je vous remercie

  2. #2
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    on peux voir votre code?

  3. #3
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2011
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2011
    Messages : 16
    Par défaut
    Voila la méthode
    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
    void accesEquipement(String ip, String user, String password,
    			String equipement) throws SocketException, IOException {
    		TelnetConnection telnet = new TelnetConnection(ip, user, password);
    		telnet.sendCommand("ovtopodump -l "+equipement+" > /home/qualite/equipement.out");
    		telnet.disconnect();
     
    		System.out.println("FTP en cours ...");
     
    		FTPClient ftp = new FTPClient();
    		FileOutputStream fos = null;
     
    		ftp.connect("10.128.102.70", 21);
    		ftp.login("qualite", "qualite");
     
    		String filename = "equipement.out";
    		fos = new FileOutputStream(filename);
     
    		ftp.retrieveFile(filename, fos);
    		ftp.disconnect();
    		System.out.println("FTP fini ...");
    En haut, la partie TELNET sert a exécuter une commande qui crée un fichier .out, la partie FTP sert a récupérer ce fichier. Le problème se pose seulement dans la partie TELNET.

    Ici la classe TelnetConnection que j'ai trouvé sur internet :
    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
    package oss;
     
    import java.io.InputStream;
    import java.io.PrintStream;
     
    import org.apache.commons.net.telnet.TelnetClient;
     
    public class TelnetConnection {
     
    	private TelnetClient telnet = new TelnetClient();
    	  private InputStream in;
    	  private PrintStream out;
    	  private char prompt = '$';
     
    	  public TelnetConnection( 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 String readUntil( String pattern ) {
    	   try {
    		 char lastChar = pattern.charAt( pattern.length() - 1 );
    		 StringBuffer sb = new StringBuffer();
    		 @SuppressWarnings("unused")
    		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 ) ) {
    			 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();
    		 System.out.println( value );
    	   }
    	   catch( Exception e ) {
    		 e.printStackTrace();
    	   }
    	  }
     
    	  public void sendCommand( String command ) {
    	   try {
    		 write( command );
    		 //String ok ="Ok !!";
    		 readUntil( prompt +"" );
    		 //return ok;
    	   }
    	   catch( Exception e ) {
    		 e.printStackTrace();
    	   }
    	  }
     
    	  public void disconnect() {
    	   try {
    		 telnet.disconnect();
    	   }
    	   catch( Exception e ) {
    		 e.printStackTrace();
    	   }
    	  }
    	}
    Je pense que ça bloque au niveau de la méthode "sendCommand()" car une fois qu'elle s’exécute mon programme reste a attendre une seconde entrée ...

  4. #4
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Vous avez quoi comme sortie?

  5. #5
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2011
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2011
    Messages : 16
    Par défaut
    Comme sortie je récupère le fichier crée par la commande TELNET

  6. #6
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    non vous obtenez quoi au moment du blocage (puisque votre application affiche au fur et à mesure ce qui se passe dans les tuyaux)

Discussions similaires

  1. Commande linux sous Java !
    Par Arcann dans le forum Général Java
    Réponses: 6
    Dernier message: 15/06/2009, 09h55
  2. exécute des commande unix sous java
    Par hbar01 dans le forum Général Java
    Réponses: 6
    Dernier message: 04/11/2008, 20h33
  3. Excécuter une commande Dos sous Java
    Par mina86 dans le forum Général Java
    Réponses: 2
    Dernier message: 31/05/2008, 12h01
  4. commande dos sous java
    Par yann999 dans le forum API standards et tierces
    Réponses: 6
    Dernier message: 12/05/2006, 14h18
  5. Réponses: 5
    Dernier message: 19/04/2005, 08h50

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