In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Function: query local IP/MAC address and filter out 127.0.0.1 loop-back address
Applicable: linux, ubuntu 16.04 debugging passed
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
Void getLocalMacIp ()
{
Int i
Int sock_mac
Char hname [128]
Struct hostent * hent
Struct in_addr * paddr = 0
Struct ifaddrs * ifAddrStruct=NULL
Struct ifreq ifr_mac
Void * tmpAddrPtr=NULL
Struct ifreq buf [16]
Struct ifconf ifc
Int interface_num
Char nif_name [128] = {0}
Char addressBuffer [128]
Unsigned char macaddr [6] = {0}
Unsigned char * pFirstByte
/ /-
/ / get device IP address, discard loopback
/ /-
Gethostname (hname, sizeof (hname))
Hent = gethostbyname (hname)
Printf ("[% s] [% d] host:% s\ n", _ _ FILE__, _ LINE__, hent- > h_name)
Getifaddrs & ifAddrStruct)
While (ifAddrStructneighbor null)
{
If (ifAddrStruct- > ifa_addr- > sa_family==AF_INET)
{
TmpAddrPtr = & ((struct sockaddr_in *) ifAddrStruct- > ifa_addr)-> sin_addr
PFirstByte = (unsigned char*) tmpAddrPtr
If (* pFirstByte! = 127)
{
Paddr = tmpAddrPtr
Inet_ntop (AF_INET, tmpAddrPtr, addressBuffer, 128)
Printf ("[% s] [% d] NIF% s address% s\ n", _ _ FILE__, _ _ LINE__
IfAddrStruct- > ifa_name, addressBuffer)
/ /-
/ / remember NIF name for future use
/ /-
Strcpy (nif_name, ifAddrStruct- > ifa_name)
}
}
IfAddrStruct=ifAddrStruct- > ifa_next
}
/ /-
/ / get device MAC address, discard loopback
/ /-
Sock_mac = socket (AF_INET, SOCK_DGRAM, 0)
If (sock_mac > 0)
{
Ifc.ifc_len = sizeof (buf)
Ifc.ifc_req = buf
Ioctl (sock_mac, SIOCGIFCONF, (char *) & ifc)
Interface_num = ifc.ifc_len / sizeof (struct ifreq)
While (interface_num--)
{
If (nif_name [0] & & strcmp (nif_name, BUF [interface _ num] .ifr _ name))
{
Continue
}
Ioctl (sock_mac, SIOCGIFHWADDR, (char *) & BUF [interface _ num])
Memcpy (macaddr, & buf [interface _ num] .ifr _ hwaddr.sa_data [0], 6)
/ / printf ("NIF% s", buf [interface _ num] .ifr _ name)
/ / printf ("mac x:x:x:x:x:x\ n"
/ / macaddr [0], macaddr [1], macaddr [2], macaddr [3], macaddr [4], macaddr [5])
}
Close (sock_mac)
}
}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.