linux机器mac地址查看,获取linux机器的IP和mac地址

#include // for ::strncpy

#include // for ::ioctl

#include // for inet_ntoa

#include // for struct ifreq

#include // for ::close

#include

#include

#include

using namespace std;

int GetNetDeviceAddress(int fd, const char* netDevice, struct in_addr* addr);

vector getNetDevName()

{

vector result;

ifstream ifs("/proc/net/dev");

string line;

if(ifs) {

while(getline(ifs, line)) {

size_t end = 0;

if ((end = line.find(":")) != string::npos) {

size_t start = line.find_first_not_of(' ');

string name = line.substr(start, end-start);

if (name.compare("lo")) {

result.push_back(name);

}

}

}

}

ifs.close();

return result;

}

vector getLocalIpAddress()

{

vector ipAddrs;

vector netdevs = getNetDevName();

if (netdevs.size() <= 0) {

return ipAddrs;

}

for(size_t i=0; i

int sock;

if ((sock = ::socket(AF_INET, SOCK_DGRAM, 0)) > 0) {

struct in_addr addr;

bzero(&addr, sizeof(addr));

if (!(GetNetDeviceAddress(sock, netdevs[i].c_str(), &addr))) {

ipAddrs.push_back(inet_ntoa(addr));

}

::close(sock);

}

}

return ipAddrs;

}

int GetNetDeviceAddress(int fd, const char* netDevice, struct in_addr* addr)

{

struct ifreq req;

::strncpy(req.ifr_name, netDevice, IF_NAMESIZE);

if (::ioctl(fd, SIOCGIFFLAGS, &req) < 0) {

return -1;

}

// Check whether network interface can be accessed

if ((req.ifr_flags & IFF_RUNNING) == 0) {

return -1;

}

// Retrieve binded IP address for the interface

if (::ioctl(fd, SIOCGIFADDR, &req) < 0) {

return -1;

}

else {

::memcpy(addr, &(((struct sockaddr_in*)&req.ifr_addr)->sin_addr), sizeof(struct in_addr));

}

return 0;

}

vector getLocalMacAddress()

{

vector macAddrs;

vector netdevs = getNetDevName();

if (netdevs.size() <= 0) {

return macAddrs;

}

for(size_t i=0; i

int sock;

if ((sock = ::socket(AF_INET, SOCK_DGRAM, 0)) > 0) {

struct ifreq ifr_mac;

bzero(&ifr_mac, sizeof(ifr_mac));

::strncpy(ifr_mac.ifr_name, netdevs[i].c_str(), IF_NAMESIZE);

char addr[32] = {0};

if (::ioctl(sock, SIOCGIFHWADDR, &ifr_mac) >= 0) {

snprintf(addr, 32, "%02x:%02x:%02x:%02x:%2x:%2x",

(unsigned char)ifr_mac.ifr_hwaddr.sa_data[0],

(unsigned char)ifr_mac.ifr_hwaddr.sa_data[1],

(unsigned char)ifr_mac.ifr_hwaddr.sa_data[2],

(unsigned char)ifr_mac.ifr_hwaddr.sa_data[3],

(unsigned char)ifr_mac.ifr_hwaddr.sa_data[4],

(unsigned char)ifr_mac.ifr_hwaddr.sa_data[5]);

macAddrs.push_back(addr);

}

else {

cout<

}

::close(sock);

}

}

return macAddrs;

}

int main()

{

vector ips = getLocalIpAddress();

vector macs = getLocalMacAddress();

cout << "IP:"<

for(size_t i=0; i

cout << "\t" << ips[i] << endl;

}

cout << endl;

cout << "Mac:"<

for(size_t i=0; i

cout << "\t" << macs[i] << endl;

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值