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

Collection et Stream Java Discussion :

Affecter une donnée d'un tableau à une commande unix


Sujet :

Collection et Stream Java

  1. #1
    Membre régulier
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2013
    Messages
    286
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Avril 2013
    Messages : 286
    Points : 76
    Points
    76
    Par défaut Affecter une donnée d'un tableau à une commande unix
    Bonjour
    j'ai un tableau où je stock les interfaces d'un routeur cisco
    alors je veux ensuite lancer la commande "show running-config interface..."
    sur chaque interface du tableau
    par exemple le tableau myData contient:
    Fa0
    FA6
    fa7
    ..

    la commande doit se lancée comme celà
    show running-config interface Fa0
    show running-config interface Fa6
    ...

    j'ai essayer avec le code suivant mais rien ne marche

    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
     
     if(myData.get(0)!=null){
      String data=myData.get(0).toString();
      System.out.println("Data at 0th element is "+data);                  
      write("show running-config interface "+data);
      fichierUntil(data);
    // le fichierUntil est une fonction qui contient le traitement du résultat de la commande
    public String fichierUntil(String device) throws IOException {
     
            StringBuffer sb = new StringBuffer();
            char ch;
     
            try {
                readUntil1("show running-config interface "+device);
     
                while (true) {
                   do {
                        ch = (char) in.read();
                        sb.append(ch);
                    } while(ch!='#');
    S'il vous plait aidez moi

  2. #2
    Membre habitué
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2011
    Messages
    262
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2011
    Messages : 262
    Points : 157
    Points
    157
    Par défaut
    dans ton code y'a des ereurs de syntaxe.

    j'ai pas bien compris ce que tu veux, mais normalement tu dois faire une boucle for sur le tableau
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     for(int i=0;i<tontableau.length;i++){
    	    	//là tu fais le travail sur la chaine tontableau[i]
    	    }

  3. #3
    Membre régulier
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2013
    Messages
    286
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Avril 2013
    Messages : 286
    Points : 76
    Points
    76
    Par défaut
    oui je fais la boucle mais çà marche pas.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    public void fetchData(ArrayList myData){
                try{
    System.out.println("Size of my data in fetchData method::"+myData.size());
    for(int x=i;x<myData.size();x++){
      if(myData.get(i)!=null){
    String data=myData.get(I).toString();
    write1("show run interface "+data);
    fichierUntil(data);
    }
    }
    et la méthode fichierUntil traite le résultat de la commande

    ce que je veux c'est de lancer la commande unix sur toutes les interfaces que j'ai stocker dans le tableau myData

  4. #4
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Bonjour,

    Je ne comprend pas tes besoins mais quelque remarque:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    for(int x=i;x<myData.size();x++){
      if(myData.get(i)!=null){ // i est constante dans ta boucle
    String data=myData.get(I).toString(); // I est différent de i, java est sensible à la casse
    write1("show run interface "+data);
    fichierUntil(data);
    }
    }
    A+.

  5. #5
    Membre régulier
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2013
    Messages
    286
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Avril 2013
    Messages : 286
    Points : 76
    Points
    76
    Par défaut
    c'est juste une faute de frappe mais çà marche pas

  6. #6
    Membre habitué
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2011
    Messages
    262
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2011
    Messages : 262
    Points : 157
    Points
    157
    Par défaut
    y'a personne qui pourra t'aider avec ton bout de code, il faut que tu mets tout le code et l'erreur que tu obtient et dans quelle line.
    est ce que dans ta methode ou avant....
    qu'est ce que le programme affiche?
    quelle exception?

  7. #7
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    De toute façon les codes que tu nous donnes ne nous disent rien:

    1. des boucles sans fermeture de block
    2. de bout de méthodes qui ne va pas jusqu'à la fin
    3. une boucle infinie
    4. ....


    A+0

  8. #8
    Membre régulier
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2013
    Messages
    286
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Avril 2013
    Messages : 286
    Points : 76
    Points
    76
    Par défaut
    OK parce que j'ai un long code qui contient la partie où je fais le telnet sur le routeur ensuite je lance la commande
    la fonction fichierUntil(String device)
    permet de traiter le résultat de la commande unix
    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
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
     
    public class Test {
     
        private TelnetClient telnet = new TelnetClient();
        private InputStream in;
        private PrintStream out;
        static Statement st;
        static ResultSet rs;
        public static Connection con;
        private String client;
        private String vrf;
        private String address;
        private String type;
        private String JR;
        private String configData[] = new String[3];
        private String mask;
     ArrayList<String> myData=null;
     //ArrayList<Integer> numData=null;
     
        public Test() {
            myData=new ArrayList<String>();   
            //numData=new ArrayList<Integer>();
        }
     
    public String Config(String server, String user, String password, String device) {
            JFrame controllingFrame = null;
            try {
                // Connect to the specified server
                try {
                    telnet.setConnectTimeout(100);
                    telnet.connect(server, 23);
                } catch (UnknownHostException e1) {
                    return "Failure  ";
                } catch (SocketTimeoutException e) {
                    return "Failure ";
                } catch (NoRouteToHostException e) {
                    return "Failure ";
                } catch (ConnectException e) {
                    return "Failure ";
                }
     
                in = telnet.getInputStream();
                out = new PrintStream(telnet.getOutputStream());
     
                char ch;
                StringBuffer sa = new StringBuffer();
                do {
                    ch = (char) in.read();
                    sa.append(ch);
                } while (ch != '>' && ch != '#' && !sa.toString().contains("Username: ") && !sa.toString().contains("Password:"));
     
                if (sa.toString().contains("Username: ")) {            
     
                    write(user);
                    readUntil("Password: ");
                    writePass(password);
     
                    sa = new StringBuffer();
                    do {
                        ch = (char) in.read();
                        sa.append(ch);
     
                    } while (ch != '>' && ch != '#' && ch != '%');
     
                    if (ch == '%') {
                        in.close();
                        out.close();
                        telnet.disconnect();
                        return "tacacs";
                    } else if (ch == '>') {
                        write("en");
                        readUntil("Password: ");
                        writePass(password);
                        //fetchData(myData);
                        in.close();
                       out.close();
                       telnet.disconnect();
                        return "Succes";
                    }
                } else {
                    in.close();
                    out.close();
                    telnet.disconnect();
                    return "no Login";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }  
    public void fetchData(ArrayList myData){
                try{
    System.out.println("Size of my data :"+myData.size());
    for(int x=0;x<myData.size();x++){
      if(myData.get(x)!=null){
    String data=myData.get(x).toString();
    write1("show run int "+data);
    fichierUntil(data);
    }
     
    }
                }catch(Exception e){}
    }
     
        public String fichierUntil(String device) throws IOException {
     
            StringBuffer sb = new StringBuffer();
            char ch;
     
            try {
                readUntil1("show run int "+device);
     
                while (true) {
                    do {
                        ch = (char) in.read();
                        sb.append(ch);
                    } while (!sb.toString().contains("description") && ch != '#' && !sb.toString().contains("%"));
                    if (sb.toString().contains("%")) {
                        System.out.print("Invalid Input");
                        sb = new StringBuffer();
                    } else if (sb.toString().contains("#")) {
                        in.close();
                        out.close();
                        return "";
                    } else if (sb.toString().contains("description")) {
                        sb = new StringBuffer();
                        do {
                            ch = (char) in.read();
                            sb.append(ch);
                        } while (ch != 10 && ch != 13);
                        /*Data 1*/
                        client = sb.toString();
                        configData[0] = client;
                        System.out.print("La description du client est" + client);
     
                        String chaine = sb.toString();
                        int pos = chaine.indexOf("_TM") + 3;
                        String number = chaine.substring(pos);
                        int next = number.indexOf("_");
                        if (next > -1) {
                            number = number.substring(0, next);
                        }
                        System.out.print("La JR du client est: " + number + "\n");
                        sb = new StringBuffer();
     
                        do {
                            ch = (char) in.read();
                            sb.append(ch);
                        } while (!sb.toString().contains("ip vrf forwarding") && !(sb.toString().contains("ip address ")) && !(sb.toString().contains("xconnect ")) && !(sb.toString().contains("#")));
     
                        if (sb.toString().contains("ip vrf forwarding")) {
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (ch != 10 && ch != 13);
                            /*Data2*/
                            vrf = sb.toString();
                            configData[1] = vrf;
                            System.out.print("La vrf du client est :" + vrf);
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (!sb.toString().contains("ip address ") && !sb.toString().contains("#"));
     
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (ch != 32);
                            /*Data3 */
                            address = sb.toString();
     
                            System.out.print("L'adresse du client est :" + address);
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (ch != 32);
                            System.out.print(sb);
     
                                mask=sb.toString();                       
                                int pos1 = mask.lastIndexOf(".") + 1;
                                String lastPast_mask = mask.substring(pos1, mask.length());
                                lastPast_mask=lastPast_mask.trim();
                                int d = Integer.parseInt(lastPast_mask);
                                int pos_ip = address.lastIndexOf(".") + 1;
                                String lastPast_ip = address.substring(pos_ip, address.length());
                                lastPast_ip=lastPast_ip.trim();
                                int p = Integer.parseInt(lastPast_ip);
                                if(d==252){                                
                                   p= (p & 0xFC);
                                }
                                else if(d==248)
                                {                              
                                   p= (p & 0xF8);
                                }
     
                                String firstPart = address.substring(0, pos_ip);
                                String nouvelleAddress = firstPart + String.valueOf(p);
                                System.out.println("Nouvelle address=" + nouvelleAddress);
                                //configData[2]=address;
                                configData[2] = nouvelleAddress;
     
                        } else if (sb.toString().contains("ip address ")) {
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (ch != 32 && ch != '#');
     
                            address = sb.toString();
                            System.out.print("L'adresse du client est :" + address);
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (ch != 32);
                            System.out.print(sb);
     
                           mask=sb.toString(); 
                                address = address.trim();
                                mask = mask.trim();
                                int pos1 = mask.lastIndexOf(".") + 1;
                                String lastPast_mask = mask.substring(pos1, mask.length());
                                lastPast_mask=lastPast_mask.trim();
                                int d = Integer.parseInt(lastPast_mask);
                                int pos_ip = address.lastIndexOf(".") + 1;
                                 String lastPast_ip = address.substring(pos_ip, address.length());
                                lastPast_ip=lastPast_ip.trim();
                                int p = Integer.parseInt(lastPast_ip);
                                if(d==252){                                
                                   p = p / 4;
                                   p = p * 4;
                                }
                                else if(d==248)
                                {
     
                                   p = p / 8;
                                   p = p * 8;
                                }
     
                                String firstPart = address.substring(0, pos_ip);
                                String nouvelleAddress = firstPart + String.valueOf(p);
                                System.out.println("Nouvelle address=" + nouvelleAddress);
                                //configData[2]=address;
                                configData[2] = nouvelleAddress;
                            sb = new StringBuffer();
                        } else if (sb.toString().contains("xconnect ")) {
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (ch != 32 && ch != '#');
                            address = sb.toString();
                            System.out.print("Client L2L @Ip est :" + address + "\n");
                            String nouvelleAddress = nouvelleAddress(address);
                            //configData[2]=address;
                            configData[2] = nouvelleAddress;
                            sb = new StringBuffer();
                        }
                    }
                    in.close();
                    out.close();
                    return "";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
     
        public String nouvelleAddress(String address) {
            address = address.trim();
            int pos = address.lastIndexOf(".") + 1;
            String lastPast = address.substring(pos, address.length());
            int num1 = Integer.parseInt(lastPast);
            int num2 = num1 / 8;
            int num3 = num2 * 8;
            String firstPart = address.substring(0, pos);
            String nouvelleAddress = firstPart + String.valueOf(num3);
            System.out.println("Nouvelle address=" + nouvelleAddress);
            return nouvelleAddress;
        }
     
        public String readUntil(String pattern) {
            try {
                char lastChar = pattern.charAt(pattern.length() - 1);
                StringBuffer sb = new StringBuffer();
                StringBuffer s = new StringBuffer();
                boolean found = true;
                char ch = (char) in.read();
                while (found) {
                    sb.append(ch);
                    s.append('a');
                    if (ch == lastChar) {
                        if (sb.toString().endsWith(pattern)) {
                            return "no";
                        }
                    }
                    ch = (char) in.read();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
     
        public String readUntil1(String pattern) {
            try {
                char lastChar = pattern.charAt(pattern.length() - 1);
                StringBuffer sb = new StringBuffer();
                boolean found = true;
     
                char ch = (char) in.read();
                while (found) {
                    sb.append(ch);
                    if (ch == lastChar) {
                        if (sb.toString().endsWith(pattern)) {
                            return sb.toString();
                        }
                    }
     
                    if (sb.toString().endsWith("#")) {
                        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();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        public void write1(String value) {
            try {
                out.println(value);
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        public void writePass(String value) {
            try {
                out.println(value);
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        public void disconnect() {
            try {
                telnet.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
     
    }
    le Main:
    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
     
    public class Main_Test {
        public static void main(String[] args) {
     
              try{
      Telnet_Interface telnet=new Telnet_Interface();            
    Test telnet_R = new Test();
    ArrayList myData=telnet.getMyData();
     
    telnet_R.Config("182.16.2.13", "user", "password", "182.16.2.13");
    System.out.println("Size of my data in main_Test:"+myData.size());
     
    if(myData!=null){
    telnet_R.fetchData(myData);      
    }
    else{
    System.out.println("My data is null");
    }
     
    }catch (Exception e) {
                e.printStackTrace();
            }
          }
    }
    les erreurs sont les suivants:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    run:
    Size of my data in main_Test:197
    java.io.IOException: Stream closed
    Size of my data :197
    	at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)
    	at java.io.BufferedInputStream.fill(BufferedInputStream.java:206)
    	at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    	at projet.Test.readUntil1(Test.java:347)
    	at projet.Test.fichierUntil(Test.java:142)
    	at projet.Test.fetchData(Test.java:129)
    	at projet.Main_Test.main(Main_Test.java:25)
    et elle se repetent puisque j'ai plusieurs données dans le tableau.

  9. #9
    Membre chevronné
    Inscrit en
    Mai 2006
    Messages
    1 364
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 1 364
    Points : 1 984
    Points
    1 984
    Par défaut
    Ce serait bien de refaire ce code parce qu'il y a pas mal de problemes. Certaines parties servent à rien - voir sont dangereuses (par exemple le while(true)) et certaines variables sont définies comme instances d'objet alors qu'elles devraient etre locales.

    Bref, pour revenir au probleme, l'exception est tres claire : tu essaies de lire un inputstream qui est fermé. Comme l'InputStream "in" est global, il ne faut pas le fermer avant d'avoir fini de le lire (c'est à dire ne pas appeler "close" avant la fin). Visiblement, dans ton cas, tu le fais...

  10. #10
    Membre régulier
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2013
    Messages
    286
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Avril 2013
    Messages : 286
    Points : 76
    Points
    76
    Par défaut
    rie ne change les mêmes erreurs ça fait 3 jours que je travaille sur cette partie et j'arrive à rien çà me gêne vraiment le java et les commande unix

  11. #11
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Tu dois poster ton code modifier.

    A+.

  12. #12
    Membre régulier
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2013
    Messages
    286
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Avril 2013
    Messages : 286
    Points : 76
    Points
    76
    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
    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
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
     
    public class Test {
     
        private TelnetClient telnet = new TelnetClient();
        private InputStream in;
        private PrintStream out;
     
     ArrayList<String> myData=null;
     
        public Test() {
            myData=new ArrayList<String>();   
     
        }
     
    public String Config(String server, String user, String password, String device,ArrayList<String> myData) {
            JFrame controllingFrame = null;
            try {
                // Connect to the specified server
                try {
                    telnet.setConnectTimeout(100);
                    telnet.connect(server, 23);
                } catch (UnknownHostException e1) {
                    return "Failure  ";
                } catch (SocketTimeoutException e) {
                    return "Failure ";
                } catch (NoRouteToHostException e) {
                    return "Failure ";
                } catch (ConnectException e) {
                    return "Failure ";
                }
     
                in = telnet.getInputStream();
                out = new PrintStream(telnet.getOutputStream());
     
                char ch;
                StringBuffer sa = new StringBuffer();
                do {
                    ch = (char) in.read();
                    sa.append(ch);
                } while (ch != '>' && ch != '#' && !sa.toString().contains("Username: ") && !sa.toString().contains("Password:"));
     
                if (sa.toString().contains("Username: ")) {            
     
                    write(user);
                    readUntil("Password: ");
                    writePass(password);
     
                    sa = new StringBuffer();
                    do {
                        ch = (char) in.read();
                        sa.append(ch);
     
                    } while (ch != '>' && ch != '#' && ch != '%');
     
                    if (ch == '%') {
                        in.close();
                        out.close();
                        telnet.disconnect();
                        return "tacacs";
                    } else if (ch == '>') {
                        write("en");
                        readUntil("Password: ");
                        writePass(password);
                        for(int x=0;x<myData.size();x++){
      if(myData.get(x)!=null){
    String data=myData.get(x).toString();
    write1("show run int "+data);
     
                        }}
                        fichierUntil(device);
                        in.close();
                       out.close();
                       telnet.disconnect();
                        return "Succes";
                    }
                } else {
                    in.close();
                    out.close();
                    telnet.disconnect();
                    return "no Login";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }  
    public void fetchData(ArrayList myData){
                try{
    System.out.println("Size of my data :"+myData.size());
    for(int x=0;x<myData.size();x++){
      if(myData.get(x)!=null){
    String data=myData.get(x).toString();
    write1("show run int "+data);
    fichierUntil(data);
    }
     
    }
                }catch(Exception e){}
    }
     
        public String fichierUntil(String device) throws IOException {
     
            StringBuffer sb = new StringBuffer();
            char ch;
            try {
    for(int x=0;x<myData.size();x++){
      if(myData.get(x)!=null){
    String data=myData.get(x).toString();
    write1("show run int "+data);
     
                readUntil1("show run int "+device);      
                    do {
                        ch = (char) in.read();
                        sb.append(ch);
                    } while (!sb.toString().contains("description") && ch != '#' && !sb.toString().contains("%"));
                    if (sb.toString().contains("%")) {
                        System.out.print("Invalid Input");
                        sb = new StringBuffer();
                    } else if (sb.toString().contains("#")) {
                        in.close();
                        out.close();
                        return "";
                    } else if (sb.toString().contains("description")) {
                        sb = new StringBuffer();
                        do {
                            ch = (char) in.read();
                            sb.append(ch);
                        } while (ch != 10 && ch != 13);
                        /*Data 1*/
                        client = sb.toString();
                        configData[0] = client;
                        System.out.print("La description du client est" + client);
     
                        String chaine = sb.toString();
                        int pos = chaine.indexOf("_TM") + 3;
                        String number = chaine.substring(pos);
                        int next = number.indexOf("_");
                        if (next > -1) {
                            number = number.substring(0, next);
                        }
                        System.out.print("La JR du client est: " + number + "\n");
                        sb = new StringBuffer();
     
                        do {
                            ch = (char) in.read();
                            sb.append(ch);
                        } while (!sb.toString().contains("ip vrf forwarding") && !(sb.toString().contains("ip address ")) && !(sb.toString().contains("xconnect ")) && !(sb.toString().contains("#")));
     
                        if (sb.toString().contains("ip vrf forwarding")) {
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (ch != 10 && ch != 13);
                            /*Data2*/
                            vrf = sb.toString();
                            configData[1] = vrf;
                            System.out.print("La vrf du client est :" + vrf);
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (!sb.toString().contains("ip address ") && !sb.toString().contains("#"));
     
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (ch != 32);
                            /*Data3 */
                            address = sb.toString();
     
                            System.out.print("L'adresse du client est :" + address);
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (ch != 32);
                            System.out.print(sb);
     
                                mask=sb.toString();                       
                                int pos1 = mask.lastIndexOf(".") + 1;
                                String lastPast_mask = mask.substring(pos1, mask.length());
                                lastPast_mask=lastPast_mask.trim();
                                int d = Integer.parseInt(lastPast_mask);
                                int pos_ip = address.lastIndexOf(".") + 1;
                                String lastPast_ip = address.substring(pos_ip, address.length());
                                lastPast_ip=lastPast_ip.trim();
                                int p = Integer.parseInt(lastPast_ip);
                                if(d==252){                                
                                   p= (p & 0xFC);
                                }
                                else if(d==248)
                                {                              
                                   p= (p & 0xF8);
                                }
     
                                String firstPart = address.substring(0, pos_ip);
                                String nouvelleAddress = firstPart + String.valueOf(p);
                                System.out.println("Nouvelle address=" + nouvelleAddress);
                                //configData[2]=address;
                                configData[2] = nouvelleAddress;
     
                        } else if (sb.toString().contains("ip address ")) {
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (ch != 32 && ch != '#');
     
                            address = sb.toString();
                            System.out.print("L'adresse du client est :" + address);
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (ch != 32);
                            System.out.print(sb);
     
                           mask=sb.toString(); 
                                address = address.trim();
                                mask = mask.trim();
                                int pos1 = mask.lastIndexOf(".") + 1;
                                String lastPast_mask = mask.substring(pos1, mask.length());
                                lastPast_mask=lastPast_mask.trim();
                                int d = Integer.parseInt(lastPast_mask);
                                int pos_ip = address.lastIndexOf(".") + 1;
                                 String lastPast_ip = address.substring(pos_ip, address.length());
                                lastPast_ip=lastPast_ip.trim();
                                int p = Integer.parseInt(lastPast_ip);
                                if(d==252){                                
                                   p = p / 4;
                                   p = p * 4;
                                }
                                else if(d==248)
                                {
     
                                   p = p / 8;
                                   p = p * 8;
                                }
     
                                String firstPart = address.substring(0, pos_ip);
                                String nouvelleAddress = firstPart + String.valueOf(p);
                                System.out.println("Nouvelle address=" + nouvelleAddress);
                                //configData[2]=address;
                                configData[2] = nouvelleAddress;
                            sb = new StringBuffer();
                        } else if (sb.toString().contains("xconnect ")) {
                            sb = new StringBuffer();
                            do {
                                ch = (char) in.read();
                                sb.append(ch);
                            } while (ch != 32 && ch != '#');
                            address = sb.toString();
                            System.out.print("Client L2L @Ip est :" + address + "\n");
                            String nouvelleAddress = nouvelleAddress(address);
                            //configData[2]=address;
                            configData[2] = nouvelleAddress;
                            sb = new StringBuffer();
                        }
                    }
                    in.close();
                    out.close();
                    return "";
      }
    }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
     
        public String nouvelleAddress(String address) {
            address = address.trim();
            int pos = address.lastIndexOf(".") + 1;
            String lastPast = address.substring(pos, address.length());
            int num1 = Integer.parseInt(lastPast);
            int num2 = num1 / 8;
            int num3 = num2 * 8;
            String firstPart = address.substring(0, pos);
            String nouvelleAddress = firstPart + String.valueOf(num3);
            System.out.println("Nouvelle address=" + nouvelleAddress);
            return nouvelleAddress;
        }
     
     
     
        public String readUntil1(String pattern) {
            try {
                char lastChar = pattern.charAt(pattern.length() - 1);
                StringBuffer sb = new StringBuffer();
                boolean found = true;
     
                char ch = (char) in.read();
                while (found) {
                    sb.append(ch);
                    if (ch == lastChar) {
                        if (sb.toString().endsWith(pattern)) {
                            return sb.toString();
                        }
                    }
     
                    if (sb.toString().endsWith("#")) {
                        return sb.toString();
                    }
                    ch = (char) in.read();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
     
     
        public void write1(String value) {
            try {
                out.println(value);
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        public void writePass(String value) {
            try {
                out.println(value);
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        public void disconnect() {
            try {
                telnet.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
     
    }
    the main:

    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
     
    public class Main_Test {
        public static void main(String[] args) {
     
              try{
      Telnet_Interface telnet=new Telnet_Interface();            
    Test telnet_R = new Test();
    ArrayList myData=telnet.getMyData();
    telnet.Config("182.16.2.133", "user", "pass", "182.16.2.133");
    telnet_R.Config("182.16.2.133", "user", "pass", "182.16.2.133",myData);
    System.out.println("Size of my data in main_Test:"+myData.size());
     
    }catch (Exception e) {
                e.printStackTrace();
            }
          }
    }
    le résultat est juste çà:
    run:
    Size of my data in main_Test:197
    GÉNÉRATION TERMINÉE (durée totale* 2 secondes)
    mais pour la commande il y a rien

Discussions similaires

  1. Réponses: 1
    Dernier message: 08/01/2010, 13h46
  2. [Tableaux] Ajout d'une donnée dans un tableau
    Par the magic developer dans le forum Langage
    Réponses: 7
    Dernier message: 04/11/2008, 19h24
  3. [RegEx] Remplacer une donnée d'un tableau
    Par Invité dans le forum Langage
    Réponses: 3
    Dernier message: 02/05/2008, 11h11
  4. Réponses: 1
    Dernier message: 08/09/2007, 19h00
  5. Réponses: 7
    Dernier message: 11/01/2007, 18h54

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