Telnet + routeur + configuration

cs_ghofrane Messages postés 44 Date d'inscription mardi 18 juillet 2006 Statut Membre Dernière intervention 11 août 2009 - 24 nov. 2007 à 09:35
raniach Messages postés 3 Date d'inscription vendredi 20 mai 2011 Statut Membre Dernière intervention 30 mai 2011 - 20 mai 2011 à 11:55
Bonjour,
j'ai un programme qui ouvre une session telnet sur un routeur et accède au mode priviligé. ensuite j'exécute une commande show running-conf:

TelnetWrapper telnet = new TelnetWrapper();      
     
         //connect to the ipaddress        //
      try {
               telnet.connect("192.168.54.150", 23);
              telnet.login("password");
              System.out.println(telnet.send("en"));
              telnet.login("password");
              telnet.send("show running-config");
      } catch(java.io.IOException e) {
               e.printStackTrace();
            }
   

En exécutant cette commande show running-config d'après le terminal linux je dois appuyer sur entrée ou espace pour avancer et avoir tout le résultat.
je ne sais pas comment traduire ça en commande pour telnet?

Et aussi je veux récupérer le résultat de la commande dans un fichier texte ( c'est un peu le truc de > dans visual basic) ?

merci pour votre aide.

NB:j'ai pensé à enovyer le code ascii de la touche espace dans telnet send mais je ne sais pas comment faire ça aussi en java et surtout combien de fois suis je obliger d'envoyer ceci pour récupérer tt le résultat.

5 réponses

cs_rymoucha Messages postés 37 Date d'inscription jeudi 19 avril 2007 Statut Membre Dernière intervention 30 novembre 2009
20 mai 2008 à 19:08
bonjour,
moi aussi j'ai le meme probleme que vous, alors si vs avez pu resoudre le probleme, je serai contente de connaitre la solution
meeerci
0
tomamine10 Messages postés 1 Date d'inscription lundi 12 novembre 2007 Statut Membre Dernière intervention 21 avril 2010
21 avril 2010 à 01:06
package com.informit.commons;

import org.apache.commons.net.telnet.*;
import java.io.*;

public class TelnetSample
{
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private char prompt = '$';

public TelnetSample( 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 ) ) {
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 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();
}
}

public static void main( String[] args ) {
try {
TelnetSample telnet = new TelnetSample( "192.168.1.99",
"username",
"password" );
telnet.sendCommand( "cd /mydir/mysubdir" );
telnet.su( "root-password" );
telnet.sendCommand( "./restart.sh" );
telnet.disconnect();
}
catch( Exception e ) {
e.printStackTrace();
}
}
}
0
assiaasy Messages postés 6 Date d'inscription samedi 14 août 2010 Statut Membre Dernière intervention 24 août 2010
19 août 2010 à 00:34
bonsoir ghofrane,
je suis étudiante et je suis sur le point de réaliser une application java pour la configuration d'un routeur cisco sur windows XP, je seré reconnaissante si vous pouvez m'envoyer le code complet que vous avez réalisé ainsi que le nom du package car j'ai essayé de faire la même chose que vous mais j'arrive même pas à me connecter au routeur
0
raniach Messages postés 3 Date d'inscription vendredi 20 mai 2011 Statut Membre Dernière intervention 30 mai 2011
20 mai 2011 à 11:52
rania
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
raniach Messages postés 3 Date d'inscription vendredi 20 mai 2011 Statut Membre Dernière intervention 30 mai 2011
20 mai 2011 à 11:55
bonjour,
je suis une étudiante je suis sur le point de réaliser une application java pour la configuration d'un routeur cisco sur windows 7 mais j'ai rencontré un pb au niveau d’exécution de code et la connexion au routeur.
0
Rejoignez-nous