Hi ,

I need to parse a file that contain linux interface information. My file text is like below

Interface ge1
Hardware is Ethernet
Current HW addr: 00a1.2599.0003
Physical:00a1.2599.0003
Description: BC3 - Slot 3 (Phys Slot 7)
index 5001 metric 1 mtu 1500 duplex-full arp ageing timeout 0
<BROADCAST,MULTICAST>
VRF Binding: Not bound
Bandwidth 1g
VRRP Master of : VRRP is not configured on this interface.
input packets 00, bytes 00, dropped 00, multicast packets 00
output packets 00, bytes 00, multicast packets 00 broadcast packets 00
Interface ge2
Hardware is Ethernet
Current HW addr: 00a1.2599.0004
Physical:00a1.2599.0004
Description: BC4 - Slot 4 (Phys Slot 8)
index 5002 metric 1 mtu 1500 duplex-full arp ageing timeout 0
<BROADCAST,MULTICAST>
VRF Binding: Not bound
Bandwidth 1g
VRRP Master of : VRRP is not configured on this interface.
input packets 00, bytes 00, dropped 00, multicast packets 00
output packets 00, bytes 00, multicast packets 00 broadcast packets 00

I need to pick two things in two different variables.
1)Interface Name ---- that is ge1 or ge2 corresponding to Interface keyword
2)<UP,BROADCAST,RUNNING,MULTICAST> ... every individual words like UP/BROADCAST in this line

So when my parsing will be finished i will have information like
ifname = ge1 broadcast = BROADCAST multicast = MULTICAST
ifname = ge2 broadcast = BROADCAST multicast = MULTICAST
ifname = ge3 broadcast = BROADCAST multicast = MULTICAST

.
Could you guys help me first to find best approach for doing this. I want to implement it in C++. But if anything u have in your mind please share it.

I am so new with programing.

Thanks

Member Avatar for iamthwee

Seems straight forward enough.

A temporary storage device and string parsing.

What you got so far?

I tried this am not storing things now just for experiment. memset should not be necessary(mine is dumb code)
Any improvement is appreciated.

#include <stdio.h>   /* required for file operations */                                                               
#include <string.h>                                                                                                   
                                                                                                                      
FILE *fr;            /* declare the file pointer */                                                                   
                                                                                                                      
main()                                                                                                                
                                                                                                                      
{                                                                                                                     
    char ifname[80];                                                                                                  
    char firstStr[50];                                                                                                
    char line[80];                                                                                                    
    fr = fopen ("interface.txt", "rt");  /* open the file for reading */                                              
    while(fgets(line, 80, fr) != NULL)                                                                                
    {                                                                                                                 
      /* get a line, up to 80 chars from fr.  done if NULL */                                                         
      memset(firstStr,0,sizeof(firstStr));                                                                            
      memset(ifname,0,sizeof(ifname));                                                                                
      sscanf (line, "%50s %80s", firstStr, ifname);                                                                   
      if((strcmp(firstStr,"Interface") ==0) || (firstStr[0] == '<') &&                                                
         (firstStr[1] == 'U') && (firstStr[2] == 'P'))                                                                
      {                                                                                                               
        if((firstStr[0] == '<'))                                                                                      
        {                                                                                                             
          memcpy(firstStr, "UP", sizeof("UP"));                                                                       
          printf ("%s %s\n", firstStr,ifname);                                                                        
        }                                                                                                             
        else                                                                                                          
        {                                                                                                             
          printf ("%s %s\n", firstStr,ifname);                                                                        
        }                                                                                                             
      }                                                                                                               
      else if((firstStr[0] == '<'))                                                                                   
      {                                                                                                               
        memcpy(firstStr, "DOWN", sizeof("DOWN"));                                                                     
        printf ("%s %s\n", firstStr,ifname);                                                                          
      }                                                                                                               
    }                                                                                                                 
    fclose(fr);  /* close the file prior to exiting the routine */                                                    
} /*of main*/

Output is:

Interface xe25
DOWN
Interface xe26
DOWN
Interface xe27
UP
Interface xe28
UP

Member Avatar for iamthwee

^OK, so I see you are using c type coding in c++. Not that I don't know c...

I'll let someone else step in.

I'd like to be able to read your code. Please post a clean version of it. Here are some guidelines to help.

In general:read a line
check if one of your keywords is present
if so, parse the data out
go back to read another line

Your code is actually C. You're not using any C++-specific features. That's not necessarily a bad thing, just thought you should know. :)

If you want to stick with C-style functions you might find strncmp() useful instead of comparing individual elements of a char[] array. You should probably also make sure fopen() succeeded (didn't return NULL). Other than that, your code looks good.

Just for comparison, if you wanted to use C++ streams and whatnot you could end up with something like this (haven't compiled it, might not work):

#include <iostream>
#include <fstream>  // for file I/O
#include <string>

int main() {
    std::ifstream file("interface.txt");
    if(!file.is_open()) { /* ... */ }

    std::string line;
    while(std::getline(file, line)) {
        if(line.substr(0, 10) == "Interface ") {
            std::cout << line.substr(10) << std::endl;
        }
        else if(line[0] == '<') {
            if(line.substr(1, 2) == "UP") std::cout << "UP\n";
            else std::cout << "DOWN\n";
        }
    }
}

By the way, if you're going to be doing a lot of text processing I highly suggest you learn Perl. Here's approximately the same functionality written in Perl.

open(FILE, "interface.txt") or die;
while(<FILE>) {
    if(/^Interface (\w+)/) {
        print "Interface $1\n";
    }
    elsif(/^<(\w+)/) {
        print "UP\n" if($1 eq 'UP');
        print "DOWN\n" else;
    }
}

Just personal opinion. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.