How to extract rows or columns from a string in Java?

0

I explain: I get a response string from a console of a switch. This is fine and it works. A command is executed that returns a table like this:

SwitchPrueba#show port-security
Secure Port  MaxSecureAddr  CurrentAddr  SecurityViolation    SecurityAction
                (Count)       (Count)          (Count)
---------------------------------------------------------------------------
      Fa0/1              2            1                  0         Restrict
      Fa0/2              1            0                  0         Restrict
      Fa0/3              1            0                **8**       Restrict
      Fa0/4              1            0                  0         Restrict
      Fa0/5              1            0                  0         Restrict
      Fa0/6              1            0                  0         Restrict
      Fa0/7              1            0                  0         Restrict
      Fa0/8              1            0                  0         Restrict
      Fa0/9              1            0                  0         Restrict
     Fa0/10              1            0                  0         Restrict
     Fa0/11              1            0                  0         Restrict
     Fa0/12              1            0                  0         Restrict
     Fa0/13              1            1                  0         Restrict
     Fa0/14              1            1                  0         Restrict
     Fa0/15              1            0                  0         Restrict
     Fa0/16              1            0                  0         Restrict
     Fa0/17              1            0                  0         Restrict
     Fa0/18              1            1                  0         Restrict
     Fa0/19              1            0                  0         Restrict
     Fa0/20              1            0                  0         Restrict

SwitchPrueba#exit

Now the problem is coming. I need to execute another command that is executed depending on the SecurityViolation column where the command to execute is when the values of that column are greater than 0. Then the command would be something like this:

  

clear port-security dynamic interface Fa0 / 3

since it is the only one greater than 0

All that table I receive as a string. And I really do not know how to try to get line by line or column by column.

I do not put code because I do not have it. It would practically be something like this:

String respuesta = "SwitchPrueba#show port-security
Secure Port  MaxSecureAddr  CurrentAddr  SecurityViolation    SecurityAction
                (Count)       (Count)          (Count)
---------------------------------------------------------------------------
      Fa0/1              2            1                  0         Restrict
      Fa0/2              1            0                  0         Restrict
      Fa0/3              1            0                **8**       Restrict
      Fa0/4              1            0                  0         Restrict
      Fa0/5              1            0                  0         Restrict
      Fa0/6              1            0                  0         Restrict
      Fa0/7              1            0                  0         Restrict
      Fa0/8              1            0                  0         Restrict
      Fa0/9              1            0                  0         Restrict
     Fa0/10              1            0                  0         Restrict
     Fa0/11              1            0                  0         Restrict
     Fa0/12              1            0                  0         Restrict
     Fa0/13              1            1                  0         Restrict
     Fa0/14              1            1                  0         Restrict
     Fa0/15              1            0                  0         Restrict
     Fa0/16              1            0                  0         Restrict
     Fa0/17              1            0                  0         Restrict
     Fa0/18              1            1                  0         Restrict
     Fa0/19              1            0                  0         Restrict
     Fa0/20              1            0                  0         Restrict

    SwitchPrueba#exit"
    
asked by Luis Fernando 07.03.2018 в 22:15
source

1 answer

1

Considering what you explain what you could do is take the execution output of the first command and analyze it from java. Then I leave an example that works well with your case ie the interface prints whose security violation is greater than zero. In each line of code I put what I do. This is an indicative example (so that you see the main idea of the data analysis), it will depend on you to adapt it / improve it according to your needs. I based myself on the example you set as an exit. To invoke a command from a java program, use the line

Process process = Runtime.getRuntime().exec("java -jar C:/proyectojava/port-security.jar");

In the exec you have to put the command that you want to execute, in my case the example jar (port-security.jar) returns your output on screen emulating the process that you do. You have to adapt it to yours.

This is the complete example considering only the analysis of the information

package programa;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] argv) throws IOException
    {       
        /*Podes leer de un archivo o bien tomar el input de un proceso. 
          Si lo queres es que tome el input de un proceso hay que usar lo  
         siguiente. 

         En mi ejemplo port-security.jar devuelve la salida por pantalla que pones por pantalla. 
         Puede ser un comando cualquiera el que le pases*/
        BufferedReader reader=null;
        try {
        Process process = Runtime.getRuntime().exec("java -jar C:/proyectojava/port-security.jar");
        reader = new BufferedReader( new InputStreamReader(process.getInputStream()));      
        String s = null;
        int contador=0;  
        String interfaz=null;
        String security=null;
        String[] parts=null;
        while ((s = reader.readLine()) != null) {               

            if (contador==4) // El contador es para comenzar a tomar los datos de donde interesa. Otra opción puede ser quitarle el encabezado antes
            { 
               s=s.trim().replaceAll(" +", " "); //Reemplazamos los espacios por un solo espacio         
               parts=s.split(" "); // Aplicamos el split por el espacio para tener una lista de los diferentes campos del registro
               if (parts.length>3) { //Controlamos las posiciones para que no arroje excepción
                      interfaz=parts[0];    // Seleccionamos el campo interfaz
                      security=parts[3]; //Vamos al campo de security violation
                      security=security.replace("*","");//Le quitamos los asteriscos               
                      if (security.matches("^[1-9]*")) // Si es mayor a cero imprimimos la interfaz y el campo securityviolation
                      {
                          System.out.println(interfaz); //Imprimimos ya que el campo securityviolation es superior a 0
                      }
               }
            }
            else    
                  contador++;
         }
        }
        finally {
            if (reader!=null)
                   reader.close();
        }
    }


}

The output on the screen is (which is the data that was trying to get).

Fa0/3 

Greetings and I hope it is of your use.

    
answered by 08.03.2018 / 03:13
source