LINUX get IP address and MAC address __linux

Source: Internet
Author: User
Tags get ip sprintf

#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h> #include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/if_arp.h> #ifdef SOLARIS
#include <sys/sockio.h>
#endif #define Maxinterfaces Main (argc, argv)
register int argc;
Register Char *argv[];
{
Register int FD, intrface, retn = 0;
struct Ifreq buf[maxinterfaces];
struct Arpreq ARP;
struct ifconf ifc; if (FD = socket (af_inet, SOCK_DGRAM, 0)) >= 0)
{
Ifc.ifc_len = sizeof buf;
Ifc.ifc_buf = (caddr_t) buf;
if (!ioctl (FD, siocgifconf, (char *) &AMP;IFC)
{
Get interface Information
Intrface = ifc.ifc_len/sizeof (struct ifreq);
printf ("Interface num is intrface=%d\n\n\n", intrface);
Get device IP and MAC address based on the pretext information loop
while (intrface--> 0)
{
Get device Name
printf ("NET device%s\n", buf[intrface].ifr_name);

To determine the type of network card
if (!) ( IOCTL (FD, Siocgifflags, (char *) &buf[intrface]))
{
if (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);
}//Determine NIC status
if (Buf[intrface].ifr_flags & iff_up)
{
Puts ("The interface status is up");
}
Else
{
Puts ("The interface status is down");
//Get the IP address of the current network card
if (!) ( IOCTL (FD, SIOCGIFADDR, (char *) &buf[intrface]))
{
Puts ("IP address is:");
Puts (Inet_ntoa (struct sockaddr_in*) (&buf[intrface].ifr_addr)) (-&GT;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);
}
/* This section can ' t get hardware address,i don ' t know whether the reason is module driver*/
#ifdef SOLARIS
Get MAC Address
arp.arp_pa.sa_family = af_inet;
arp.arp_ha.sa_family = af_inet;
((struct sockaddr_in*) &arp.arp_pa)->sin_addr.s_addr= ((struct sockaddr_in*) (&buf[intrface].ifr_addr))- >sin_addr.s_addr;
if (!) ( IOCTL (FD, SIOCGARP, (char *) &arp))
{
Puts ("HW address is:"); Display MAC address in hexadecimal
printf ("%02x:%02x:%02x:%02x:%02x:%02x\n",
(unsigned char) arp.arp_ha.sa_data[0],
(unsigned char) arp.arp_ha.sa_data[1],
(unsigned char) arp.arp_ha.sa_data[2],
(unsigned char) arp.arp_ha.sa_data[3],
(unsigned char) arp.arp_ha.sa_data[4],
(unsigned char) arp.arp_ha.sa_data[5]); Puts ("");
Puts ("");
}
#else
#if 0
/*get HW Address for the net card * *
if (!) ( IOCTL (FD, SIOCGENADDR, (char *) &buf[intrface]))
{
Puts ("HW address is:"); printf ("%02x:%02x:%02x:%02x:%02x:%02x\n",
(unsigned char) buf[intrface].ifr_enaddr[0],
(unsigned char) buf[intrface].ifr_enaddr[1],
(unsigned char) buf[intrface].ifr_enaddr[2],
(unsigned char) buf[intrface].ifr_enaddr[3],
(unsigned char) buf[intrface].ifr_enaddr[4],
(unsigned char) buf[intrface].ifr_enaddr[5]); Puts ("");
Puts ("");
}
#endif
if (!) ( IOCTL (FD, SIOCGIFHWADDR, (char *) &buf[intrface]))
{
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 ("");
}
#endif Else
{
Char str[256]; sprintf (str, "Cpm:ioctl device%s", buf[intrface].ifr_name);
Perror (str);
}
}//while
} else
Perror ("Cpm:ioctl"); } else
Perror ("Cpm:socket"); Close (FD);
return RETN;
Compile: GCC filename

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.