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

Servlets/JSP Java Discussion :

java servlet telnet valves


Sujet :

Servlets/JSP Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Femme Profil pro
    Inscrit en
    Avril 2011
    Messages
    116
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Avril 2011
    Messages : 116
    Par défaut java servlet telnet valves
    Bonjour,

    J'essai de me connecter avec une connection telnet a un switch h3c mais j'ai une erreur et je ne vois pas de quoi cela vien

    voici mon code

    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
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
     
    package ubpackage;
     
    import java.lang.*;
    import java.util.*;
    import java.util.Date;
    import java.util.Locale;
    import java.text.SimpleDateFormat;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.io.PrintWriter;
    import javax.servlet.*;
    import javax.servlet.ServletException;
    import java.net.SocketException;
    import javax.servlet.http.*;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.sql.*;			// API SQL
    import com.novell.ldap.*;		// API LDAP
    import java.io.InputStream;		// Toutes les API pour le script "Cisco"
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.io.FileOutputStream;
    import org.apache.commons.net.telnet.TelnetClient;
    import org.apache.commons.net.telnet.TelnetNotificationHandler;
    import org.apache.commons.net.telnet.SimpleOptionHandler;
    import org.apache.commons.net.telnet.EchoOptionHandler;
    import org.apache.commons.net.telnet.TerminalTypeOptionHandler;
    import org.apache.commons.net.telnet.SuppressGAOptionHandler;
    import org.apache.commons.net.telnet.InvalidTelnetOptionException;
    import java.util.StringTokenizer;
    import org.apache.commons.net.telnet.*;
    import java.io.*;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.*;
    import java.net.JarURLConnection;
     
     
     
     
    public class telnet extends HttpServlet{
     
     
    	  private TelnetClient telneto = new TelnetClient();
    	  private InputStream in;
    	  private PrintStream out;
    	  private 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( "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 {
    		 telneto.disconnect();
    	   }
    	   catch( Exception e ) {
    		 e.printStackTrace();
    	   }
    	  }
     
     
    		  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException 
    			{  
    			  try {
    				  response.setContentType("text/html");
    					PrintWriter out = response.getWriter();
    					out.println("<html>");
    					out.println("<head><title></title></head>");
    					out.println("<body>");
     
    				 telnet telneto = new telnet("10.247.112.220","log","pass");
    				 telneto.sendCommand("display current-configuration"); 
    				 telneto.disconnect();
    				 out.println("</center></body></html>");
    			   }
    			   catch( Exception e ) {
    				 e.printStackTrace();
    			   }
     
    			}
    		  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    		  	{
    			      doGet(request, response);
    			}
     
     
    }
    et mon erreur

    org.apache.catalina.valves.errorreportvalve.invoke(errorrepportvalve.java:10)

  2. #2
    Membre confirmé
    Femme Profil pro
    Inscrit en
    Avril 2011
    Messages
    116
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Avril 2011
    Messages : 116
    Par défaut
    bonjour,
    voilà je n'ai plus mon erreur je ne sais pas de quoi cela vient^^
    j'ai tout d'abord ajouté commons-net.jar ce qui n'avait rien changé à mon problème puis j'ai ordonné mon travail, Enfin maintenant cela fonctionne :p Si mon code peut aider quelqu'un... le voici :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    public static void telnet()
    	{
    		principal.outstr("<html><head></head>",false);
    		principal.outstr("<body>",false);
    		telnet.telnet("10.247.112.220","log","pass");
    		telnet.sendCommand("display current-configuration"); 
    		//telnet.disconnect();
    		principal.outstr("</center></body></html>",true);
    	}

    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
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
     
    public class telnet{
     
     
    	  static TelnetClient telneto = new TelnetClient();
    	  static InputStream in;
    	  static PrintStream out;
    	  static char prompt = '$';
     
    	  public static void 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 );
    		 // Advance to a prompt
    		 readUntil(prompt + " ");
    	   }
    	   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 )
    	  {
    	 //  try {
    		 write( command );
    		/* return readUntil( prompt + " " );
    	   		}
    	   catch( Exception e )
    	   		{
    		 e.printStackTrace();
    	   		}
    	   return null;*/
    	  }
    	  public static void disconnect() 
    	  {
    	   try	{
    		 disconnect();
    	   		}
    	   catch( Exception e )
    	   		{
    		 e.printStackTrace();
    	   		}
    	  }
     
     
     
    }

  3. #3
    Membre confirmé
    Femme Profil pro
    Inscrit en
    Avril 2011
    Messages
    116
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Avril 2011
    Messages : 116
    Par défaut
    En faite j'ai encore un petit problème la connexion s'établie bien, mais l'envoi des commandes non, j'essai donc de créer un objet comme ceci
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    telnet telneto = new telnet("10.247.112.220","log","pass");
    pour ensuite utiliser mes méthodes sur cet objet mais cela ne fonctionne pas
    voici l'erreur :

    C:\Users\admin\workspace\jipam\src\adeline.java:152: cannot find symbol
    [javac] symbol : constructor telnet(java.lang.String,java.lang.String,java.lang.String)
    [javac] location: class ubpackage.telnet
    [javac] telnet telneto = new telnet("10.247.112.220","log","pass");
    [javac] ^
    [javac] Note: C:\Users\admin\workspace\jipam\src\principal.java uses unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.

  4. #4
    Membre confirmé
    Femme Profil pro
    Inscrit en
    Avril 2011
    Messages
    116
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Avril 2011
    Messages : 116
    Par défaut
    Me revoilà,

    la création a fonctionné avec ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    telnet t;
    t= new telnet("10.247.112.220","log","pass")
    seulement je n'arrive pas à utiliser les méthodes sendCommand et disconnect

    en faite si j'enlève la ligne readUntil( prompt + " " );

    dans mon constructeur la méthode sendcommand fonctionne mais ne retourne rien (autrement dit le message est envoyé mais c'est comme si le switch le voyait pas) comment faire?

  5. #5
    Membre confirmé
    Femme Profil pro
    Inscrit en
    Avril 2011
    Messages
    116
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Avril 2011
    Messages : 116
    Par défaut
    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
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    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");
    	//	 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();
    	  		}
    	  }
    et voilà ça fonctionne!! =)

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. connection java servlet <->mysql
    Par foubou dans le forum JDBC
    Réponses: 2
    Dernier message: 12/05/2008, 02h26
  2. compiler un .Java (servlet) dans un projet Tomcat
    Par choupeo dans le forum Eclipse Java
    Réponses: 2
    Dernier message: 10/12/2007, 10h57
  3. [java] Servlet BIRT engine : ne marche pas!
    Par nicolep dans le forum BIRT
    Réponses: 23
    Dernier message: 16/03/2007, 14h41
  4. [Debutant(e)]java.servlet introuvable
    Par roninou dans le forum Eclipse Java
    Réponses: 3
    Dernier message: 07/02/2006, 12h20
  5. [JAVA][SERVLET][TOMCAT][COOKIE] addCookie(cookie) marche pas
    Par nickylarson34 dans le forum Servlets/JSP
    Réponses: 8
    Dernier message: 08/08/2005, 17h58

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