Connecter à un switch via un code java

inama1 Messages postés 10 Date d'inscription lundi 26 mars 2012 Statut Membre Dernière intervention 18 mai 2012 - 18 mai 2012 à 09:18
cs_laurent1024 Messages postés 987 Date d'inscription mardi 31 mai 2005 Statut Membre Dernière intervention 30 août 2012 - 18 mai 2012 à 14:34
Bonjour,
j'ai réussi à connecter à un switch , mais il y a un problème dans l'exécution de ma classe qui limite le bon fonctionnement de mon projet( dans la coté gauche et au dessus de netbeans le Running reste bloqué ).
J’espère que vous avez compris ma demande.
Voici mon code , aidez moi à trouver l'erreur et merci pour votre collaboration :)
package bean;

/**
* Class represents the router i/f
* in other words : handles router interaction
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import org.apache.commons.net.telnet.*;
import java.io.*;
import java.io.PrintStream;
import java.io.InputStream;
import java.io.OutputStream;

public final class ping
{

public ping ()
{}
private static String command1="terminal length 0",command2="ping 172.22.152.60";
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;

private char prompt = '$';

public ping( 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();
//BufferedWriter fop = new BufferedWriter(new FileWriter("c:\\test.txt"));
//OutputStream output = new FileOutputStream("c:\\ping.txt");

//PrintWriter in = new PrintWriter( new FileWriter("c:\\ping.txt") );

//output.close();

out = new PrintStream( telnet.getOutputStream() );


// Log the user on
readUntil( "Username: " );
write( user );

//enter password
readUntil( "Password: " );
write( password );

//enter command
readUntil("DCN_SW_TUN9000_SRV#" );
write ( command1 );

//enter command
readUntil("DCN_SW_TUN9000_SRV#" );
write ( command2 );

PrintStream out = new PrintStream(new FileOutputStream("c:\\services\\GTB.txt"));
System.setOut(out);




//PrintStream out = new PrintStream(new FileOutputStream("c:\\Documents and Settings\\asma.tounsi.stg\\listcommand.txt"));
//System.setOut(out);
}
catch( Exception e ) {
}
}

public void su( String password ) {
try {
write( "su" );
readUntil( "Password: " );
write( password );
prompt = '#';
readUntil(" " );

}
catch( Exception e ) {
}
}

public String readUntil( String pattern ) {
try {
char lastChar = pattern.charAt( pattern.length() - 1 );
StringBuilder sb = new StringBuilder();
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 ) {
}
return null;
}

public void write( String value ) {
try {
out.println( value );
out.flush();
System.out.println( value );
}
catch( Exception e ) {
}
}

public String sendCommand( String command ) {
try {
write( command );
return readUntil( prompt + " " );
}
catch( Exception e ) {
}
return null;
}

public void disconnect() {
try {
telnet.disconnect();
}
catch( Exception e ) {
}
}

public static void main( String[] args ) {
try {
ping telnet = new ping( "172.22.152.60","stg","stg123" );
telnet.sendCommand("");
//telnet.su( "root-password" );
// Sring sendCommand = telnet.sendCommand(command1);

telnet.disconnect();

}
catch( Exception e ) {
}
}


}

1 réponse

cs_laurent1024 Messages postés 987 Date d'inscription mardi 31 mai 2005 Statut Membre Dernière intervention 30 août 2012 25
18 mai 2012 à 14:34
Bonjour.
Tu aurais pu utiliser la balise code, et mettre de l'indentation dans ton code, parce que là c'est pas évident à lire.
Dans tes catch, ajoute des printStackTrace(), ça permettra de voir s'il y a des exceptions
catch( Exception e ) {
    e.printStackTrace();
}


+
0
Rejoignez-nous