How to get the Mac Eth0 in the two Linux methods

xiaoxiao2021-03-06  64

How to obtain the MAC address of the machine Eth0 in Linux, use IOCTL to implement the device to implement the device to implement the device IOCTL and view the file attached to the source code: / * Get this unit with the C language Hardware Address * / # include #include #include #include #include #include #include #include

#define maxInterfaces 16

INT main (argc, argv) register int argc; register char * argv []; {register int fd, intrface, retrn = 0; struct ifreq buf [maxinterface]; struct arpRP; struct ifconf IFC;

IF ((fd = socket, sock_dgram, 0)> = 0) {ifc.ifc_len = sizeof buf; ifc.ifc_buf = (CADDR_T) BUF; if (! ioctl (fd, siocifconf, (char *) & IFC) ) {INTRFACE = IFC.IFC_LEN / SIZEOF (Struct IFREQ); Printf ("Interface Num IS Intrface =% D / N / N / N", IntrFace; While (IntrFace--> 0) {Printf ("Net Device% S / N ", BUF [IntrFace] .IFR_NAME);

/ * Jugde WHETHER THE NET CARD Status is promisc * / if (! (Fd, siocgifflags, (char *) & busf [intrface])) {ix (buf [Intrface] .IFR_FLAGS & IFF_PROMISC) {PUTS ("THE Interface is promisc "); RETN ;}} else {char str [256];

Sprintf (STR, "CPM: IOCTL DEVICE% S", BUF [IntrFace] .IFR_NAME); PERROR (STR);}

/ * Jugde WHETER THE NET CARD Status is up * / if (buf [Intrface] .IFR_FLAGS & IFF_UP) {PUTS ("The Interface Status IS Up");} else {PUTS ("The Interface Status Is Down";

/ * Get IP of the net card * / if (! (Ioctl (fd, siocifaddr, (char *) & busf [intrface])) {PUTS ("IP Address IS:"); PUTS (INET_NTOA ((Struct SockAddr_in) * (& Buf [IntrFace] .IFR_ADDR)) -> SIN_ADDR); PUTS (""); // Puts (buf [IntrFace] .IFR_ADDR.SA_DATA);} else {char str [256]; sprintf (Str, "CPM: IOCTL DEVICE% S", BUF [IntrFace] .IFR_NAME); PERROR (STR);}

/ * Get HW address of the net card * / if (! (Fd, siocgifhwaddr))) {PUTS ("HW address is:");

Printf ("% 02x:% 02x:% 02x:% 02x:% 02x: 02x ​​/ n", (unsigned char) buf [Intrface] .IFR_HWADDR. sa_data [0], (unsigned char) buf [Intrface] .IFR_HWADDR SA_DATA [1], (Unsigned Char) BUF [Intrface]. IFR_HWADDR. Sa_data [2], (unsigned char) buf [Intrface] .IFR_HWADDR. Sa_data [3], (unsigned char) buf [IntrFace] .IFR_HWADDR. Sa_data [4] (UNSIGNED Char) BUF [IntrFace] .IFR_HWADDR. SA_DATA [5]);

PUTS (""); PUTS ("");

Else {char str [256];

Sprintf (STR, "CPM: IOCTL DEVICE% S", BUF [Intrface] .IFR_NAME); PERROR (STR);}}} Else Perror ("CPM: IOCTL");

Else Perror ("CPM: Socket");

Close (fd); Return Retn;}

Another way:

转载请注明原文地址:https://www.9cbs.com/read-112561.html

New Post(0)