Notes Detecting network card and network cable Connection status under Linux

Source: Internet
Author: User

http://blog.chinaunix.net/space.php?uid=20357359&do=blog&cuid=1798479

Under Linux detects the network card and the network cable Connection status, uses the IOCTL to send to the socketSIOCETHTOOL命令字。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/sockios.h>
#include <linux/ethtool.h>
int get_netlink_status (const char *if_name);
int main ()
{
if (Getuid ()! = 0)
{
fprintf (stderr, "NetLink Status Check need Root power.\n");
return 1;
}
printf ("Net Link status:%d\n", Get_netlink_status ("eth0"));
return 0;
}
If_name like "Ath0", "eth0". Notice:call This function
Need root privilege.
return value:
-1--Error, details can check errno
1--Interface link up
0--Interface link down.
int get_netlink_status (const char *if_name)
{
int SKFD;
struct Ifreq IFR;
struct Ethtool_value edata;
Edata.cmd = Ethtool_glink;
Edata.data = 0;
memset (&AMP;IFR, 0, sizeof (IFR));
strncpy (Ifr.ifr_name, If_name, sizeof (Ifr.ifr_name)-1);
Ifr.ifr_data = (char *) &edata;
if ((SKFD = socket (af_inet, SOCK_DGRAM, 0)) < 0)
return-1;
if (IOCTL (SKFD, Siocethtool, &ifr) = =-1)
{
Close (SKFD);
return-1;
}
Close (SKFD);
return edata.data;
}

-----------------------------------------------------------

# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:25:35:68:CC:D6
inet addr:192.168.1.168 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::215:c5ff:fe18:ccd6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:130722 errors:0 dropped:0 overruns:0 frame:0
TX packets:112560 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:104371099 (99.5 MiB) TX bytes:20518584 (19.5 MiB)
Interrupt:16

One of the running means the network card and network cable normal link, unplug the network cable and then run this command will find running is not.
My goal is to use C language to implement the program, and Linux system provides popen/pclose process pipeline let C and shell very convenient interaction, but when using the attention to set permissions, so as not to create a security risk. Don't say much, look at the following C code combined with the shell command to detect the network card and network connectivity status:
Netstat.c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**********************************************************************
* Function Name: Getnetstat
* Function Description: Detect if the network link is disconnected
* Input Parameters:
* Output Parameters: None
* Return value: Normal link 1, break back-1
* Other Instructions: This program requires Superuser privileges to successfully invoke the Ifconfig command
* Modified Date version number modify the content of the person
* ---------------------------------------------------------------------
* 2010/04/02 V1.0 EDEN_MGQW
***********************************************************************/
int Getnetstat ()
{
Char Buffer[bufsiz];
FILE *READ_FP;
int chars_read;
int ret;
memset (buffer, 0, bufsiz);
READ_FP = Popen ("Ifconfig eth0 | grep RUNNING "," R ");
if (read_fp! = NULL)
{
Chars_read = fread (buffer, sizeof (char), BUFSIZ-1, READ_FP);
if (Chars_read > 0)
{
ret = 1;
}
Else
{
ret =-1;
}
Pclose (READ_FP);
}
Else
{
ret =-1;
}
return ret;
}
int main ()
{
int i=0;
i = Getnetstat ();
printf ("\nnetstat =%d\n", i);
return 0;
}

Notes Detecting network card and network cable Connection status under Linux

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.