2

I am trying to find the best way how to parse specific output from jsch when connecting and executing commands on a c7000 HP enclosure.

What I have done so far is written a program that connects to a hp enclosure, executes a command, retrieves the output of the command and converts it from a stream to a String.

I end up with this String

Server Blade #1 Information:
    Type: Server Blade
    Manufacturer: HP
    Product Name: ProLiant BL280c G6
    Part Number: 507865-B21     
    System Board Spare Part Number: 531337-001
    Serial Number: XZ73616G9Z      
    UUID: 38926035-5636-5D43-3330-359274423959
    Server Name: SERVERONE
    Asset Tag: [Unknown]
    ROM Version: I25 02/01/2013

    CPU 1: Intel(R) Xeon(R) CPU E5520 @ 2.27GHz (4 cores)
    CPU 2: Intel(R) Xeon(R) CPU E5520 @ 2.27GHz (4 cores)
    Memory: 49152 MB

Now I need to extract some information from this string and put it in a variable/variables. I have tried with regex but don't seem to hack it. What I need is to end up with for example, product name "Proliant BL280c G6" in a string variable that I later can use, same goes with serial number or any other info in there. What I do not need is the preceding text as Type: or Part Number:. I only need to extract what comes after that.

I am pretty new with Java, learning lots every day, can anyone here point me in the right direction of the best way of solving this?

EDIT: Thank you very much all for quick responses. I got a few ideas now on how to solve the problem. The biggest help goes to showing me how to use regex expressions correctly. What i missed there was the possibility of excluding string pieces not needed ex. (?<=Product\sName:).

0

3 Answers 3

2
String delims = "\n";   //this is the identifier of a line change in java
String[] lines = result.split(delims);  

for (int i=0;i<lines.length;i++)  
{  
        System.out.println(lines[i]);  
}  

This code will save (and print) the lines in that String (assuming that what you posted is saved as a java String).

You have several ways to do this thou. Sure they will be more reasonable methods to make this (regex, parsers,...), but you can do this to check if a String contains a Substring:

 str1.toLowerCase().contains(str2.toLowerCase())

So, for example, if you want to know if a line of lines[i] contains the word Product Name, just make this:

 subS = "Product Name";
 if (lines[x].toLowerCase().contains(subS.toLowerCase())); 
 //x being a number between 0 and the total of lines

As stated, this is a very rustical method, but it will work.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use Asier's method of splitting the lines by the newline character ('\n') and then to get the value of each one you can do line.substring(line.indexOf(":")+1, line.length()). You can also get the name of the parameter with line.substring(0, line.indexOf(":")).

You could make a HashMap and access them by key:

HashMap<String, String> map = new HashMap<String, String>(); //or new HashMap<>() if you are on Java 7
String str = "your server stuff goes here";
String[] lines = str.split("\n");
for(int i = 0; i < lines.length; i++)
{
  String curline = lines[i];
  int index = curline.indexOf(":");
  map.put(curline.substring(0, index).trim(), curline.substring(index+1, curline.length()).trim());
}

Comments

0

Assuming, that your response is in source.txt, we read the file

File file = new File("source.txt");
BufferedReader fileReader = new BufferedReader(new FileReader(file));

Reading line by line we match it against regexp with lookup behind:

String line;
String LOOKUP_BEHIND = "(?<=[a-zA-Z0-9 ]:)";
while((line = fileReader.readLine())!= null){
    Matcher matcher1 = Pattern.compile(LOOKUP_BEHIND + ".+").matcher(line);
    if (matcher1.find()){
        System.out.println(matcher1.group(0));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.