-1

I am using the command ip -details -statistics link show can0

My result :

re-started bus-errors arbit-lost error-warn error-pass bus-off
  0          0          0          15         13         0         numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 
RX: bytes  packets  errors  dropped overrun mcast   
7110496    888812   9       0       9       0       
TX: bytes  packets  errors  dropped carrier collsns 
1761652    880826   0       0       0       0       

Is it possible to read error-warn, error-pass, overrun; and writing them to a variable in the C programming language?

Domi Nik
  • 1
  • 2

2 Answers2

1

Use handle = popen("LANG=C LC_ALL=C ip -details -statistics link show can0"); and getline() to read the six output lines. On the lines with important information, you can use a loop with strtoul() to parse the numeric fields, and keep only the ones you want.

Note that preceding the command with LANG=C LC_ALL=C ensures the output is in the default locale, untranslated. It may not matter in this particular case, but is useful whenever getting information this way using popen().

Glärbo
  • 91
  • 1
0

This is extremely fragile and certainly not recommended (I would expect you to spend a lot of time editing the format strings), but you can do something like:

#include <stdio.h>                                                                 
static void                                                                        
skip_line(FILE *fp)                                                                
{                                                                                  
        int c;                                                                     
        while( (c = getc(fp)) != EOF && c != '\n' )                                
                ;                                                                  
}                                                                                  
                                                                                   
int                                                                                
main(void)                                                                         
{                                                                                  
        const char *cmd = "ip -details -statistics link show can0":                
        int error_warn, error_pass, overrun;                                       
        FILE *fp = popen(cmd, "r");                                                
        if( fp == NULL ){                                                          
                perror("popen");                                                   
                return 1;                                                          
        }                                                                          
        skip_line(fp);                                                             
        if( 2 != fscanf(fp, "%*d%*d%*d%d%d", &error_warn, &error_pass) ){          
                fprintf(stderr, "unexpeced input");                                
                return 1;                                                          
        }                                                                          
        printf("error_warn: %d\n", error_warn);                                    
        printf("error_pass: %d\n", error_pass);                                    
        skip_line(fp); /* Read to end of 2nd line */                               
        skip_line(fp); /* Skip 3 line */                                           
        if( 1 != fscanf(fp, "%*d%*d%*d%*d%d", &overrun) ){                         
                fprintf(stderr, "unexpeced input");                                
                return 1;                                                          
        }                                                                          
        printf("overrun: %d\n", overrun);                                          
        return 0;                                                                  
}  
William Pursell
  • 204,365
  • 48
  • 270
  • 300