Methode class java

Résolu/Fermé
batbat52 Messages postés 118 Date d'inscription jeudi 11 mars 2010 Statut Membre Dernière intervention 21 mai 2012 - 23 juin 2011 à 11:23
batbat52 Messages postés 118 Date d'inscription jeudi 11 mars 2010 Statut Membre Dernière intervention 21 mai 2012 - 23 juin 2011 à 15:01
Bonjour,

Voila je créée une connexion telnet à un switch en java la connection fonctionne seulement quand j'appel les fonction disconnect et send message cela ne fonctionne pas.
A l'inverse si je supprime cette ligne readUntil(""+prompt+""); dans mon constructeur telnet alors cela fonctionne mais mon switch n'a pas l'air de voir la commande et donc ne renvoi pas de réponse contrairement a si j'insere la commande directement dans le constructeur

Voici mon code présent dans la classe adeline.java

public static void telnet()
	{principal.outstr("<html><head></head>",false);
		principal.outstr("<body>",false);
	telnet t;
	t = new telnet("10.247.112.233","log","pass");
		
		t.sendCommand("display current-configuration"); 
			t.deco();
		principal.outstr("</center></body></html>",true);
	}


Mon code présent dans telnet.java


class telnet{

	
	  static TelnetClient telneto = new TelnetClient();
	  static InputStream in;
	  static PrintStream out;
	  static char prompt = '$';
	
	  public telnet(String server, String user, String password) {
	   try {
		 // Connect to the specified server
		 telneto.connect(server, 23);
		 // Get input and output stream references
		 in = telneto.getInputStream();
		 out = new PrintStream(telneto.getOutputStream());
		 // Log the user on
		 readUntil("Username:");
		 write(user);
		 readUntil("Password:");
		 write(password);
		
		// write("display current-configuration");
		 // Advance to a prompt
		readUntil(""+prompt+"");
		// write("display current-configuration");
	   }
	   catch( Exception e ) {
		 e.printStackTrace();
	   }
	  }
	 /* public static su(String password) {
	    try {
	      write( "su" );
	      readUntil("Password:");
	      write( password );
	      prompt = '#';
	      readUntil( prompt + " " );
	    }
	    catch( Exception e )
	    {
	      e.printStackTrace();
	    }
	  }*/
	  public static 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 static void write( String value ) {
	   try {
		 out.println( value );
		 out.flush();
		 System.out.println( value );
	   }
	   catch( Exception e ) 
	   {
		 e.printStackTrace();
	   }
	  }
	  public static void /*String*/ sendCommand( String command )
	  {  //in = telneto.getInputStream();
		// out = new PrintStream(telneto.getOutputStream());
	 //  try {
		write(command);
		/* return readUntil( prompt + " " );
	   		}
	   		catch( Exception e )
	   		{
		 e.printStackTrace();
	   		}
	   return null;*/
	  }
	 public static void deco() 
	  {
	  try	{
		  	telneto.disconnect();

	  		}
	  catch( Exception e )
	   		{
		  		e.printStackTrace();
	  		}
	  }
	 
		 

A voir également:

1 réponse

batbat52 Messages postés 118 Date d'inscription jeudi 11 mars 2010 Statut Membre Dernière intervention 21 mai 2012 9
23 juin 2011 à 15:01
solution :

lass telnet{
 
	
	  static TelnetClient telneto = new TelnetClient();
	  static InputStream in;
	  static PrintStream out;
	  static char prompt = '$';
	
	  public telnet(String server, String user, String password) {
	   try {
		 // Connect to the specified server
		 telneto.connect(server, 23);
		 // Get input and output stream references
		 in = telneto.getInputStream();
		 out = new PrintStream(telneto.getOutputStream());
		 // Log the user on
		 readUntil("Username:");
		 write(user);
		 readUntil("Password:");
		 write(password);
		// write("display current-configuration");
	//	 readUntil(""+prompt+"");
			
		// write("display current-configuration");
		 
		 
		
		 // Advance to a prompt
		// 
		// write("display current-configuration");
	   }
	   catch( Exception e ) {
		 e.printStackTrace();
	   }
	  }
	 /* public static su(String password) {
	    try {
	      write( "su" );
	      readUntil("Password:");
	      write( password );
	      prompt = '#';
	      readUntil( prompt + " " );
	    }
	    catch( Exception e )
	    {
	      e.printStackTrace();
	    }
	  }*/
	  public static 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 static void write( String value ) {
	   try {
		 out.println( value );
		 out.flush();
		 System.out.println( value );
	   }
	   catch( Exception e ) 
	   {
		 e.printStackTrace();
	   }
	  }
	  public static void /*String*/ sendCommand( String command )
	  {  //in = telneto.getInputStream();
		// out = new PrintStream(telneto.getOutputStream());
	 //  try {
		write(command);
	readUntil(""+prompt+"");
	   /*		}
	   	catch( Exception e )
	   		{
		 e.printStackTrace();
	   		}
	   return null;*/
	  }
	 public static void deco() 
	  {
	  try	{
		  	telneto.disconnect();
		  	readUntil(""+prompt+"");
 
	  		}
	  catch( Exception e )
	   		{
		  		e.printStackTrace();
	  		}
	  }
0