Use the C language to implement ifconfig to obtain statistics on network card receiving and sending traffic

Source: Internet
Author: User
Tags strtok

In Windows, we can use the ipconfig command to obtain information about the NIC. in Linux, the command is ifconfig.

The information we can obtain is richer, including the traffic received and sent by the network card. It is not easy to use the C language to implement this command. As a result, the blogger consulted the relevant information, we know that the NIC information is saved in the/proc/net/dev folder. Therefore, we can obtain the NIC information by reading the information in this file.

This file contains four parts: Number of sent packets, sent traffic, number of received packets, and received traffic. At the same time, due to the constant changes in the network environment, therefore, the content of this file is also updated in real time.

The following figure shows the implementation result of the ifconfig command.

 
 


Note that there are many parameters that are not stored in the file

The following is a piece of C language code implemented by the blogger to obtain the received and sent traffic.

Notes have been provided for important parts.


[Cpp] view plaincopyprint?
# Include <stdio. h>
# Include <string. h>
# Include <sys/types. h>
# Include <sys/stat. h>
# Include <fcntl. h>
# Include <unistd. h>
# Include <stdlib. h>
 
Long * my_ipconfig (char * ath0)
{

Int nDevLen = strlen (ath0 );
If (nDevLen <1 | nDevLen> 100)
{
Printf ("dev length too long \ n ");
Return NULL;
}
Int fd = open ("/proc/net/dev", O_RDONLY | O_EXCL );
If (-1 = fd)
{
Printf ("/proc/net/dev not exists! \ N ");
Return NULL;
}

Char buf [1024*2];
Lseek (fd, 0, SEEK_SET );
Int nBytes = read (fd, buf, sizeof (buf)-1 );
If (-1 = nBytes)
{
Perror ("read error ");
Close (fd );
Return NULL;
}
Buf [nBytes] = '\ 0 ';
// Returns the pointer to ath0 for the first time.
Char * pDev = strstr (buf, ath0 );
If (NULL = pDev)
{
Printf ("don't find dev % s \ n", ath0 );
Return NULL;
}
Char * p;
Char * ifconfig_value;
Int I = 0;
Static long rx2_tx10 [2];
/* Remove unnecessary fields such as spaces, tabs, and line breaks */
For (p = strtok (pDev, "\ t \ r \ n"); p = strtok (NULL, "\ t \ r \ n "))
{
I ++;
Ifconfig_value = (char *) malloc (20 );
Strcpy (ifconfig_value, p );
/* The second field in the obtained string is the received traffic */
If (I = 2)
{
Rx2_tx10 [0] = atol (ifconfig_value );
}
/* The tenth field in the obtained string is the sending traffic */
If (I = 10)
{
Rx2_tx10 [1] = atol (ifconfig_value );
Break;
}
Free (ifconfig_value );
}
Return rx2_tx10;
}
Int main ()
{
Long * ifconfig_result;
Double re_mb;

/* Eth0 is the name of the NIC on the master computer */
Ifconfig_result = my_ipconfig ("eth0 ");

/* The unit of the value stored in the file is B, which is calculated and converted to MB */
Re_mb = (double) ifconfig_result [0]/(1024*1024 );
Printf ("received traffic: % 0.2f MB \ n", re_mb );

/* The unit of the value stored in the file is B, which is calculated and converted to MB */
Re_mb = (double) ifconfig_result [1]/(1024*1024 );
Printf ("sending traffic: % 0.2f MB \ n", re_mb );

}

# Include <stdio. h>
# Include <string. h>
# Include <sys/types. h>
# Include <sys/stat. h>
# Include <fcntl. h>
# Include <unistd. h>
# Include <stdlib. h>

Long * my_ipconfig (char * ath0)
{

Int nDevLen = strlen (ath0 );
If (nDevLen <1 | nDevLen> 100)
{
Printf ("dev length too long \ n ");
Return NULL;
}
Int fd = open ("/proc/net/dev", O_RDONLY | O_EXCL );
If (-1 = fd)
{
Printf ("/proc/net/dev not exists! \ N ");
Return NULL;
}

Char buf [1024*2];
Lseek (fd, 0, SEEK_SET );
Int nBytes = read (fd, buf, sizeof (buf)-1 );
If (-1 = nBytes)
{
Perror ("read error ");
Close (fd );
Return NULL;
}
Buf [nBytes] = '\ 0 ';
// Returns the pointer to ath0 for the first time.
Char * pDev = strstr (buf, ath0 );
If (NULL = pDev)
{
Printf ("don't find dev % s \ n", ath0 );
Return NULL;
}
Char * p;
Char * ifconfig_value;
Int I = 0;
Static long rx2_tx10 [2];
/* Remove unnecessary fields such as spaces, tabs, and line breaks */
For (p = strtok (pDev, "\ t \ r \ n"); p = strtok (NULL, "\ t \ r \ n "))
{
I ++;
Ifconfig_value = (char *) malloc (20 );
Strcpy (ifconfig_value, p );
/* The second field in the obtained string is the received traffic */
If (I = 2)
{
Rx2_tx10 [0] = atol (ifconfig_value );
}
/* The tenth field in the obtained string is the sending traffic */
If (I = 10)
{
Rx2_tx10 [1] = atol (ifconfig_value );
Break;
}
Free (ifconfig_value );
}
Return rx2_tx10;
}
Int main ()
{
Long * ifconfig_result;
Double re_mb;
 
/* Eth0 is the name of the NIC on the master computer */
Ifconfig_result = my_ipconfig ("eth0 ");
 
/* The unit of the value stored in the file is B, which is calculated and converted to MB */
Re_mb = (double) ifconfig_result [0]/(1024*1024 );
Printf ("received traffic: % 0.2f MB \ n", re_mb );

/* The unit of the value stored in the file is B, which is calculated and converted to MB */
Re_mb = (double) ifconfig_result [1]/(1024*1024 );
Printf ("sending traffic: % 0.2f MB \ n", re_mb );
 
}

The name of the saved file is dele. c.

Run the following commands:

Gcc-o dele. c

./Dele

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.