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

Java Discussion :

Telnet et Java


Sujet :

Java

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Octobre 2011
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Telnet et Java
    Bonjour,

    J'ai crée un petit porgramme qui suivant la présence d'un employé ( information venant d'un fichier texte ) ouvre une session telnet.
    Tout fonctionne très bien mais ce que je n'arrive pas à faire c'est automatiser les commandes telnet. ( j'utilise la librairie com.jscape.inet.telnet.*)

    C'est à dire que je me connecte en telnet au 192.168.100.*** , ça marche mais après y me demande mon mot de pass et je n'arrive pas à ce que mon programme mette le mot de pass automatiquement. Et pareil après pour les commandes telnet.

    Si quelqu'un pourrait m'aider, ça serait génial.

    Ci-dessous mon code





    package telnetexample;

    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
    168
    169
    170
    171
    172
    import com.jscape.inet.telnet.*;
    import java.io.*;
     
    public class TelnetExample extends TelnetAdapter {
     
    	private Telnet telnet = null;
            //private Telnet passw = null; 
    	private OutputStream output = null;
    	private static BufferedReader reader = null;
    	private boolean connected = false;
     
    	public TelnetExample(String hostname, String password) throws IOException, TelnetException {
     
    		String input = null;
    		// create new Telnet instance
    		telnet = new Telnet(hostname);
     
     
    		// register this class as TelnetListener
    		telnet.addTelnetListener(this);
     
    		// establish Telnet connection
    		telnet.connect();
    		connected = true;
     
    		// get output stream
    		output = telnet.getOutputStream();
     
    		// sends all data entered at console to Telnet server
    		while ((input = reader.readLine()) != null) {
    			if (connected) {
    				((TelnetOutputStream) output).println(input);
    			} else {
    				break;
    			}
    		}
    	}
     
     
    	/** 
             * Invoked when Telnet socket is connected.
             * @see TelnetConnectedEvent
             * @see Telnet#connect
             */
    	public void connected(TelnetConnectedEvent event) {
    		System.out.println("Connected");
    	}
     
    	/** 
             * Invoked when Telnet socket is disconnected. Disconnect can
             * occur in many circumstances including IOException during socket read/write.
             * @see TelnetDisconnectedEvent
             * @see Telnet#disconnect
             */
    	public void disconnected(TelnetDisconnectedEvent event) {
    		connected = false;
    		System.out.print("Disconnected.  Press enter key to quit.");
    	}
     
    	/**
             * Invoked when Telnet server requests that the Telnet client begin performing specified <code>TelnetOption</code>.
             * @param event a <code>DoOptionEvent</code>
             * @see DoOptionEvent
             * @see TelnetOption
             */
     
     
    	public void doOption(DoOptionEvent event) {
    		// refuse any options requested by Telnet server
    		telnet.sendWontOption(event.getOption());
    	}
     
    	/**
             * Invoked when Telnet server offers to begin performing specified <code>TelnetOption</code>.
             * @param event a <code>WillOptionEvent</code>
             * @see WillOptionEvent
             * @see TelnetOption
             */
    	public void willOption(WillOptionEvent event) {
    		// refuse any options offered by Telnet server
    		telnet.sendDontOption(event.getOption());
    	}
     
    	/**
             * Invoked when data is received from Telnet server.
             * @param event a <code>TelnetDataReceivedEvent</code>
             * @see TelnetDataReceivedEvent
             */
    	public void dataReceived(TelnetDataReceivedEvent event) {
    		// print data recevied from Telnet server to console
    		System.out.print(event.getData());
    	}
     
    	/**
             * Main method for launching program
             * @param args program arguments
             */
    	public static void main(String[] args) {
     
                    String chaine="";
    		String fichier ="presence.txt";
                    String cherche = "presence : on";
                    String cherche2 = "resence : off";
                    Integer  index;
                    Integer  index2;
    		Integer i;
     
     
                    for (i=0;i<1;i++)
                    {
     
    		//lecture du fichier texte	
    		try{
    			InputStream ips=new FileInputStream(fichier); 
    			InputStreamReader ipsr=new InputStreamReader(ips);
    			BufferedReader br=new BufferedReader(ipsr);
    			String ligne;
    			while ((ligne=br.readLine())!=null){
    				System.out.println(ligne);
    				chaine+=ligne+"\n";
     
     
                                    index=ligne.indexOf(cherche);
                                    //System.out.println(index);
                                    index2=ligne.indexOf(cherche2);
     
                                    //System.out.println("indexOf(presence : on) = " + 
                                    //ligne.indexOf("presence : on"));  
     
                                    //System.out.println("indexOf(presence : off) = " + 
                                    //ligne.indexOf("resence : off"));  
     
                       if (index == 0) {
    		try {
     
     
     
    			reader = new BufferedReader(new InputStreamReader(System.in));
     
    			// prompt user for Telnet server hostname
    			//System.out.print("Enter Telnet server hostname (e.g. 10.0.0.1): ");
    			String hostname = "192.168.100.254";
                            String password = "cisco";
                            System.out.println(password);
    			// create new TelnetExample instance
    			TelnetExample example = new TelnetExample(hostname, password);
                            //TelnetExample pass = new TelnetExample(hostname);
     
     
    		} catch (Exception e) {
    			e.printStackTrace(System.out);
    		}
                     }
                                   //else {System.out.println("je ne suis pas la");  }
                                   if (index2 == 1) {System.out.println("je ne suis pas la"); break;}
     
    			}
     
    			br.close(); 
    		}		
    		catch (Exception e){
    			System.out.println(e.toString());
    		}
     
                    }    
     
     
     
     
     
        }
    }

  2. #2
    Membre régulier
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2011
    Messages
    65
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Avril 2011
    Messages : 65
    Points : 88
    Points
    88
    Par défaut Flux
    Salut,

    en fait de ce que je me souviens pour avoir eut le même genre de problématique, il te faut récupérer les flux d'entrées sorties de ton telnet et envoyer directement dedans les informations.

Discussions similaires

  1. Exécution commande Telnet sous Java
    Par akbwalid dans le forum API standards et tierces
    Réponses: 10
    Dernier message: 27/07/2011, 13h36
  2. session telnet via java
    Par maymaoua dans le forum Développement
    Réponses: 2
    Dernier message: 21/06/2010, 18h56
  3. Interagir sur un système linux avec une connexion telnet en JAVA
    Par grandthor dans le forum Général Java
    Réponses: 9
    Dernier message: 17/04/2009, 00h47
  4. Connexion telnet en java
    Par zeus2005 dans le forum API standards et tierces
    Réponses: 1
    Dernier message: 15/11/2007, 13h18
  5. Communication Telnet->Application JAVA
    Par HuZimA dans le forum Entrée/Sortie
    Réponses: 4
    Dernier message: 21/03/2006, 15h38

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