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;
} |