Linux uses C language to read Nic MAC addresses

Source: Internet
Author: User
Article Title: linux reads the MAC address of the NIC in C language. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

If you want to get the serial number of the hard disk and CPU, read the hdparm source code, compile and run it, but you cannot get the serial number of the hard disk, then try to read the MAC address of the NIC, this is the code found on the Internet.

# Include
# Include
# Include
# Include
# Include
# Include

Int main (int argc, char * argv [])
{

Struct ifreq;
Int sock;

If (argc! = 2)
{
Printf ("Usage: ethname \ n ");
Return 1;
}
If (sock = socket (AF_INET, SOCK_STREAM, 0) <0)
{
Perror ("socket ");
Return 2;
}
Strcpy (ifreq. ifr_name, argv [1]);
If (ioctl (sock, SIOCGIFHWADDR, & ifreq) <0)
{
Perror ("ioctl ");
Return 3;
}
Printf ("% 02x: % 02x: % 02x: % 02x: % 02x: % 02x \ n ",
(Unsigned char) ifreq. ifr_hwaddr.sa_data [0],
(Unsigned char) ifreq. ifr_hwaddr.sa_data [1],
(Unsigned char) ifreq. ifr_hwaddr.sa_data [2],
(Unsigned char) ifreq. ifr_hwaddr.sa_data [3],
(Unsigned char) ifreq. ifr_hwaddr.sa_data [4],
(Unsigned char) ifreq. ifr_hwaddr.sa_data [5]);
Return 0;
}

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.