How to use C Programming in Linux to check whether the network card is connected to the network cable | Linux Command to check whether the network card is connected to the network cable

Source: Internet
Author: User
1. Use C programming to check whether the network card is connected to the network cable # Include <net/If. h> // iff_running
# Include <sys/IOCTL. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <net/if_arp.h>

Int main ()
{
If (check_nic ("eth0") = 0)
Printf ("detect OK./N ");
Else
Printf ("detect fail./N ");
}

// If the network card is connected, 0 is returned; otherwise,-1 is returned.
Int check_nic (char * nic_name)
{
Struct ifreq IFR;
Int skfd = socket (af_inet, sock_dgram, 0 );

strcpy (IFR. ifr_name, nic_name);
If (IOCTL (skfd, siocgifflags, & IFR) <0)
{< br> return-1;
}< br> If (IFR. ifr_flags & iff_running)
return 0; // network cable inserted into the NIC
else return-1;
}< span style = "color: #000000; "> 2. Run the Linux Command to check whether the NIC is connected to the network root # ethtool eth0 reference: http://topic.csdn.net/u/20100920/16/7D58E76F-39FE-44B3-B4AC-D65D5AE57D2D.html

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.